attach to a process in gui or textdump + fix get_proc returning NULL for filtering
[lttngtop.git] / src / iostreamtop.c
1 /*
2 * Copyright (C) 2011-2012 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 along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <string.h>
23 #include <babeltrace/babeltrace.h>
24
25 #include "lttngtoptypes.h"
26 #include "common.h"
27 #include "iostreamtop.h"
28
29 void add_file(struct processtop *proc, struct files *file, int fd)
30 {
31 struct files *tmp_file;
32 struct processtop *parent;
33 int size;
34 int i;
35
36 size = proc->process_files_table->len;
37 parent = proc->threadparent;
38 if (parent)
39 insert_file(parent, fd);
40 if (size <= fd) {
41 /* Add NULL file structures for undefined FDs */
42 for (i = size; i < fd; i++) {
43 g_ptr_array_add(proc->process_files_table, NULL);
44 }
45 g_ptr_array_add(proc->process_files_table, file);
46 } else {
47 tmp_file = g_ptr_array_index(proc->process_files_table, fd);
48 if (tmp_file == NULL)
49 g_ptr_array_index(proc->process_files_table, fd) = file;
50 else {
51 if (strcmp(tmp_file->name, file->name) != 0) {
52 size = proc->process_files_table->len;
53 g_ptr_array_set_size(proc->process_files_table,
54 size+1);
55 g_ptr_array_index(proc->process_files_table,
56 size) = tmp_file;
57 g_ptr_array_index(proc->process_files_table,
58 fd) = file;
59 } else
60 tmp_file->flag = __NR_open;
61 }
62 }
63 /*
64 * The file may have be created in the parent
65 */
66 if (file->flag == -1) {
67 file->fd = fd;
68 file->flag = __NR_open;
69 lttngtop.nbfiles++;
70 lttngtop.nbnewfiles++;
71 }
72 }
73
74 /*
75 * Edit the file
76 * Called by handled_statedump_filename
77 */
78 void edit_file(struct processtop *proc, struct files *file, int fd)
79 {
80 int size = proc->process_files_table->len;
81 struct files *tmpfile;
82
83 if (fd >= size) {
84 add_file(proc, file, fd);
85 } else {
86 tmpfile = g_ptr_array_index(proc->process_files_table, fd);
87 if (tmpfile) {
88 tmpfile->name = strdup(file->name);
89 free(file);
90 } else
91 add_file(proc, file, fd);
92 }
93 }
94
95 void insert_file(struct processtop *proc, int fd)
96 {
97 struct files *tmp;
98 struct files *tmp_parent;
99 struct processtop *parent;
100
101 if (fd < 0)
102 return;
103 if (fd >= proc->process_files_table->len) {
104 tmp = g_new0(struct files, 1);
105 tmp->name = "Unknown";
106 tmp->read = 0;
107 tmp->write = 0;
108 tmp->fd = fd;
109 tmp->flag = -1;
110 add_file(proc, tmp, fd);
111 } else {
112 tmp = g_ptr_array_index(proc->process_files_table, fd);
113 if (tmp == NULL) {
114 tmp = g_new0(struct files, 1);
115 tmp->name = "Unknown";
116 tmp->read = 0;
117 tmp->write = 0;
118 tmp->fd = fd;
119 tmp->flag = -1;
120 add_file(proc, tmp, fd);
121 } else {
122 parent = proc->threadparent;
123 if (parent) {
124 tmp_parent = g_ptr_array_index(
125 parent->process_files_table, fd);
126 if (tmp_parent &&
127 (strcmp(tmp->name, tmp_parent->name)) != 0)
128 tmp->name = strdup(tmp_parent->name);
129 }
130 }
131 }
132 }
133
134 void close_file(struct processtop *proc, int fd)
135 {
136 struct files *file;
137
138 file = get_file(proc, fd);
139 if (file != NULL) {
140 file->flag = __NR_close;
141 lttngtop.nbfiles--;
142 }
143 lttngtop.nbclosedfiles++;
144 }
145
146 struct files *get_file(struct processtop *proc, int fd)
147 {
148 int len;
149 struct files *tmp = NULL;
150
151 len = proc->process_files_table->len;
152
153 /*
154 * It is possible that a file was open before taking the trace
155 * and its fd could be greater than all of the others fd
156 * used by the process
157 */
158 if (fd < len && fd >= 0)
159 tmp = g_ptr_array_index(proc->process_files_table, fd);
160
161 return tmp;
162 }
163
164 void show_table(GPtrArray *tab)
165 {
166 int i;
167 struct files *file;
168
169 for (i = 0 ; i < tab->len; i++) {
170 file = g_ptr_array_index(tab, i);
171 if (file == NULL)
172 fprintf(stderr, "NULL, ");
173 else
174 fprintf(stderr, "%s, ", file->name);
175 }
176 fprintf(stderr, "]\n\n");
177 }
178
179 void show_history(struct file_history *history)
180 {
181 struct file_history *tmp = history;
182
183 while (tmp != NULL) {
184 fprintf(stderr, "fd = %d, name = %s\n", tmp->file->fd,
185 tmp->file->name);
186 tmp = tmp->next;
187 }
188
189 }
190
191 int update_iostream_ret(struct lttngtop *ctx, int tid, char *comm,
192 unsigned long timestamp, uint64_t cpu_id, int ret)
193 {
194 struct processtop *tmp;
195 struct files *tmpfile;
196 int err = 0;
197
198 tmp = get_proc(ctx, tid, comm, timestamp);
199
200 if (!tmp) {
201 err = -1;
202 goto end;
203 }
204 if (tmp->syscall_info != NULL) {
205 if (tmp->syscall_info->type == __NR_read
206 && ret > 0) {
207 tmp->totalfileread += ret;
208 tmp->fileread += ret;
209 tmpfile = get_file(tmp, tmp->syscall_info->fd);
210 if (tmpfile)
211 tmpfile->read += ret;
212 } else if (tmp->syscall_info->type == __NR_write
213 && ret > 0) {
214 tmp->totalfilewrite += ret;
215 tmp->filewrite += ret;
216 tmpfile = get_file(tmp, tmp->syscall_info->fd);
217 if (tmpfile)
218 tmpfile->write += ret;
219 } else if (tmp->syscall_info->type == __NR_open
220 && ret > 0) {
221 tmpfile = tmp->files_history->file;
222 add_file(tmp, tmpfile, ret);
223 tmpfile->fd = ret;
224 } else {
225 err = -1;
226 }
227 g_free(tmp->syscall_info);
228 tmp->syscall_info = NULL;
229 }
230
231 end:
232 return err;
233 }
234
235 struct syscalls *create_syscall_info(unsigned int type, uint64_t cpu_id,
236 unsigned int tid, int fd)
237 {
238 struct syscalls *syscall_info;
239
240 syscall_info = g_new0(struct syscalls, 1);
241 syscall_info->type = type;
242 syscall_info->cpu_id = cpu_id;
243 syscall_info->tid = tid;
244 syscall_info->fd = fd;
245
246 return syscall_info;
247 }
248
249 struct file_history *create_file(struct file_history *history, char *file_name)
250 {
251 struct files *new_file;
252 struct file_history *new_history;
253
254 new_file = g_new0(struct files, 1);
255 new_history = g_new0(struct file_history, 1);
256 new_file->name = strdup(file_name);
257 new_file->read = 0;
258 new_file->write = 0;
259 new_file->flag = -1;
260 new_history->file = new_file;
261 new_history->next = history;
262
263 return new_history;
264 }
265
266 enum bt_cb_ret handle_exit_syscall(struct bt_ctf_event *call_data,
267 void *private_data)
268 {
269 const struct bt_definition *scope;
270 unsigned long timestamp;
271 char *comm;
272 uint64_t ret, tid;
273 uint64_t cpu_id;
274
275 timestamp = bt_ctf_get_timestamp(call_data);
276 if (timestamp == -1ULL)
277 goto error;
278
279 comm = get_context_comm(call_data);
280 tid = get_context_tid(call_data);
281
282 scope = bt_ctf_get_top_level_scope(call_data,
283 BT_EVENT_FIELDS);
284 ret = bt_ctf_get_int64(bt_ctf_get_field(call_data,
285 scope, "_ret"));
286 if (bt_ctf_field_get_error()) {
287 fprintf(stderr, "Missing ret context info\n");
288 goto error;
289 }
290
291 cpu_id = get_cpu_id(call_data);
292
293 /*
294 * if we encounter an exit_syscall and
295 * it is not for a syscall read or write
296 * we just abort the execution of this callback
297 */
298 if ((update_iostream_ret(&lttngtop, tid, comm, timestamp, cpu_id, ret)) < 0)
299 return BT_CB_ERROR_CONTINUE;
300
301 return BT_CB_OK;
302
303 error:
304 return BT_CB_ERROR_STOP;
305 }
306
307
308 enum bt_cb_ret handle_sys_write(struct bt_ctf_event *call_data,
309 void *private_data)
310 {
311 const struct bt_definition *scope;
312 struct processtop *tmp;
313 unsigned long timestamp;
314 uint64_t cpu_id;
315 int64_t tid;
316 char *procname;
317 int fd;
318
319 timestamp = bt_ctf_get_timestamp(call_data);
320 if (timestamp == -1ULL)
321 goto error;
322
323 tid = get_context_tid(call_data);
324 cpu_id = get_cpu_id(call_data);
325
326 procname = get_context_comm(call_data);
327
328 scope = bt_ctf_get_top_level_scope(call_data,
329 BT_EVENT_FIELDS);
330 fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data,
331 scope, "_fd"));
332 if (bt_ctf_field_get_error()) {
333 fprintf(stderr, "Missing fd context info\n");
334 goto error;
335 }
336
337 tmp = get_proc(&lttngtop, tid, procname, timestamp);
338 if (!tmp)
339 goto end;
340
341 tmp->syscall_info = create_syscall_info(__NR_write, cpu_id, tid, fd);
342
343 insert_file(tmp, fd);
344
345 end:
346 return BT_CB_OK;
347
348 error:
349 return BT_CB_ERROR_STOP;
350 }
351
352 enum bt_cb_ret handle_sys_read(struct bt_ctf_event *call_data,
353 void *private_data)
354 {
355 struct processtop *tmp;
356 const struct bt_definition *scope;
357 unsigned long timestamp;
358 uint64_t cpu_id;
359 int64_t tid;
360 char *procname;
361 int fd;
362
363 timestamp = bt_ctf_get_timestamp(call_data);
364 if (timestamp == -1ULL)
365 goto error;
366
367 tid = get_context_tid(call_data);
368 cpu_id = get_cpu_id(call_data);
369
370 procname = get_context_comm(call_data);
371
372 scope = bt_ctf_get_top_level_scope(call_data,
373 BT_EVENT_FIELDS);
374 fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data,
375 scope, "_fd"));
376 if (bt_ctf_field_get_error()) {
377 fprintf(stderr, "Missing fd context info\n");
378 goto error;
379 }
380
381 tmp = get_proc(&lttngtop, tid, procname, timestamp);
382 if (!tmp)
383 goto end;
384
385 tmp->syscall_info = create_syscall_info(__NR_read, cpu_id, tid, fd);
386
387 insert_file(tmp, fd);
388
389 end:
390 return BT_CB_OK;
391
392 error:
393 return BT_CB_ERROR_STOP;
394 }
395
396
397 enum bt_cb_ret handle_sys_open(struct bt_ctf_event *call_data,
398 void *private_data)
399 {
400
401 struct processtop *tmp;
402 const struct bt_definition *scope;
403 unsigned long timestamp;
404 uint64_t cpu_id;
405 int64_t tid;
406 char *procname;
407 char *file;
408
409 timestamp = bt_ctf_get_timestamp(call_data);
410 if (timestamp == -1ULL)
411 goto error;
412
413 tid = get_context_tid(call_data);
414 cpu_id = get_cpu_id(call_data);
415
416 procname = get_context_comm(call_data);
417
418 scope = bt_ctf_get_top_level_scope(call_data,
419 BT_EVENT_FIELDS);
420 file = bt_ctf_get_string(bt_ctf_get_field(call_data,
421 scope, "_filename"));
422 if (bt_ctf_field_get_error()) {
423 fprintf(stderr, "Missing file name context info\n");
424 goto error;
425 }
426
427 tmp = get_proc(&lttngtop, tid, procname, timestamp);
428 if (!tmp)
429 goto end;
430
431 tmp->syscall_info = create_syscall_info(__NR_open, cpu_id, tid, -1);
432
433 tmp->files_history = create_file(tmp->files_history, file);
434
435 end:
436 return BT_CB_OK;
437
438 error:
439 return BT_CB_ERROR_STOP;
440 }
441
442
443 enum bt_cb_ret handle_sys_close(struct bt_ctf_event *call_data,
444 void *private_data)
445 {
446 const struct bt_definition *scope;
447 struct processtop *tmp;
448 unsigned long timestamp;
449 int64_t tid;
450 char *procname;
451 int fd;
452
453 timestamp = bt_ctf_get_timestamp(call_data);
454 if (timestamp == -1ULL)
455 goto error;
456
457 tid = get_context_tid(call_data);
458
459 procname = get_context_comm(call_data);
460
461 scope = bt_ctf_get_top_level_scope(call_data,
462 BT_EVENT_FIELDS);
463 fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data,
464 scope, "_fd"));
465 if (bt_ctf_field_get_error()) {
466 fprintf(stderr, "Missing fd context info\n");
467 goto error;
468 }
469
470 tmp = get_proc(&lttngtop, tid, procname, timestamp);
471 if (!tmp)
472 goto end;
473
474 close_file(tmp, fd);
475
476 end:
477 return BT_CB_OK;
478
479 error:
480 return BT_CB_ERROR_STOP;
481 }
482
483 enum bt_cb_ret handle_statedump_file_descriptor(struct bt_ctf_event *call_data,
484 void *private_data)
485 {
486 const struct bt_definition *scope;
487 struct processtop *parent;
488 struct files *file;
489 unsigned long timestamp;
490 int64_t pid;
491 char *file_name;
492 int fd;
493
494 timestamp = bt_ctf_get_timestamp(call_data);
495 if (timestamp == -1ULL)
496 goto error;
497
498 scope = bt_ctf_get_top_level_scope(call_data,
499 BT_EVENT_FIELDS);
500 pid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
501 scope, "_pid"));
502 if (bt_ctf_field_get_error()) {
503 fprintf(stderr, "Missing tid context info\n");
504 goto error;
505 }
506
507 scope = bt_ctf_get_top_level_scope(call_data,
508 BT_EVENT_FIELDS);
509 fd = bt_ctf_get_int64(bt_ctf_get_field(call_data,
510 scope, "_fd"));
511 if (bt_ctf_field_get_error()) {
512 fprintf(stderr, "Missing fd context info\n");
513 goto error;
514 }
515
516 scope = bt_ctf_get_top_level_scope(call_data,
517 BT_EVENT_FIELDS);
518 file_name = bt_ctf_get_string(bt_ctf_get_field(call_data,
519 scope, "_filename"));
520 if (bt_ctf_field_get_error()) {
521 fprintf(stderr, "Missing file name context info\n");
522 goto error;
523 }
524
525 parent = get_proc_pid(&lttngtop, pid, pid, timestamp);
526 if (!parent)
527 goto end;
528
529 parent->files_history = create_file(parent->files_history, file_name);
530 file = parent->files_history->file;
531 edit_file(parent, file, fd);
532
533 end:
534 return BT_CB_OK;
535
536 error:
537 return BT_CB_ERROR_STOP;
538 }
This page took 0.062631 seconds and 4 git commands to generate.