| 1 | /* |
| 2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdbool.h> |
| 26 | #include <string.h> |
| 27 | #include <babeltrace/assert-internal.h> |
| 28 | #include <babeltrace/babeltrace.h> |
| 29 | #include <popt.h> |
| 30 | #include <glib.h> |
| 31 | |
| 32 | static |
| 33 | void print_usage(FILE *fp) |
| 34 | { |
| 35 | fprintf(stderr, "Usage: babeltrace-log [OPTIONS] OUTPUT-PATH\n"); |
| 36 | fprintf(stderr, "\n"); |
| 37 | fprintf(stderr, "Options:\n"); |
| 38 | fprintf(stderr, "\n"); |
| 39 | fprintf(stderr, " -t, --with-timestamps Extract timestamps from lines and map them to\n"); |
| 40 | fprintf(stderr, " a CTF clock class\n"); |
| 41 | } |
| 42 | |
| 43 | static |
| 44 | int parse_params(int argc, char *argv[], char **output_path, |
| 45 | bool *no_extract_ts) |
| 46 | { |
| 47 | enum { |
| 48 | OPT_WITH_TIMESTAMPS = 1, |
| 49 | OPT_HELP = 2, |
| 50 | }; |
| 51 | static struct poptOption opts[] = { |
| 52 | { "with-timestamps", 't', POPT_ARG_NONE, NULL, OPT_WITH_TIMESTAMPS, NULL, NULL }, |
| 53 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
| 54 | { NULL, '\0', 0, NULL, 0, NULL, NULL }, |
| 55 | }; |
| 56 | poptContext pc = NULL; |
| 57 | int opt; |
| 58 | int ret = 0; |
| 59 | const char *leftover; |
| 60 | |
| 61 | *no_extract_ts = true; |
| 62 | pc = poptGetContext(NULL, argc, (const char **) argv, opts, 0); |
| 63 | if (!pc) { |
| 64 | fprintf(stderr, "Cannot get popt context\n"); |
| 65 | goto error; |
| 66 | } |
| 67 | |
| 68 | poptReadDefaultConfig(pc, 0); |
| 69 | |
| 70 | while ((opt = poptGetNextOpt(pc)) > 0) { |
| 71 | switch (opt) { |
| 72 | case OPT_HELP: |
| 73 | print_usage(stdout); |
| 74 | goto end; |
| 75 | case OPT_WITH_TIMESTAMPS: |
| 76 | *no_extract_ts = false; |
| 77 | break; |
| 78 | default: |
| 79 | fprintf(stderr, "Unknown command-line option specified (option code %d)\n", |
| 80 | opt); |
| 81 | goto error; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (opt < -1) { |
| 86 | fprintf(stderr, "While parsing command-line options, at option %s: %s\n", |
| 87 | poptBadOption(pc, 0), poptStrerror(opt)); |
| 88 | goto error; |
| 89 | } |
| 90 | |
| 91 | leftover = poptGetArg(pc); |
| 92 | if (!leftover) { |
| 93 | fprintf(stderr, "Command line error: Missing output path\n"); |
| 94 | print_usage(stderr); |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | *output_path = strdup(leftover); |
| 99 | BT_ASSERT(*output_path); |
| 100 | goto end; |
| 101 | |
| 102 | error: |
| 103 | ret = 1; |
| 104 | |
| 105 | end: |
| 106 | if (pc) { |
| 107 | poptFreeContext(pc); |
| 108 | } |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | int main(int argc, char *argv[]) |
| 114 | { |
| 115 | char *output_path = NULL; |
| 116 | bool no_extract_ts; |
| 117 | int retcode; |
| 118 | GError *error = NULL; |
| 119 | gchar *bt_argv[] = { |
| 120 | BT_CLI_PATH, |
| 121 | "run", |
| 122 | "--component", |
| 123 | "dmesg:src.text.dmesg", |
| 124 | "--params", |
| 125 | NULL, /* no-extract-timestamp=? placeholder */ |
| 126 | "--component", |
| 127 | "ctf:sink.ctf.fs", |
| 128 | "--key", |
| 129 | "path", |
| 130 | "--value", |
| 131 | NULL, /* output path placeholder */ |
| 132 | "--params", |
| 133 | "single-trace=yes", |
| 134 | "--connect", |
| 135 | "dmesg:ctf", |
| 136 | NULL, /* sentinel */ |
| 137 | }; |
| 138 | |
| 139 | retcode = parse_params(argc, argv, &output_path, &no_extract_ts); |
| 140 | if (retcode) { |
| 141 | goto end; |
| 142 | } |
| 143 | |
| 144 | if (no_extract_ts) { |
| 145 | bt_argv[5] = "no-extract-timestamp=yes"; |
| 146 | } else { |
| 147 | bt_argv[5] = "no-extract-timestamp=no"; |
| 148 | } |
| 149 | |
| 150 | bt_argv[11] = output_path; |
| 151 | (void) g_spawn_sync(NULL, bt_argv, NULL, |
| 152 | G_SPAWN_CHILD_INHERITS_STDIN, NULL, NULL, |
| 153 | NULL, NULL, &retcode, &error); |
| 154 | |
| 155 | if (error) { |
| 156 | fprintf(stderr, "Failed to execute \"%s\": %s (%d)\n", |
| 157 | bt_argv[0], error->message, error->code); |
| 158 | } |
| 159 | |
| 160 | end: |
| 161 | free(output_path); |
| 162 | |
| 163 | if (error) { |
| 164 | g_error_free(error); |
| 165 | } |
| 166 | |
| 167 | return retcode; |
| 168 | } |