Commit | Line | Data |
---|---|---|
34ac0e6c MD |
1 | /* |
2 | * babeltrace.c | |
3 | * | |
4 | * Babeltrace Trace Converter | |
5 | * | |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
34ac0e6c MD |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
c462e188 MD |
19 | * |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
34ac0e6c | 27 | */ |
4c8bfb7e | 28 | |
95d36295 | 29 | #include <babeltrace/babeltrace.h> |
7fb21036 | 30 | #include <babeltrace/format.h> |
95d36295 | 31 | #include <babeltrace/context.h> |
7237592a | 32 | #include <babeltrace/context-internal.h> |
95d36295 | 33 | #include <babeltrace/ctf/types.h> |
9843982d | 34 | #include <babeltrace/ctf/events.h> |
04ae3991 MD |
35 | /* TODO: fix object model for format-agnostic callbacks */ |
36 | #include <babeltrace/ctf/events-internal.h> | |
9efd5d76 | 37 | #include <babeltrace/ctf/iterator.h> |
31bdef5c | 38 | #include <babeltrace/ctf-text/types.h> |
2748fb3b | 39 | #include <babeltrace/debug-info.h> |
c40a57e5 | 40 | |
6204d33c | 41 | #include <babeltrace/iterator.h> |
33bceaf8 | 42 | #include <babeltrace/plugin/component-factory.h> |
34ac0e6c MD |
43 | #include <popt.h> |
44 | #include <errno.h> | |
45 | #include <stdlib.h> | |
bbefb8dd MD |
46 | #include <ctype.h> |
47 | #include <sys/stat.h> | |
48 | #include <sys/types.h> | |
49 | #include <fcntl.h> | |
afb48eae | 50 | #include <unistd.h> |
70accc14 | 51 | #include <inttypes.h> |
a99343cf | 52 | #include <ftw.h> |
11e1d048 | 53 | #include <string.h> |
4c8bfb7e | 54 | |
a44bc4c9 MD |
55 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
56 | ||
24d5af5b MD |
57 | #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */ |
58 | ||
afb48eae | 59 | #define DEFAULT_FILE_ARRAY_SIZE 1 |
9fe26ec7 | 60 | |
34d99418 JD |
61 | #define NET_URL_PREFIX "net://" |
62 | #define NET4_URL_PREFIX "net4://" | |
63 | #define NET6_URL_PREFIX "net6://" | |
64 | ||
33bceaf8 | 65 | static struct bt_component_factory *component_factory; |
11e1d048 | 66 | static char *opt_input_format, *opt_output_format; |
34ac0e6c | 67 | |
41075e78 MD |
68 | /* |
69 | * We are not freeing opt_input_paths ipath elements when exiting from | |
70 | * main() for backward compatibility with libpop 0.13, which does not | |
71 | * allocate copies for arguments returned by poptGetArg(), and for | |
72 | * general compatibility with the documented behavior. This is known to | |
73 | * cause a small memory leak with libpop 0.16. | |
74 | */ | |
4e85cbfd | 75 | static GPtrArray *opt_input_paths; |
9fe26ec7 | 76 | static char *opt_output_path; |
c40a57e5 | 77 | static int opt_stream_intersection; |
33bceaf8 | 78 | static char *opt_plugin_path; |
34ac0e6c | 79 | |
37b99bdb | 80 | static struct bt_format *fmt_read; |
afb48eae | 81 | |
7e3e3582 JG |
82 | void bt_dummy_hook(void); |
83 | void bt_lttng_live_hook(void); | |
84 | void bt_ctf_hook(void); | |
85 | void bt_ctf_text_hook(void); | |
86 | void bt_ctf_metadata_hook(void); | |
87 | ||
11e1d048 | 88 | static |
bbefb8dd MD |
89 | void strlower(char *str) |
90 | { | |
91 | while (*str) { | |
34224260 | 92 | *str = tolower((int) *str); |
bbefb8dd MD |
93 | str++; |
94 | } | |
95 | } | |
96 | ||
34ac0e6c MD |
97 | enum { |
98 | OPT_NONE = 0, | |
9fe26ec7 MD |
99 | OPT_OUTPUT_PATH, |
100 | OPT_INPUT_FORMAT, | |
101 | OPT_OUTPUT_FORMAT, | |
34ac0e6c | 102 | OPT_HELP, |
7fb21036 | 103 | OPT_LIST, |
33bceaf8 | 104 | OPT_PLUGIN_PATH, |
34ac0e6c MD |
105 | OPT_VERBOSE, |
106 | OPT_DEBUG, | |
d63ca2cd | 107 | OPT_NAMES, |
359d7456 | 108 | OPT_FIELDS, |
8d8ed9af | 109 | OPT_NO_DELTA, |
11ac6674 | 110 | OPT_CLOCK_OFFSET, |
65923160 | 111 | OPT_CLOCK_OFFSET_NS, |
03798a93 | 112 | OPT_CLOCK_CYCLES, |
11ac6674 MD |
113 | OPT_CLOCK_SECONDS, |
114 | OPT_CLOCK_DATE, | |
115 | OPT_CLOCK_GMT, | |
82ace6d6 | 116 | OPT_CLOCK_FORCE_CORRELATE, |
7da9b2f3 | 117 | OPT_STREAM_INTERSECTION, |
05984e0c | 118 | OPT_DEBUG_INFO_DIR, |
ff9ce920 | 119 | OPT_DEBUG_INFO_FULL_PATH, |
5cde0dc1 | 120 | OPT_DEBUG_INFO_TARGET_PREFIX, |
34ac0e6c MD |
121 | }; |
122 | ||
9fe26ec7 | 123 | /* |
33bceaf8 | 124 | * We are _not_ using POPT_ARG_STRING's ability to store directly into |
9fe26ec7 | 125 | * variables, because we want to cast the return to non-const, which is |
41075e78 MD |
126 | * not possible without using poptGetOptArg explicitly. This helps us |
127 | * controlling memory allocation correctly without making assumptions | |
128 | * about undocumented behaviors. poptGetOptArg is documented as | |
129 | * requiring the returned const char * to be freed by the caller. | |
9fe26ec7 | 130 | */ |
34ac0e6c MD |
131 | static struct poptOption long_options[] = { |
132 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
c790c052 | 133 | { "output", 'w', POPT_ARG_STRING, NULL, OPT_OUTPUT_PATH, NULL, NULL }, |
9fe26ec7 MD |
134 | { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL }, |
135 | { "output-format", 'o', POPT_ARG_STRING, NULL, OPT_OUTPUT_FORMAT, NULL, NULL }, | |
34ac0e6c | 136 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
7fb21036 | 137 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
33bceaf8 | 138 | { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL }, |
34ac0e6c MD |
139 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
140 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, | |
cba1661c | 141 | { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL }, |
359d7456 | 142 | { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL }, |
8d8ed9af | 143 | { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL }, |
11ac6674 | 144 | { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL }, |
65923160 | 145 | { "clock-offset-ns", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET_NS, NULL, NULL }, |
03798a93 | 146 | { "clock-cycles", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL }, |
11ac6674 MD |
147 | { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL }, |
148 | { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL }, | |
149 | { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL }, | |
82ace6d6 | 150 | { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL }, |
7da9b2f3 | 151 | { "stream-intersection", 0, POPT_ARG_NONE, NULL, OPT_STREAM_INTERSECTION, NULL, NULL }, |
2748fb3b | 152 | #ifdef ENABLE_DEBUG_INFO |
05984e0c | 153 | { "debug-info-dir", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_DIR, NULL, NULL }, |
ff9ce920 | 154 | { "debug-info-full-path", 0, POPT_ARG_NONE, NULL, OPT_DEBUG_INFO_FULL_PATH, NULL, NULL }, |
5cde0dc1 | 155 | { "debug-info-target-prefix", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_TARGET_PREFIX, NULL, NULL }, |
c40a57e5 | 156 | #endif |
34ac0e6c MD |
157 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
158 | }; | |
159 | ||
7fb21036 MD |
160 | static void list_formats(FILE *fp) |
161 | { | |
162 | fprintf(fp, "\n"); | |
163 | bt_fprintf_format_list(fp); | |
164 | } | |
165 | ||
34ac0e6c | 166 | static void usage(FILE *fp) |
4c8bfb7e | 167 | { |
4c15b06b | 168 | fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION); |
4e85cbfd | 169 | fprintf(fp, "usage : babeltrace [OPTIONS] FILE...\n"); |
34ac0e6c | 170 | fprintf(fp, "\n"); |
4e85cbfd MD |
171 | fprintf(fp, " FILE Input trace file(s) and/or directory(ies)\n"); |
172 | fprintf(fp, " (space-separated)\n"); | |
173 | fprintf(fp, " -w, --output OUTPUT Output trace path (default: stdout)\n"); | |
34ac0e6c | 174 | fprintf(fp, "\n"); |
d2b8ea6b MD |
175 | fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n"); |
176 | fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n"); | |
34ac0e6c | 177 | fprintf(fp, "\n"); |
4d5678b9 MD |
178 | fprintf(fp, " -h, --help This help message\n"); |
179 | fprintf(fp, " -l, --list List available formats\n"); | |
33bceaf8 | 180 | fprintf(fp, " --plugin-path Supplementary plug-in path\n"); |
4d5678b9 | 181 | fprintf(fp, " -v, --verbose Verbose mode\n"); |
cba1661c | 182 | fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n"); |
4d5678b9 | 183 | fprintf(fp, " -d, --debug Debug mode\n"); |
cba1661c | 184 | fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n"); |
8d8ed9af | 185 | fprintf(fp, " --no-delta Do not print time delta between consecutive events\n"); |
359d7456 | 186 | fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n"); |
82662ad4 | 187 | fprintf(fp, " (payload OR args OR arg)\n"); |
12c9c3bc MD |
188 | fprintf(fp, " none, all, scope, header, (context OR ctx)\n"); |
189 | fprintf(fp, " (default: payload,context)\n"); | |
359d7456 | 190 | fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n"); |
32cfb8ad | 191 | fprintf(fp, " all, trace, trace:hostname, trace:domain,\n"); |
f133896d | 192 | fprintf(fp, " trace:procname, trace:vpid, loglevel, emf, callsite.\n"); |
c3815874 | 193 | fprintf(fp, " (default: trace:hostname,trace:procname,trace:vpid)\n"); |
03798a93 | 194 | fprintf(fp, " --clock-cycles Timestamp in cycles\n"); |
11ac6674 | 195 | fprintf(fp, " --clock-offset seconds Clock offset in seconds\n"); |
65923160 | 196 | fprintf(fp, " --clock-offset-ns ns Clock offset in nanoseconds\n"); |
11ac6674 MD |
197 | fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n"); |
198 | fprintf(fp, " (default is: [hh:mm:ss.ns])\n"); | |
199 | fprintf(fp, " --clock-date Print clock date\n"); | |
200 | fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n"); | |
82ace6d6 MD |
201 | fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n"); |
202 | fprintf(fp, " across traces.\n"); | |
7da9b2f3 | 203 | fprintf(fp, " --stream-intersection Only print events when all streams are active.\n"); |
2748fb3b | 204 | #ifdef ENABLE_DEBUG_INFO |
c40a57e5 AB |
205 | fprintf(fp, " --debug-info-dir Directory in which to look for debugging information\n"); |
206 | fprintf(fp, " files. (default: /usr/lib/debug/)\n"); | |
5cde0dc1 | 207 | fprintf(fp, " --debug-info-target-prefix Directory to use as a prefix for executable lookup\n"); |
5e28eb5b | 208 | fprintf(fp, " --debug-info-full-path Show full debug info source and binary paths (if available)\n"); |
c40a57e5 | 209 | #endif |
7fb21036 | 210 | list_formats(fp); |
34ac0e6c MD |
211 | fprintf(fp, "\n"); |
212 | } | |
213 | ||
cba1661c MD |
214 | static int get_names_args(poptContext *pc) |
215 | { | |
216 | char *str, *strlist, *strctx; | |
9f2c779c | 217 | int ret = 0; |
cba1661c MD |
218 | |
219 | opt_payload_field_names = 0; | |
12c9c3bc | 220 | opt_context_field_names = 0; |
cba1661c MD |
221 | strlist = (char *) poptGetOptArg(*pc); |
222 | if (!strlist) { | |
223 | return -EINVAL; | |
224 | } | |
225 | str = strtok_r(strlist, ",", &strctx); | |
226 | do { | |
227 | if (!strcmp(str, "all")) | |
228 | opt_all_field_names = 1; | |
229 | else if (!strcmp(str, "scope")) | |
230 | opt_scope_field_names = 1; | |
231 | else if (!strcmp(str, "context") || !strcmp(str, "ctx")) | |
232 | opt_context_field_names = 1; | |
233 | else if (!strcmp(str, "header")) | |
234 | opt_header_field_names = 1; | |
235 | else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg")) | |
236 | opt_payload_field_names = 1; | |
12c9c3bc MD |
237 | else if (!strcmp(str, "none")) { |
238 | opt_all_field_names = 0; | |
239 | opt_scope_field_names = 0; | |
240 | opt_context_field_names = 0; | |
241 | opt_header_field_names = 0; | |
242 | opt_payload_field_names = 0; | |
243 | } else { | |
359d7456 | 244 | fprintf(stderr, "[error] unknown field name type %s\n", str); |
9f2c779c MD |
245 | ret = -EINVAL; |
246 | goto end; | |
359d7456 MD |
247 | } |
248 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
9f2c779c MD |
249 | end: |
250 | free(strlist); | |
251 | return ret; | |
359d7456 MD |
252 | } |
253 | ||
254 | static int get_fields_args(poptContext *pc) | |
255 | { | |
256 | char *str, *strlist, *strctx; | |
9f2c779c | 257 | int ret = 0; |
359d7456 | 258 | |
359d7456 MD |
259 | strlist = (char *) poptGetOptArg(*pc); |
260 | if (!strlist) { | |
261 | return -EINVAL; | |
262 | } | |
263 | str = strtok_r(strlist, ",", &strctx); | |
264 | do { | |
c3815874 | 265 | opt_trace_default_fields = 0; |
359d7456 MD |
266 | if (!strcmp(str, "all")) |
267 | opt_all_fields = 1; | |
82662ad4 | 268 | else if (!strcmp(str, "trace")) |
359d7456 | 269 | opt_trace_field = 1; |
c3815874 MD |
270 | else if (!strcmp(str, "trace:hostname")) |
271 | opt_trace_hostname_field = 1; | |
8c250d87 | 272 | else if (!strcmp(str, "trace:domain")) |
359d7456 | 273 | opt_trace_domain_field = 1; |
8c250d87 | 274 | else if (!strcmp(str, "trace:procname")) |
359d7456 | 275 | opt_trace_procname_field = 1; |
8c250d87 | 276 | else if (!strcmp(str, "trace:vpid")) |
359d7456 | 277 | opt_trace_vpid_field = 1; |
d86d62f8 | 278 | else if (!strcmp(str, "loglevel")) |
359d7456 | 279 | opt_loglevel_field = 1; |
f6714e20 MD |
280 | else if (!strcmp(str, "emf")) |
281 | opt_emf_field = 1; | |
f133896d MD |
282 | else if (!strcmp(str, "callsite")) |
283 | opt_callsite_field = 1; | |
cba1661c | 284 | else { |
359d7456 | 285 | fprintf(stderr, "[error] unknown field type %s\n", str); |
9f2c779c MD |
286 | ret = -EINVAL; |
287 | goto end; | |
cba1661c MD |
288 | } |
289 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
9f2c779c MD |
290 | end: |
291 | free(strlist); | |
292 | return ret; | |
cba1661c MD |
293 | } |
294 | ||
34ac0e6c MD |
295 | /* |
296 | * Return 0 if caller should continue, < 0 if caller should return | |
297 | * error, > 0 if caller should exit without reporting error. | |
298 | */ | |
bbefb8dd | 299 | static int parse_options(int argc, char **argv) |
34ac0e6c MD |
300 | { |
301 | poptContext pc; | |
302 | int opt, ret = 0; | |
41075e78 | 303 | const char *ipath; |
34ac0e6c | 304 | |
0f980a35 MD |
305 | if (argc == 1) { |
306 | usage(stdout); | |
307 | return 1; /* exit cleanly */ | |
308 | } | |
309 | ||
bbefb8dd | 310 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
34ac0e6c MD |
311 | poptReadDefaultConfig(pc, 0); |
312 | ||
cba1661c | 313 | /* set default */ |
e505794f | 314 | opt_context_field_names = 1; |
cba1661c MD |
315 | opt_payload_field_names = 1; |
316 | ||
34ac0e6c MD |
317 | while ((opt = poptGetNextOpt(pc)) != -1) { |
318 | switch (opt) { | |
9fe26ec7 MD |
319 | case OPT_OUTPUT_PATH: |
320 | opt_output_path = (char *) poptGetOptArg(pc); | |
321 | if (!opt_output_path) { | |
322 | ret = -EINVAL; | |
323 | goto end; | |
324 | } | |
325 | break; | |
326 | case OPT_INPUT_FORMAT: | |
327 | opt_input_format = (char *) poptGetOptArg(pc); | |
328 | if (!opt_input_format) { | |
329 | ret = -EINVAL; | |
330 | goto end; | |
331 | } | |
332 | break; | |
333 | case OPT_OUTPUT_FORMAT: | |
334 | opt_output_format = (char *) poptGetOptArg(pc); | |
335 | if (!opt_output_format) { | |
336 | ret = -EINVAL; | |
337 | goto end; | |
338 | } | |
339 | break; | |
34ac0e6c | 340 | case OPT_HELP: |
7fb21036 | 341 | usage(stdout); |
34ac0e6c MD |
342 | ret = 1; /* exit cleanly */ |
343 | goto end; | |
7fb21036 MD |
344 | case OPT_LIST: |
345 | list_formats(stdout); | |
346 | ret = 1; | |
347 | goto end; | |
33bceaf8 JG |
348 | case OPT_PLUGIN_PATH: |
349 | opt_plugin_path = (char *) poptGetOptArg(pc); | |
350 | if (!opt_plugin_path) { | |
351 | ret = -EINVAL; | |
352 | goto end; | |
353 | } ; | |
354 | break; | |
34ac0e6c MD |
355 | case OPT_VERBOSE: |
356 | babeltrace_verbose = 1; | |
357 | break; | |
cba1661c MD |
358 | case OPT_NAMES: |
359 | if (get_names_args(&pc)) { | |
360 | ret = -EINVAL; | |
361 | goto end; | |
362 | } | |
363 | break; | |
359d7456 MD |
364 | case OPT_FIELDS: |
365 | if (get_fields_args(&pc)) { | |
366 | ret = -EINVAL; | |
367 | goto end; | |
368 | } | |
369 | break; | |
34ac0e6c MD |
370 | case OPT_DEBUG: |
371 | babeltrace_debug = 1; | |
372 | break; | |
8d8ed9af | 373 | case OPT_NO_DELTA: |
359d7456 | 374 | opt_delta_field = 0; |
8d8ed9af | 375 | break; |
03798a93 JD |
376 | case OPT_CLOCK_CYCLES: |
377 | opt_clock_cycles = 1; | |
11ac6674 MD |
378 | break; |
379 | case OPT_CLOCK_OFFSET: | |
380 | { | |
9f2c779c | 381 | char *str; |
34224260 | 382 | char *endptr; |
11ac6674 | 383 | |
9f2c779c | 384 | str = (char *) poptGetOptArg(pc); |
11ac6674 MD |
385 | if (!str) { |
386 | fprintf(stderr, "[error] Missing --clock-offset argument\n"); | |
387 | ret = -EINVAL; | |
388 | goto end; | |
389 | } | |
390 | errno = 0; | |
61cf588b | 391 | opt_clock_offset = strtoll(str, &endptr, 0); |
11ac6674 MD |
392 | if (*endptr != '\0' || str == endptr || errno != 0) { |
393 | fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str); | |
394 | ret = -EINVAL; | |
9f2c779c | 395 | free(str); |
11ac6674 MD |
396 | goto end; |
397 | } | |
9f2c779c | 398 | free(str); |
11ac6674 MD |
399 | break; |
400 | } | |
401 | case OPT_CLOCK_SECONDS: | |
402 | opt_clock_seconds = 1; | |
403 | break; | |
65923160 IJ |
404 | case OPT_CLOCK_OFFSET_NS: |
405 | { | |
406 | char *str; | |
407 | char *endptr; | |
408 | ||
409 | str = (char *) poptGetOptArg(pc); | |
410 | if (!str) { | |
411 | fprintf(stderr, "[error] Missing --clock-offset-ns argument\n"); | |
412 | ret = -EINVAL; | |
413 | goto end; | |
414 | } | |
415 | errno = 0; | |
61cf588b | 416 | opt_clock_offset_ns = strtoll(str, &endptr, 0); |
65923160 IJ |
417 | if (*endptr != '\0' || str == endptr || errno != 0) { |
418 | fprintf(stderr, "[error] Incorrect --clock-offset-ns argument: %s\n", str); | |
419 | ret = -EINVAL; | |
420 | free(str); | |
421 | goto end; | |
422 | } | |
423 | free(str); | |
424 | break; | |
425 | } | |
426 | ||
11ac6674 MD |
427 | case OPT_CLOCK_DATE: |
428 | opt_clock_date = 1; | |
429 | break; | |
430 | case OPT_CLOCK_GMT: | |
431 | opt_clock_gmt = 1; | |
432 | break; | |
82ace6d6 MD |
433 | case OPT_CLOCK_FORCE_CORRELATE: |
434 | opt_clock_force_correlate = 1; | |
435 | break; | |
7da9b2f3 JD |
436 | case OPT_STREAM_INTERSECTION: |
437 | opt_stream_intersection = 1; | |
438 | break; | |
05984e0c JG |
439 | case OPT_DEBUG_INFO_DIR: |
440 | opt_debug_info_dir = (char *) poptGetOptArg(pc); | |
441 | if (!opt_debug_info_dir) { | |
c40a57e5 AB |
442 | ret = -EINVAL; |
443 | goto end; | |
444 | } | |
445 | break; | |
ff9ce920 JG |
446 | case OPT_DEBUG_INFO_FULL_PATH: |
447 | opt_debug_info_full_path = 1; | |
448 | break; | |
5cde0dc1 AB |
449 | case OPT_DEBUG_INFO_TARGET_PREFIX: |
450 | opt_debug_info_target_prefix = (char *) poptGetOptArg(pc); | |
451 | if (!opt_debug_info_target_prefix) { | |
452 | ret = -EINVAL; | |
453 | goto end; | |
454 | } | |
455 | break; | |
34ac0e6c MD |
456 | default: |
457 | ret = -EINVAL; | |
458 | goto end; | |
459 | } | |
460 | } | |
461 | ||
4e85cbfd | 462 | do { |
41075e78 | 463 | ipath = poptGetArg(pc); |
4e85cbfd MD |
464 | if (ipath) |
465 | g_ptr_array_add(opt_input_paths, (gpointer) ipath); | |
466 | } while (ipath); | |
cba1661c | 467 | |
34ac0e6c MD |
468 | end: |
469 | if (pc) { | |
470 | poptFreeContext(pc); | |
471 | } | |
472 | return ret; | |
473 | } | |
474 | ||
a99343cf | 475 | static GPtrArray *traversed_paths = 0; |
afb48eae | 476 | |
a99343cf YB |
477 | /* |
478 | * traverse_trace_dir() is the callback function for File Tree Walk (nftw). | |
479 | * it receives the path of the current entry (file, dir, link..etc) with | |
480 | * a flag to indicate the type of the entry. | |
481 | * if the entry being visited is a directory and contains a metadata file, | |
482 | * then add the path to a global list to be processed later in | |
483 | * add_traces_recursive. | |
484 | */ | |
485 | static int traverse_trace_dir(const char *fpath, const struct stat *sb, | |
486 | int tflag, struct FTW *ftwbuf) | |
487 | { | |
488 | int dirfd, metafd; | |
489 | int closeret; | |
490 | ||
491 | if (tflag != FTW_D) | |
492 | return 0; | |
493 | ||
494 | dirfd = open(fpath, 0); | |
495 | if (dirfd < 0) { | |
496 | fprintf(stderr, "[error] [Context] Unable to open trace " | |
497 | "directory file descriptor.\n"); | |
498 | return 0; /* partial error */ | |
499 | } | |
500 | metafd = openat(dirfd, "metadata", O_RDONLY); | |
501 | if (metafd < 0) { | |
502 | closeret = close(dirfd); | |
503 | if (closeret < 0) { | |
504 | perror("close"); | |
505 | return -1; | |
506 | } | |
507 | /* No meta data, just return */ | |
508 | return 0; | |
509 | } else { | |
9544a40b MD |
510 | int err_close = 0; |
511 | ||
a99343cf YB |
512 | closeret = close(metafd); |
513 | if (closeret < 0) { | |
514 | perror("close"); | |
9544a40b | 515 | err_close = 1; |
a99343cf YB |
516 | } |
517 | closeret = close(dirfd); | |
518 | if (closeret < 0) { | |
519 | perror("close"); | |
9544a40b MD |
520 | err_close = 1; |
521 | } | |
522 | if (err_close) { | |
523 | return -1; | |
a99343cf YB |
524 | } |
525 | ||
526 | /* Add path to the global list */ | |
527 | if (traversed_paths == NULL) { | |
528 | fprintf(stderr, "[error] [Context] Invalid open path array.\n"); | |
529 | return -1; | |
530 | } | |
531 | g_ptr_array_add(traversed_paths, g_string_new(fpath)); | |
532 | } | |
533 | ||
534 | return 0; | |
535 | } | |
d0d82191 | 536 | |
20d24215 YB |
537 | /* |
538 | * bt_context_add_traces_recursive: Open a trace recursively | |
539 | * | |
540 | * Find each trace present in the subdirectory starting from the given | |
613f532b MD |
541 | * path, and add them to the context. The packet_seek parameter can be |
542 | * NULL: this specify to use the default format packet_seek. | |
20d24215 | 543 | * |
b945d4a5 | 544 | * Return: 0 on success, < 0 on failure, > 0 on partial failure. |
20d24215 | 545 | * Unable to open toplevel: failure. |
b945d4a5 MD |
546 | * Unable to open some subdirectory or file: warn and continue (partial |
547 | * failure); | |
20d24215 YB |
548 | */ |
549 | int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path, | |
613f532b | 550 | const char *format_str, |
1cf393f6 | 551 | void (*packet_seek)(struct bt_stream_pos *pos, |
613f532b | 552 | size_t offset, int whence)) |
20d24215 | 553 | { |
fd94f11c | 554 | int ret = 0, trace_ids = 0; |
20d24215 | 555 | |
34d99418 JD |
556 | if ((strncmp(path, NET4_URL_PREFIX, sizeof(NET4_URL_PREFIX) - 1)) == 0 || |
557 | (strncmp(path, NET6_URL_PREFIX, sizeof(NET6_URL_PREFIX) - 1)) == 0 || | |
558 | (strncmp(path, NET_URL_PREFIX, sizeof(NET_URL_PREFIX) - 1)) == 0) { | |
559 | ret = bt_context_add_trace(ctx, | |
560 | path, format_str, packet_seek, NULL, NULL); | |
561 | if (ret < 0) { | |
562 | fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" " | |
563 | "for reading.\n", path); | |
34d99418 JD |
564 | } |
565 | return ret; | |
566 | } | |
a99343cf | 567 | /* Should lock traversed_paths mutex here if used in multithread */ |
20d24215 | 568 | |
a99343cf | 569 | traversed_paths = g_ptr_array_new(); |
a99343cf YB |
570 | ret = nftw(path, traverse_trace_dir, 10, 0); |
571 | ||
572 | /* Process the array if ntfw did not return a fatal error */ | |
573 | if (ret >= 0) { | |
08c82b90 MD |
574 | int i; |
575 | ||
a99343cf YB |
576 | for (i = 0; i < traversed_paths->len; i++) { |
577 | GString *trace_path = g_ptr_array_index(traversed_paths, | |
578 | i); | |
579 | int trace_id = bt_context_add_trace(ctx, | |
580 | trace_path->str, | |
581 | format_str, | |
582 | packet_seek, | |
583 | NULL, | |
584 | NULL); | |
20d24215 | 585 | if (trace_id < 0) { |
7f89ddce | 586 | fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" from %s " |
a99343cf | 587 | "for reading.\n", trace_path->str, path); |
ca718275 | 588 | /* Allow to skip erroneous traces. */ |
b945d4a5 | 589 | ret = 1; /* partial error */ |
a99343cf | 590 | } else { |
fd94f11c | 591 | trace_ids++; |
20d24215 | 592 | } |
a99343cf | 593 | g_string_free(trace_path, TRUE); |
20d24215 YB |
594 | } |
595 | } | |
d0d82191 | 596 | |
a99343cf YB |
597 | g_ptr_array_free(traversed_paths, TRUE); |
598 | traversed_paths = NULL; | |
599 | ||
600 | /* Should unlock traversed paths mutex here if used in multithread */ | |
20d24215 | 601 | |
27cd3890 MD |
602 | /* |
603 | * Return an error if no trace can be opened. | |
604 | */ | |
fd94f11c | 605 | if (trace_ids == 0) { |
27cd3890 | 606 | fprintf(stderr, "[error] Cannot open any trace for reading.\n\n"); |
b945d4a5 | 607 | ret = -ENOENT; /* failure */ |
27cd3890 | 608 | } |
20d24215 YB |
609 | return ret; |
610 | } | |
a44bc4c9 | 611 | |
7237592a MD |
612 | static |
613 | int trace_pre_handler(struct bt_trace_descriptor *td_write, | |
614 | struct bt_context *ctx) | |
615 | { | |
616 | struct ctf_text_stream_pos *sout; | |
617 | struct trace_collection *tc; | |
618 | int ret, i; | |
619 | ||
620 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
621 | trace_descriptor); | |
622 | ||
623 | if (!sout->parent.pre_trace_cb) | |
624 | return 0; | |
625 | ||
626 | tc = ctx->tc; | |
627 | for (i = 0; i < tc->array->len; i++) { | |
628 | struct bt_trace_descriptor *td = | |
629 | g_ptr_array_index(tc->array, i); | |
630 | ||
631 | ret = sout->parent.pre_trace_cb(&sout->parent, td); | |
632 | if (ret) { | |
633 | fprintf(stderr, "[error] Writing to trace pre handler failed.\n"); | |
634 | goto end; | |
635 | } | |
636 | } | |
637 | ret = 0; | |
638 | end: | |
639 | return ret; | |
640 | } | |
641 | ||
642 | static | |
643 | int trace_post_handler(struct bt_trace_descriptor *td_write, | |
644 | struct bt_context *ctx) | |
645 | { | |
646 | struct ctf_text_stream_pos *sout; | |
647 | struct trace_collection *tc; | |
648 | int ret, i; | |
649 | ||
650 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
651 | trace_descriptor); | |
652 | ||
653 | if (!sout->parent.post_trace_cb) | |
654 | return 0; | |
655 | ||
656 | tc = ctx->tc; | |
657 | for (i = 0; i < tc->array->len; i++) { | |
658 | struct bt_trace_descriptor *td = | |
659 | g_ptr_array_index(tc->array, i); | |
660 | ||
661 | ret = sout->parent.post_trace_cb(&sout->parent, td); | |
662 | if (ret) { | |
663 | fprintf(stderr, "[error] Writing to trace post handler failed.\n"); | |
664 | goto end; | |
665 | } | |
666 | } | |
667 | ret = 0; | |
668 | end: | |
669 | return ret; | |
670 | } | |
671 | ||
672 | static | |
1b8455b7 | 673 | int convert_trace(struct bt_trace_descriptor *td_write, |
95d36295 JD |
674 | struct bt_context *ctx) |
675 | { | |
e4195791 | 676 | struct bt_ctf_iter *iter; |
95d36295 | 677 | struct ctf_text_stream_pos *sout; |
9981b1c2 | 678 | struct bt_iter_pos *begin_pos = NULL, *end_pos = NULL; |
c50d2a7a | 679 | struct bt_ctf_event *ctf_event; |
95d36295 JD |
680 | int ret; |
681 | ||
682 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
683 | trace_descriptor); | |
684 | ||
7da9b2f3 | 685 | if (!sout->parent.event_cb) { |
7237592a | 686 | return 0; |
7da9b2f3 | 687 | } |
7237592a | 688 | |
7da9b2f3 | 689 | if (opt_stream_intersection) { |
82008834 | 690 | iter = bt_ctf_iter_create_intersect(ctx, &begin_pos, &end_pos); |
7da9b2f3 | 691 | } else { |
9981b1c2 JG |
692 | begin_pos = bt_iter_create_time_pos(NULL, 0); |
693 | begin_pos->type = BT_SEEK_BEGIN; | |
694 | iter = bt_ctf_iter_create(ctx, begin_pos, NULL); | |
7da9b2f3 | 695 | } |
95d36295 JD |
696 | if (!iter) { |
697 | ret = -1; | |
698 | goto error_iter; | |
699 | } | |
e4195791 | 700 | while ((ctf_event = bt_ctf_iter_read_event(iter))) { |
c50d2a7a | 701 | ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream); |
95d36295 | 702 | if (ret) { |
3394d22e | 703 | fprintf(stderr, "[error] Writing event failed.\n"); |
95d36295 JD |
704 | goto end; |
705 | } | |
e4195791 | 706 | ret = bt_iter_next(bt_ctf_get_iter(iter)); |
7da9b2f3 | 707 | if (ret < 0) { |
95d36295 | 708 | goto end; |
7da9b2f3 | 709 | } |
95d36295 JD |
710 | } |
711 | ret = 0; | |
712 | ||
713 | end: | |
e4195791 | 714 | bt_ctf_iter_destroy(iter); |
95d36295 | 715 | error_iter: |
9981b1c2 JG |
716 | bt_iter_free_pos(begin_pos); |
717 | bt_iter_free_pos(end_pos); | |
95d36295 JD |
718 | return ret; |
719 | } | |
720 | ||
7e3e3582 JG |
721 | void call_plugins_hooks(void) |
722 | { | |
723 | bt_dummy_hook(); | |
724 | bt_lttng_live_hook(); | |
725 | bt_ctf_hook(); | |
726 | bt_ctf_text_hook(); | |
727 | bt_ctf_metadata_hook(); | |
728 | } | |
729 | ||
bbefb8dd | 730 | int main(int argc, char **argv) |
34ac0e6c | 731 | { |
4e85cbfd | 732 | int ret, partial_error = 0, open_success = 0; |
37b99bdb | 733 | struct bt_format *fmt_write; |
1b8455b7 | 734 | struct bt_trace_descriptor *td_write; |
95d36295 | 735 | struct bt_context *ctx; |
4e85cbfd MD |
736 | int i; |
737 | ||
7e3e3582 JG |
738 | call_plugins_hooks(); |
739 | ||
4e85cbfd | 740 | opt_input_paths = g_ptr_array_new(); |
34ac0e6c MD |
741 | |
742 | ret = parse_options(argc, argv); | |
743 | if (ret < 0) { | |
3394d22e MD |
744 | fprintf(stderr, "Error parsing options.\n\n"); |
745 | usage(stderr); | |
4e85cbfd | 746 | g_ptr_array_free(opt_input_paths, TRUE); |
34ac0e6c MD |
747 | exit(EXIT_FAILURE); |
748 | } else if (ret > 0) { | |
4e85cbfd | 749 | g_ptr_array_free(opt_input_paths, TRUE); |
34ac0e6c MD |
750 | exit(EXIT_SUCCESS); |
751 | } | |
752 | printf_verbose("Verbose mode active.\n"); | |
753 | printf_debug("Debug mode active.\n"); | |
754 | ||
33bceaf8 JG |
755 | if (!opt_plugin_path) { |
756 | fprintf(stderr, "No plugin path specified, aborting...\n"); | |
757 | ret = -1; | |
758 | goto end; | |
759 | } | |
760 | printf_verbose("Looking-up plugins at %s", | |
761 | opt_plugin_path ? opt_plugin_path : "Invalid"); | |
762 | component_factory = bt_component_factory_create(); | |
763 | if (!component_factory) { | |
764 | fprintf(stderr, "Failed to create component factory.\n"); | |
765 | ret = -1; | |
766 | goto end; | |
767 | } | |
768 | ||
769 | ret = bt_component_factory_load(component_factory, opt_plugin_path); | |
770 | if (ret) { | |
771 | fprintf(stderr, "Failed to load plugins.\n"); | |
772 | goto end; | |
773 | } | |
774 | ||
775 | if (opt_input_paths->len == 0) { | |
776 | ret = -1; | |
777 | goto end; | |
778 | } | |
779 | ||
9fe26ec7 | 780 | if (opt_input_format) |
bbefb8dd | 781 | strlower(opt_input_format); |
9fe26ec7 | 782 | if (opt_output_format) |
bbefb8dd MD |
783 | strlower(opt_output_format); |
784 | ||
4e85cbfd MD |
785 | printf_verbose("Converting from directory(ies):\n"); |
786 | for (i = 0; i < opt_input_paths->len; i++) { | |
787 | const char *ipath = g_ptr_array_index(opt_input_paths, i); | |
788 | printf_verbose(" %s\n", ipath); | |
789 | } | |
34ac0e6c | 790 | printf_verbose("Converting from format: %s\n", |
b61922b5 | 791 | opt_input_format ? : "ctf <default>"); |
c3e4d5dc | 792 | printf_verbose("Converting to target: %s\n", |
478b6389 | 793 | opt_output_path ? : "<stdout>"); |
34ac0e6c | 794 | printf_verbose("Converting to format: %s\n", |
b61922b5 | 795 | opt_output_format ? : "text <default>"); |
bbefb8dd | 796 | |
11e1d048 MD |
797 | if (!opt_input_format) { |
798 | opt_input_format = strdup("ctf"); | |
799 | if (!opt_input_format) { | |
800 | partial_error = 1; | |
801 | goto end; | |
802 | } | |
803 | } | |
804 | if (!opt_output_format) { | |
805 | opt_output_format = strdup("text"); | |
806 | if (!opt_output_format) { | |
807 | partial_error = 1; | |
808 | goto end; | |
809 | } | |
810 | } | |
bbefb8dd | 811 | fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format)); |
34d99418 | 812 | if (!fmt_read) { |
3394d22e | 813 | fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n", |
bbefb8dd | 814 | opt_input_format); |
11e1d048 MD |
815 | partial_error = 1; |
816 | goto end; | |
bbefb8dd | 817 | } |
bbefb8dd MD |
818 | fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format)); |
819 | if (!fmt_write) { | |
3394d22e | 820 | fprintf(stderr, "[error] format \"%s\" is not supported.\n\n", |
bbefb8dd | 821 | opt_output_format); |
11e1d048 MD |
822 | partial_error = 1; |
823 | goto end; | |
bbefb8dd MD |
824 | } |
825 | ||
6cba487f | 826 | ctx = bt_context_create(); |
d63c2d51 JD |
827 | if (!ctx) { |
828 | goto error_td_read; | |
829 | } | |
6cba487f | 830 | |
4e85cbfd MD |
831 | for (i = 0; i < opt_input_paths->len; i++) { |
832 | const char *ipath = g_ptr_array_index(opt_input_paths, i); | |
833 | ret = bt_context_add_traces_recursive(ctx, ipath, | |
834 | opt_input_format, NULL); | |
835 | if (ret < 0) { | |
836 | fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n", | |
837 | ipath); | |
838 | } else if (ret > 0) { | |
839 | fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n", | |
840 | ipath); | |
841 | open_success = 1; /* some traces were OK */ | |
842 | partial_error = 1; | |
843 | } else { | |
844 | open_success = 1; /* all traces were OK */ | |
845 | } | |
846 | } | |
847 | if (!open_success) { | |
848 | fprintf(stderr, "[error] none of the specified trace paths could be opened.\n\n"); | |
bbefb8dd MD |
849 | goto error_td_read; |
850 | } | |
6cba487f | 851 | |
5b80ddfb | 852 | td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL); |
bbefb8dd | 853 | if (!td_write) { |
3394d22e | 854 | fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n", |
b61922b5 | 855 | opt_output_path ? : "<none>"); |
bbefb8dd MD |
856 | goto error_td_write; |
857 | } | |
847bf71a | 858 | |
24d5af5b MD |
859 | /* |
860 | * Errors happened when opening traces, but we continue anyway. | |
861 | * sleep to let user see the stderr output before stdout. | |
862 | */ | |
4e85cbfd | 863 | if (partial_error) |
24d5af5b | 864 | sleep(PARTIAL_ERROR_SLEEP); |
24d5af5b | 865 | |
7237592a MD |
866 | ret = trace_pre_handler(td_write, ctx); |
867 | if (ret) { | |
868 | fprintf(stderr, "Error in trace pre handle.\n\n"); | |
869 | goto error_copy_trace; | |
870 | } | |
871 | ||
34d99418 JD |
872 | /* For now, we support only CTF iterators */ |
873 | if (fmt_read->name == g_quark_from_static_string("ctf")) { | |
874 | ret = convert_trace(td_write, ctx); | |
875 | if (ret) { | |
876 | fprintf(stderr, "Error printing trace.\n\n"); | |
877 | goto error_copy_trace; | |
878 | } | |
46322b33 | 879 | } |
847bf71a | 880 | |
7237592a MD |
881 | ret = trace_post_handler(td_write, ctx); |
882 | if (ret) { | |
883 | fprintf(stderr, "Error in trace post handle.\n\n"); | |
884 | goto error_copy_trace; | |
885 | } | |
886 | ||
bbefb8dd | 887 | fmt_write->close_trace(td_write); |
6cba487f MD |
888 | |
889 | bt_context_put(ctx); | |
afb48eae AA |
890 | printf_verbose("finished converting. Output written to:\n%s\n", |
891 | opt_output_path ? : "<stdout>"); | |
11e1d048 | 892 | goto end; |
34ac0e6c | 893 | |
bbefb8dd | 894 | /* Error handling */ |
46322b33 | 895 | error_copy_trace: |
bbefb8dd MD |
896 | fmt_write->close_trace(td_write); |
897 | error_td_write: | |
6cba487f | 898 | bt_context_put(ctx); |
bbefb8dd | 899 | error_td_read: |
11e1d048 MD |
900 | partial_error = 1; |
901 | ||
902 | /* teardown and exit */ | |
903 | end: | |
904 | free(opt_input_format); | |
905 | free(opt_output_format); | |
9fe26ec7 | 906 | free(opt_output_path); |
05984e0c | 907 | free(opt_debug_info_dir); |
5cde0dc1 | 908 | free(opt_debug_info_target_prefix); |
4e85cbfd | 909 | g_ptr_array_free(opt_input_paths, TRUE); |
11e1d048 MD |
910 | if (partial_error) |
911 | exit(EXIT_FAILURE); | |
912 | else | |
913 | exit(EXIT_SUCCESS); | |
4c8bfb7e | 914 | } |