c0479e1b31d441a4df4c9913ffece01278fe0051
6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
22 #include <babeltrace/babeltrace.h>
23 #include <babeltrace/context.h>
24 #include <babeltrace/trace-handle.h>
25 #include <babeltrace/trace-handle-internal.h>
26 #include <babeltrace/trace-collection.h>
27 #include <babeltrace/format.h>
31 #include <fcntl.h> /* For O_RDONLY */
33 /* TODO ybrosseau: should be hidden in the CTF format */
34 #include <babeltrace/ctf/types.h> /* for ctf_move_pos_slow */
38 struct bt_context
*bt_context_create(void)
40 struct bt_context
*ctx
;
42 ctx
= g_new0(struct bt_context
, 1);
44 /* Negative handle id are errors. */
45 ctx
->last_trace_handle_id
= 0;
47 /* Instanciate the trace handle container */
48 ctx
->trace_handles
= g_hash_table_new_full(g_direct_hash
,
50 (GDestroyNotify
) bt_trace_handle_destroy
);
52 ctx
->tc
= g_new0(struct trace_collection
, 1);
53 init_trace_collection(ctx
->tc
);
58 int bt_context_add_trace(struct bt_context
*ctx
, const char *path
,
59 const char *format_str
)
61 struct trace_descriptor
*td
;
63 struct bt_trace_handle
*handle
;
65 fmt
= bt_lookup_format(g_quark_from_string(format_str
));
66 td
= fmt
->open_trace(path
, O_RDONLY
,
67 ctf_move_pos_slow
, NULL
);
69 fprintf(stdout
, "[error] [Context] Cannot Open_trace of the format %s .\n\n",
74 /* Create an handle for the trace */
75 handle
= bt_trace_handle_create(ctx
);
77 fprintf(stdout
, "[error] [Context] Creating trace handle %s .\n\n",
83 strncpy(handle
->path
, path
, PATH_MAX
);
84 handle
->path
[PATH_MAX
- 1] = '\0';
86 /* Add new handle to container */
87 g_hash_table_insert(ctx
->trace_handles
,
88 (gpointer
) (unsigned long) handle
->id
,
90 trace_collection_add(ctx
->tc
, td
);
95 * Unable to open toplevel: failure.
96 * Unable to open some subdirectory or file: warn and continue;
98 int bt_context_add_traces(struct bt_context
*ctx
, const char *path
,
99 const char *format_str
)
104 char lpath
[PATH_MAX
];
105 char * const paths
[2] = { lpath
, NULL
};
109 * Need to copy path, because fts_open can change it.
110 * It is the pointer array, not the strings, that are constant.
112 strncpy(lpath
, path
, PATH_MAX
);
113 lpath
[PATH_MAX
- 1] = '\0';
115 tree
= fts_open(paths
, FTS_NOCHDIR
| FTS_LOGICAL
, 0);
117 fprintf(stdout
, "[error] Cannot traverse \"%s\" for reading.\n\n",
122 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
124 while ((node
= fts_read(tree
))) {
127 if (!(node
->fts_info
& FTS_D
))
130 dirfd
= open(node
->fts_accpath
, 0);
132 fprintf(stdout
, "[warning] unable to open trace "
133 "directory file descriptor.\n");
136 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
157 trace_id
= bt_context_add_trace(ctx
,
158 node
->fts_accpath
, format_str
);
160 fprintf(stdout
, "[warning] CTX opening trace \"%s\"from %s "
161 "for reading.\n\n", node
->fts_accpath
, path
);
164 g_array_append_val(trace_ids
, trace_id
);
168 g_array_free(trace_ids
, TRUE
);
175 void bt_context_remove_trace(struct bt_context
*ctx
, int handle_id
)
177 struct bt_trace_handle
*handle
;
179 handle
= g_hash_table_lookup(ctx
->trace_handles
,
180 (gpointer
) (unsigned long) handle_id
);
181 assert(handle
!= NULL
);
183 /* Remove from containers */
184 trace_collection_remove(ctx
->tc
, handle
->td
);
185 g_hash_table_remove(ctx
->trace_handles
,
186 (gpointer
) (unsigned long) handle_id
);
188 /* Close the trace */
189 handle
->format
->close_trace(handle
->td
);
191 /* Destory the handle */
192 bt_trace_handle_destroy(handle
);
196 void bt_context_destroy(struct bt_context
*ctx
)
198 finalize_trace_collection(ctx
->tc
);
200 /* Remote all traces. The g_hash_table_destroy will call
201 * bt_trace_handle_destroy on each elements.
203 g_hash_table_destroy(ctx
->trace_handles
);
205 /* ctx->tc should always be valid */
206 assert(ctx
->tc
!= NULL
);
211 void bt_context_get(struct bt_context
*ctx
)
216 void bt_context_put(struct bt_context
*ctx
)
219 if (ctx
->refcount
== 0)
220 bt_context_destroy(ctx
);
This page took 0.036227 seconds and 4 git commands to generate.