-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathctf.c
266 lines (221 loc) · 5.83 KB
/
ctf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* LTTng to GTKwave trace conversion
*
* Authors:
* Ivan Djelic <[email protected]>
* Matthieu Castet <[email protected]>
*
* Copyright (C) 2013 Parrot S.A.
*/
#include "lttng2lxt.h"
#include <babeltrace/babeltrace.h>
#include <babeltrace/ctf/iterator.h>
#include <babeltrace/ctf/events.h>
#include <babeltrace/ctf/callbacks.h>
#include <ftw.h>
#include <fcntl.h>
static struct bt_context *ctx;
static uint32_t tids;
int get_arg(void *args, const char *name, struct arg_value *value)
{
int ret = 0;
struct bt_ctf_event *ctf_event = args;
const struct bt_definition *scope;
const struct bt_definition *def;
const struct bt_declaration *decl;
enum ctf_type_id type;
scope = bt_ctf_get_top_level_scope(ctf_event, BT_EVENT_FIELDS);
def = bt_ctf_get_field(ctf_event, scope, name);
if (!def)
return -1;
decl = bt_ctf_get_decl_from_def(def);
if (!decl)
return -1;
type = bt_ctf_field_type(decl);
switch (type) {
case CTF_TYPE_INTEGER:
if (bt_ctf_get_int_signedness(decl)) {
value->i64 = bt_ctf_get_int64(def);
value->type = ARG_I64;
} else {
value->u64 = bt_ctf_get_uint64(def);
value->type = ARG_U64;
}
break;
case CTF_TYPE_STRING:
value->s = bt_ctf_get_string(def);
assert(value->s);
value->type = ARG_STR;
break;
case CTF_TYPE_ARRAY:
value->s = bt_ctf_get_char_array(def);
if (!value->s) {
ret = -1;
break;
}
value->type = ARG_STR;
break;
case CTF_TYPE_STRUCT:
case CTF_TYPE_UNTAGGED_VARIANT:
case CTF_TYPE_VARIANT:
case CTF_TYPE_FLOAT:
case CTF_TYPE_ENUM:
case CTF_TYPE_SEQUENCE:
default:
DIAG("field '%s' has unsupported CTF type %d\n", name, type);
ret = -1;
break;
}
return ret;
}
void for_each_arg(void *args,
void (*pfn)(void *cookie,
const char *name,
const struct arg_value *value),
void *cookie)
{
int ret;
struct bt_ctf_event *ctf_event = args;
unsigned int count, i;
struct bt_definition const * const *list;
struct arg_value value;
const char *name;
const struct bt_definition *scope;
scope = bt_ctf_get_top_level_scope(ctf_event, BT_EVENT_FIELDS);
assert(scope);
ret = bt_ctf_get_field_list(ctf_event, scope, &list, &count);
assert(ret == 0);
for (i = 0; i < count; i++) {
name = bt_ctf_field_name(list[i]);
if (get_arg(args, name, &value) == 0)
(*pfn)(cookie, name, &value);
}
}
int64_t get_arg_i64(void *args, const char *name)
{
struct arg_value value = {0};
(void)get_arg(args, name, &value);
return value.i64;
}
uint64_t get_arg_u64(void *args, const char *name)
{
struct arg_value value = {0};
(void)get_arg(args, name, &value);
return value.u64;
}
const char *get_arg_str(void *args, const char *name)
{
struct arg_value value = {0};
(void)get_arg(args, name, &value);
return value.s;
}
static void process_one_event(struct bt_ctf_event *ctf_event, double clock,
const struct ltt_module *mod, const char *name,
int pass)
{
const struct bt_definition *scope;
const struct bt_definition *def;
int cpu_id;
scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_PACKET_CONTEXT);
assert(scope);
def = bt_ctf_get_field(ctf_event, scope, "cpu_id");
assert(def);
cpu_id = (int)bt_ctf_get_uint64(def);
if (cpu_id >= MAX_CPU) {
DIAG("dropping event with cpu_id = %d\n", cpu_id);
return;
}
if (pass == 2)
emit_clock(clock);
TDIAG("process events", clock, "name=%s cpu=%d pass=%d\n", name, cpu_id, pass);
mod->process(name, pass, clock, cpu_id, ctf_event);
}
static void process_events(struct bt_ctf_iter *iter, int pass, int rebase_clock)
{
int ret;
double clock;
const char *name;
struct bt_ctf_event *ctf_event;
const struct ltt_module *mod;
while ((ctf_event = bt_ctf_iter_read_event(iter))) {
name = bt_ctf_event_name(ctf_event);
mod = find_module_by_name(name);
if (mod) {
clock = (double)bt_ctf_get_timestamp(ctf_event)/
1000000000.0;
if (rebase_clock) {
static double clock_base = 0.0;
if (clock_base == 0.0)
clock_base = clock;
clock -= clock_base;
}
process_one_event(ctf_event, clock, mod, name, pass);
}
ret = bt_iter_next(bt_ctf_get_iter(iter));
if (ret < 0)
FATAL("error fetching event in trace '%s'\n", name);
}
}
static int traverse_trace_dir(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf)
{
int tid;
/* size of "fpath/metadata" + '0' */
size_t sz = sizeof(char) * (strlen(fpath) + strlen("metadata") + 2);
char *metadata = malloc(sz);
if (!metadata)
return -1;
snprintf(metadata, sz, "%s/%s", fpath, "metadata");
if (access(metadata, R_OK))
goto exit;
tid = bt_context_add_trace(ctx, fpath, "ctf",
NULL, NULL, NULL);
if (tid < 0)
FATAL("cannot open trace '%s' for reading\n", fpath);
if (tid > 31)
goto exit;
tids |= 1 << tid;
exit:
free(metadata);
return 0;
}
void scan_lttng_trace(const char *name, int rebase_clock)
{
struct bt_ctf_iter *iter;
struct bt_iter_pos begin_pos;
int ret, i;
/* XXX hack to display missed event */
extern int babeltrace_ctf_console_output;
babeltrace_ctf_console_output = 1;
setenv("TZ", "", 1);
ctx = bt_context_create();
assert(ctx);
ret = nftw(name, traverse_trace_dir, 10, 0);
if (ret < 0)
FATAL("cannot open trace '%s'\n", name);
begin_pos.type = BT_SEEK_BEGIN;
iter = bt_ctf_iter_create(ctx, &begin_pos, NULL);
if (!iter)
FATAL("cannot iterate on trace '%s'\n", name);
INFO("pass 1: initializing modules and converting addresses\n");
process_events(iter, 1, rebase_clock);
babeltrace_ctf_console_output = 0;
/* flush address symbol conversion pipe */
atag_flush();
symbol_flush();
INFO("pass 2: emitting LXT traces\n");
/* rewind */
ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &begin_pos);
assert(ret == 0);
process_events(iter, 2, rebase_clock);
bt_ctf_iter_destroy(iter);
i = 0;
while (tids) {
if (tids & (1 << i)) {
bt_context_remove_trace(ctx, i);
tids &= ~(1 << i);
i++;
}
}
bt_context_put(ctx);
}