Commit | Line | Data |
---|---|---|
1fc22eb4 JD |
1 | /* |
2 | * Copyright (C) 2011 Mathieu Bain <mathieu.bain@polymtl.ca> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License Version 2 as | |
6 | * published by the Free Software Foundation; | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
16 | * MA 02111-1307, USA. | |
17 | */ | |
18 | ||
b093de8a MB |
19 | #include <stdlib.h> |
20 | #include <unistd.h> | |
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <string.h> | |
1fc22eb4 JD |
24 | #include <babeltrace/babeltrace.h> |
25 | ||
26 | #include "lttngtoptypes.h" | |
27 | #include "common.h" | |
28 | #include "iostreamtop.h" | |
29 | ||
b093de8a MB |
30 | void add_file(struct processtop *proc, struct files *file, int fd) |
31 | { | |
32 | if (proc->process_files_table->len <= fd) { | |
33 | g_ptr_array_set_size(proc->process_files_table, fd); | |
34 | g_ptr_array_add(proc->process_files_table, file); | |
35 | } else { | |
36 | g_ptr_array_index(proc->process_files_table, fd) = file; | |
37 | } | |
38 | file->fd = fd; | |
39 | } | |
40 | ||
41 | ||
42 | void insert_file(struct processtop *proc, int fd) | |
43 | { | |
44 | struct files *tmp; | |
45 | ||
46 | if (fd >= proc->process_files_table->len) { | |
47 | tmp = g_new0(struct files, 1); | |
48 | tmp->name = "Unknown"; | |
49 | add_file(proc, tmp, fd); | |
50 | } else { | |
51 | ||
52 | tmp = g_ptr_array_index(proc->process_files_table, fd); | |
53 | if (tmp == NULL) { | |
54 | tmp = g_new0(struct files, 1); | |
55 | tmp->name = "Unknown"; | |
56 | tmp->read = 0; | |
57 | tmp->write = 0; | |
58 | tmp->fd = fd; | |
59 | add_file(proc, tmp, fd); | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
64 | void close_file(struct processtop *proc, int fd) | |
65 | { | |
66 | int len; | |
67 | ||
68 | len = proc->process_files_table->len; | |
69 | ||
70 | /* | |
71 | * It is possible that a file was open before taking the trace | |
72 | * and its fd could be greater than all of the others fd | |
73 | * used by the process | |
74 | */ | |
75 | if (fd < len) { | |
76 | g_ptr_array_remove_index_fast(proc->process_files_table, fd); | |
77 | g_ptr_array_set_size(proc->process_files_table, len + 1); | |
78 | } | |
79 | } | |
80 | ||
81 | struct files *get_file(struct processtop *proc, int fd) | |
82 | { | |
83 | struct files *tmp; | |
84 | tmp = g_ptr_array_index(proc->process_files_table, fd); | |
85 | return tmp; | |
86 | } | |
87 | ||
88 | void show_table(GPtrArray *tab) | |
89 | { | |
90 | int i; | |
91 | struct files *file; | |
92 | ||
93 | for (i = 0 ; i < tab->len; i++) { | |
94 | file = g_ptr_array_index(tab, i); | |
95 | if (file == NULL) | |
96 | fprintf(stderr, "NULL, "); | |
97 | else | |
98 | fprintf(stderr, "%s, ", file->name); | |
99 | } | |
100 | fprintf(stderr, "]\n\n"); | |
101 | } | |
1fc22eb4 JD |
102 | |
103 | int update_iostream_ret(struct lttngtop *ctx, int tid, char *comm, | |
d67167cd | 104 | unsigned long timestamp, uint64_t cpu_id, int ret) |
1fc22eb4 JD |
105 | { |
106 | struct processtop *tmp; | |
b093de8a | 107 | struct files *tmpfile; |
1fc22eb4 JD |
108 | int err = 0; |
109 | ||
110 | tmp = get_proc(ctx, tid, comm, timestamp); | |
b093de8a MB |
111 | |
112 | if (tmp->syscall_info != NULL) { | |
113 | if (tmp->syscall_info->type == __NR_read | |
114 | && ret > 0) { | |
115 | tmp->totalfileread += ret; | |
116 | tmp->fileread += ret; | |
117 | tmpfile = get_file(tmp, tmp->syscall_info->fd); | |
118 | tmpfile->read += ret; | |
119 | } else if (tmp->syscall_info->type == __NR_write | |
120 | && ret > 0) { | |
121 | tmp->totalfilewrite += ret; | |
122 | tmp->filewrite += ret; | |
123 | tmpfile = get_file(tmp, tmp->syscall_info->fd); | |
124 | tmpfile->write += ret; | |
125 | } else if (tmp->syscall_info->type == __NR_open | |
126 | && ret > 0) { | |
127 | add_file(tmp, tmp->files_history->file, ret); | |
128 | } else { | |
1fc22eb4 JD |
129 | err = -1; |
130 | } | |
b093de8a MB |
131 | g_free(tmp->syscall_info); |
132 | tmp->syscall_info = NULL; | |
133 | } | |
1fc22eb4 JD |
134 | return err; |
135 | } | |
136 | ||
d67167cd | 137 | struct syscalls *create_syscall_info(unsigned int type, uint64_t cpu_id, |
b093de8a MB |
138 | unsigned int tid, int fd) |
139 | { | |
140 | struct syscalls *syscall_info; | |
141 | ||
142 | syscall_info = g_new0(struct syscalls, 1); | |
143 | syscall_info->type = type; | |
144 | syscall_info->cpu_id = cpu_id; | |
145 | syscall_info->tid = tid; | |
146 | syscall_info->fd = fd; | |
147 | ||
148 | return syscall_info; | |
149 | } | |
150 | ||
151 | struct file_history *create_file(struct file_history *history, char *file_name) | |
152 | { | |
153 | struct files *new_file; | |
154 | struct file_history *new_history; | |
155 | ||
156 | new_file = g_new0(struct files, 1); | |
157 | new_history = g_new0(struct file_history, 1); | |
158 | new_file->name = strdup(file_name); | |
159 | new_file->read = 0; | |
160 | new_file->write = 0; | |
161 | new_history->file = new_file; | |
162 | new_history->next = history; | |
163 | ||
164 | return new_history; | |
165 | } | |
166 | ||
1fc22eb4 JD |
167 | enum bt_cb_ret handle_exit_syscall(struct bt_ctf_event *call_data, |
168 | void *private_data) | |
169 | { | |
170 | struct definition *scope; | |
171 | unsigned long timestamp; | |
172 | char *comm; | |
173 | uint64_t ret, tid; | |
d67167cd | 174 | uint64_t cpu_id; |
1fc22eb4 JD |
175 | |
176 | timestamp = bt_ctf_get_timestamp(call_data); | |
177 | if (timestamp == -1ULL) | |
178 | goto error; | |
179 | ||
180 | scope = bt_ctf_get_top_level_scope(call_data, | |
181 | BT_STREAM_EVENT_CONTEXT); | |
182 | comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, | |
183 | scope, "_procname")); | |
184 | if (bt_ctf_field_get_error()) { | |
185 | fprintf(stderr, "Missing procname context info\n"); | |
186 | goto error; | |
187 | } | |
188 | ||
189 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
190 | scope, "_tid")); | |
191 | if (bt_ctf_field_get_error()) { | |
192 | fprintf(stderr, "Missing tid context info\n"); | |
193 | goto error; | |
194 | } | |
195 | ||
196 | scope = bt_ctf_get_top_level_scope(call_data, | |
197 | BT_EVENT_FIELDS); | |
198 | ret = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
199 | scope, "_ret")); | |
200 | if (bt_ctf_field_get_error()) { | |
201 | fprintf(stderr, "Missing ret context info\n"); | |
202 | goto error; | |
203 | } | |
204 | ||
d67167cd | 205 | cpu_id = get_cpu_id(call_data); |
1fc22eb4 JD |
206 | |
207 | /* | |
b093de8a MB |
208 | * if we encounter an exit_syscall and |
209 | * it is not for a syscall read or write | |
1fc22eb4 JD |
210 | * we just abort the execution of this callback |
211 | */ | |
212 | if ((update_iostream_ret(<tngtop, tid, comm, timestamp, cpu_id, ret)) < 0) | |
213 | return BT_CB_ERROR_CONTINUE; | |
214 | ||
215 | return BT_CB_OK; | |
216 | ||
217 | error: | |
218 | return BT_CB_ERROR_STOP; | |
219 | } | |
220 | ||
221 | ||
222 | enum bt_cb_ret handle_sys_write(struct bt_ctf_event *call_data, | |
223 | void *private_data) | |
224 | { | |
225 | struct definition *scope; | |
226 | struct processtop *tmp; | |
1fc22eb4 JD |
227 | unsigned long timestamp; |
228 | uint64_t cpu_id; | |
229 | char *comm; | |
230 | int64_t tid; | |
b093de8a | 231 | int fd; |
1fc22eb4 JD |
232 | |
233 | timestamp = bt_ctf_get_timestamp(call_data); | |
234 | if (timestamp == -1ULL) | |
235 | goto error; | |
236 | ||
237 | scope = bt_ctf_get_top_level_scope(call_data, | |
238 | BT_STREAM_EVENT_CONTEXT); | |
239 | comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, | |
240 | scope, "_procname")); | |
241 | if (bt_ctf_field_get_error()) { | |
242 | fprintf(stderr, "Missing procname context info\n"); | |
243 | goto error; | |
244 | } | |
245 | ||
246 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
247 | scope, "_tid")); | |
248 | if (bt_ctf_field_get_error()) { | |
249 | fprintf(stderr, "Missing tid context info\n"); | |
250 | goto error; | |
251 | } | |
252 | ||
d67167cd | 253 | cpu_id = get_cpu_id(call_data); |
1fc22eb4 | 254 | |
b093de8a MB |
255 | scope = bt_ctf_get_top_level_scope(call_data, |
256 | BT_EVENT_FIELDS); | |
257 | fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data, | |
258 | scope, "_fd")); | |
259 | if (bt_ctf_field_get_error()) { | |
260 | fprintf(stderr, "Missing fd context info\n"); | |
261 | goto error; | |
262 | } | |
263 | ||
1fc22eb4 | 264 | tmp = get_proc(<tngtop, tid, comm, timestamp); |
b093de8a MB |
265 | tmp->syscall_info = create_syscall_info(__NR_write, cpu_id, tid, fd); |
266 | ||
267 | insert_file(tmp, fd); | |
1fc22eb4 JD |
268 | |
269 | return BT_CB_OK; | |
270 | ||
271 | error: | |
272 | return BT_CB_ERROR_STOP; | |
273 | } | |
274 | ||
275 | enum bt_cb_ret handle_sys_read(struct bt_ctf_event *call_data, | |
276 | void *private_data) | |
277 | { | |
278 | struct processtop *tmp; | |
279 | struct definition *scope; | |
1fc22eb4 JD |
280 | unsigned long timestamp; |
281 | uint64_t cpu_id; | |
282 | char *comm; | |
283 | int64_t tid; | |
b093de8a | 284 | int fd; |
1fc22eb4 JD |
285 | |
286 | timestamp = bt_ctf_get_timestamp(call_data); | |
287 | if (timestamp == -1ULL) | |
288 | goto error; | |
289 | ||
290 | scope = bt_ctf_get_top_level_scope(call_data, | |
291 | BT_STREAM_EVENT_CONTEXT); | |
292 | comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, | |
293 | scope, "_procname")); | |
294 | if (bt_ctf_field_get_error()) { | |
295 | fprintf(stderr, "Missing procname context info\n"); | |
296 | goto error; | |
297 | } | |
298 | ||
299 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
300 | scope, "_tid")); | |
301 | if (bt_ctf_field_get_error()) { | |
302 | fprintf(stderr, "Missing tid context info\n"); | |
303 | goto error; | |
304 | } | |
305 | ||
d67167cd | 306 | cpu_id = get_cpu_id(call_data); |
1fc22eb4 | 307 | |
b093de8a MB |
308 | scope = bt_ctf_get_top_level_scope(call_data, |
309 | BT_EVENT_FIELDS); | |
310 | fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data, | |
311 | scope, "_fd")); | |
312 | if (bt_ctf_field_get_error()) { | |
313 | fprintf(stderr, "Missing fd context info\n"); | |
314 | goto error; | |
315 | } | |
316 | ||
317 | tmp = get_proc(<tngtop, tid, comm, timestamp); | |
318 | tmp->syscall_info = create_syscall_info(__NR_read, cpu_id, tid, fd); | |
319 | ||
320 | insert_file(tmp, fd); | |
321 | ||
322 | return BT_CB_OK; | |
323 | ||
324 | error: | |
325 | return BT_CB_ERROR_STOP; | |
326 | } | |
327 | ||
328 | ||
329 | enum bt_cb_ret handle_sys_open(struct bt_ctf_event *call_data, | |
330 | void *private_data) | |
331 | { | |
332 | ||
333 | struct processtop *tmp; | |
334 | struct definition *scope; | |
335 | unsigned long timestamp; | |
336 | uint64_t cpu_id; | |
337 | char *comm; | |
338 | int64_t tid; | |
339 | char *file; | |
340 | ||
341 | timestamp = bt_ctf_get_timestamp(call_data); | |
342 | if (timestamp == -1ULL) | |
343 | goto error; | |
344 | ||
345 | scope = bt_ctf_get_top_level_scope(call_data, | |
346 | BT_STREAM_EVENT_CONTEXT); | |
347 | comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, | |
348 | scope, "_procname")); | |
349 | if (bt_ctf_field_get_error()) { | |
350 | fprintf(stderr, "Missing procname context info\n"); | |
351 | goto error; | |
352 | } | |
353 | ||
354 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
355 | scope, "_tid")); | |
356 | if (bt_ctf_field_get_error()) { | |
357 | fprintf(stderr, "Missing tid context info\n"); | |
358 | goto error; | |
359 | } | |
360 | ||
d67167cd | 361 | cpu_id = get_cpu_id(call_data); |
b093de8a MB |
362 | |
363 | scope = bt_ctf_get_top_level_scope(call_data, | |
364 | BT_EVENT_FIELDS); | |
365 | file = bt_ctf_get_string(bt_ctf_get_field(call_data, | |
366 | scope, "_filename")); | |
367 | if (bt_ctf_field_get_error()) { | |
368 | fprintf(stderr, "Missing fd context info\n"); | |
369 | goto error; | |
370 | } | |
371 | ||
1fc22eb4 | 372 | tmp = get_proc(<tngtop, tid, comm, timestamp); |
b093de8a MB |
373 | tmp->syscall_info = create_syscall_info(__NR_open, cpu_id, tid, -1); |
374 | ||
375 | tmp->files_history = create_file(tmp->files_history, file); | |
1fc22eb4 JD |
376 | |
377 | return BT_CB_OK; | |
378 | ||
379 | error: | |
380 | return BT_CB_ERROR_STOP; | |
381 | } | |
382 | ||
b093de8a MB |
383 | |
384 | enum bt_cb_ret handle_sys_close(struct bt_ctf_event *call_data, | |
385 | void *private_data) | |
386 | { | |
387 | struct definition *scope; | |
388 | unsigned long timestamp; | |
389 | int64_t tid; | |
390 | struct processtop *tmp; | |
391 | char *comm; | |
392 | int fd; | |
393 | ||
394 | timestamp = bt_ctf_get_timestamp(call_data); | |
395 | if (timestamp == -1ULL) | |
396 | goto error; | |
397 | ||
398 | scope = bt_ctf_get_top_level_scope(call_data, | |
399 | BT_STREAM_EVENT_CONTEXT); | |
400 | comm = bt_ctf_get_char_array(bt_ctf_get_field(call_data, | |
401 | scope, "_procname")); | |
402 | if (bt_ctf_field_get_error()) { | |
403 | fprintf(stderr, "Missing procname context info\n"); | |
404 | goto error; | |
405 | } | |
406 | ||
407 | tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, | |
408 | scope, "_tid")); | |
409 | if (bt_ctf_field_get_error()) { | |
410 | fprintf(stderr, "Missing tid context info\n"); | |
411 | goto error; | |
412 | } | |
413 | ||
414 | scope = bt_ctf_get_top_level_scope(call_data, | |
415 | BT_EVENT_FIELDS); | |
416 | fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data, | |
417 | scope, "_fd")); | |
418 | if (bt_ctf_field_get_error()) { | |
419 | fprintf(stderr, "Missing fd context info\n"); | |
420 | goto error; | |
421 | } | |
422 | ||
423 | tmp = get_proc(<tngtop, tid, comm, timestamp); | |
424 | close_file(tmp, fd); | |
425 | ||
426 | return BT_CB_OK; | |
427 | ||
428 | error: | |
429 | return BT_CB_ERROR_STOP; | |
430 | } |