cleanup, live textdump working
[lttngtop.git] / src / iostreamtop.c
CommitLineData
1fc22eb4 1/*
aa15ac1c 2 * Copyright (C) 2011-2012 Mathieu Bain <mathieu.bain@polymtl.ca>
1fc22eb4
JD
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 *
71bd7ce1
AM
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.
1fc22eb4
JD
16 */
17
b093de8a
MB
18#include <stdlib.h>
19#include <unistd.h>
20#include <sys/types.h>
21#include <sys/stat.h>
22#include <string.h>
1fc22eb4
JD
23#include <babeltrace/babeltrace.h>
24
25#include "lttngtoptypes.h"
26#include "common.h"
27#include "iostreamtop.h"
28
b093de8a
MB
29void add_file(struct processtop *proc, struct files *file, int fd)
30{
ceb3a221 31 struct files *tmp_file;
59288610 32 struct processtop *parent;
ceb3a221 33 int size;
32647247 34 int i;
ceb3a221
MB
35
36 size = proc->process_files_table->len;
59288610
MB
37 parent = proc->threadparent;
38 if (parent)
39 insert_file(parent, fd);
ceb3a221 40 if (size <= fd) {
32647247
JD
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 }
b093de8a
MB
45 g_ptr_array_add(proc->process_files_table, file);
46 } else {
ceb3a221
MB
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,
59288610 54 size+1);
ceb3a221
MB
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 }
b093de8a 62 }
59288610
MB
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 }
b093de8a
MB
72}
73
59288610
MB
74/*
75 * Edit the file
76 * Called by handled_statedump_filename
77 */
ceb3a221
MB
78void edit_file(struct processtop *proc, struct files *file, int fd)
79{
80 int size = proc->process_files_table->len;
81 struct files *tmpfile;
82
59288610
MB
83 if (fd >= size) {
84 add_file(proc, file, fd);
85 } else {
ceb3a221 86 tmpfile = g_ptr_array_index(proc->process_files_table, fd);
59288610
MB
87 if (tmpfile) {
88 tmpfile->name = strdup(file->name);
89 free(file);
90 } else
91 add_file(proc, file, fd);
ceb3a221
MB
92 }
93}
b093de8a
MB
94
95void insert_file(struct processtop *proc, int fd)
96{
97 struct files *tmp;
59288610
MB
98 struct files *tmp_parent;
99 struct processtop *parent;
b093de8a 100
59288610
MB
101 if (fd < 0)
102 return;
b093de8a
MB
103 if (fd >= proc->process_files_table->len) {
104 tmp = g_new0(struct files, 1);
105 tmp->name = "Unknown";
ceb3a221
MB
106 tmp->read = 0;
107 tmp->write = 0;
108 tmp->fd = fd;
59288610 109 tmp->flag = -1;
b093de8a
MB
110 add_file(proc, tmp, fd);
111 } else {
b093de8a
MB
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;
59288610 119 tmp->flag = -1;
b093de8a 120 add_file(proc, tmp, fd);
59288610
MB
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 }
b093de8a
MB
130 }
131 }
132}
133
134void close_file(struct processtop *proc, int fd)
ceb3a221
MB
135{
136 struct files *file;
137
ceb3a221 138 file = get_file(proc, fd);
e05a35a6 139 if (file != NULL) {
ceb3a221 140 file->flag = __NR_close;
e05a35a6
JD
141 lttngtop.nbfiles--;
142 }
143 lttngtop.nbclosedfiles++;
ceb3a221
MB
144}
145
146struct files *get_file(struct processtop *proc, int fd)
b093de8a
MB
147{
148 int len;
ceb3a221 149 struct files *tmp = NULL;
b093de8a
MB
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 */
ceb3a221
MB
158 if (fd < len && fd >= 0)
159 tmp = g_ptr_array_index(proc->process_files_table, fd);
b093de8a 160
b093de8a
MB
161 return tmp;
162}
163
164void 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}
1fc22eb4 178
ceb3a221
MB
179void 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
1fc22eb4 191int update_iostream_ret(struct lttngtop *ctx, int tid, char *comm,
d67167cd 192 unsigned long timestamp, uint64_t cpu_id, int ret)
1fc22eb4
JD
193{
194 struct processtop *tmp;
b093de8a 195 struct files *tmpfile;
1fc22eb4
JD
196 int err = 0;
197
198 tmp = get_proc(ctx, tid, comm, timestamp);
b093de8a
MB
199
200 if (tmp->syscall_info != NULL) {
201 if (tmp->syscall_info->type == __NR_read
202 && ret > 0) {
203 tmp->totalfileread += ret;
204 tmp->fileread += ret;
205 tmpfile = get_file(tmp, tmp->syscall_info->fd);
6f29d91e
JD
206 if (tmpfile)
207 tmpfile->read += ret;
b093de8a
MB
208 } else if (tmp->syscall_info->type == __NR_write
209 && ret > 0) {
210 tmp->totalfilewrite += ret;
211 tmp->filewrite += ret;
212 tmpfile = get_file(tmp, tmp->syscall_info->fd);
6f29d91e
JD
213 if (tmpfile)
214 tmpfile->write += ret;
b093de8a
MB
215 } else if (tmp->syscall_info->type == __NR_open
216 && ret > 0) {
ceb3a221
MB
217 tmpfile = tmp->files_history->file;
218 add_file(tmp, tmpfile, ret);
219 tmpfile->fd = ret;
b093de8a 220 } else {
1fc22eb4
JD
221 err = -1;
222 }
b093de8a
MB
223 g_free(tmp->syscall_info);
224 tmp->syscall_info = NULL;
225 }
1fc22eb4
JD
226 return err;
227}
228
d67167cd 229struct syscalls *create_syscall_info(unsigned int type, uint64_t cpu_id,
b093de8a
MB
230 unsigned int tid, int fd)
231{
232 struct syscalls *syscall_info;
233
234 syscall_info = g_new0(struct syscalls, 1);
235 syscall_info->type = type;
236 syscall_info->cpu_id = cpu_id;
237 syscall_info->tid = tid;
238 syscall_info->fd = fd;
239
240 return syscall_info;
241}
242
243struct file_history *create_file(struct file_history *history, char *file_name)
244{
245 struct files *new_file;
246 struct file_history *new_history;
247
248 new_file = g_new0(struct files, 1);
249 new_history = g_new0(struct file_history, 1);
250 new_file->name = strdup(file_name);
251 new_file->read = 0;
252 new_file->write = 0;
59288610 253 new_file->flag = -1;
b093de8a
MB
254 new_history->file = new_file;
255 new_history->next = history;
256
257 return new_history;
258}
259
4adc8274 260enum bt_cb_ret handle_exit_syscall(struct bt_ctf_event *call_data,
1fc22eb4
JD
261 void *private_data)
262{
2e0a1190 263 const struct bt_definition *scope;
1fc22eb4
JD
264 unsigned long timestamp;
265 char *comm;
266 uint64_t ret, tid;
d67167cd 267 uint64_t cpu_id;
1fc22eb4 268
c78f2cdc 269 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
270 if (timestamp == -1ULL)
271 goto error;
272
1dec520a
JD
273 comm = get_context_comm(call_data);
274 tid = get_context_tid(call_data);
1fc22eb4
JD
275
276 scope = bt_ctf_get_top_level_scope(call_data,
277 BT_EVENT_FIELDS);
278 ret = bt_ctf_get_int64(bt_ctf_get_field(call_data,
279 scope, "_ret"));
280 if (bt_ctf_field_get_error()) {
281 fprintf(stderr, "Missing ret context info\n");
282 goto error;
283 }
284
d67167cd 285 cpu_id = get_cpu_id(call_data);
1fc22eb4
JD
286
287 /*
b093de8a
MB
288 * if we encounter an exit_syscall and
289 * it is not for a syscall read or write
1fc22eb4
JD
290 * we just abort the execution of this callback
291 */
292 if ((update_iostream_ret(&lttngtop, tid, comm, timestamp, cpu_id, ret)) < 0)
293 return BT_CB_ERROR_CONTINUE;
294
295 return BT_CB_OK;
296
297error:
298 return BT_CB_ERROR_STOP;
299}
300
301
4adc8274 302enum bt_cb_ret handle_sys_write(struct bt_ctf_event *call_data,
1fc22eb4
JD
303 void *private_data)
304{
2e0a1190 305 const struct bt_definition *scope;
1fc22eb4 306 struct processtop *tmp;
1fc22eb4
JD
307 unsigned long timestamp;
308 uint64_t cpu_id;
928f18a6
MB
309 int64_t tid;
310 char *procname;
b093de8a 311 int fd;
1fc22eb4 312
c78f2cdc 313 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
314 if (timestamp == -1ULL)
315 goto error;
316
1dec520a 317 tid = get_context_tid(call_data);
d67167cd 318 cpu_id = get_cpu_id(call_data);
1fc22eb4 319
928f18a6 320 procname = get_context_comm(call_data);
59288610 321
b093de8a
MB
322 scope = bt_ctf_get_top_level_scope(call_data,
323 BT_EVENT_FIELDS);
324 fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data,
325 scope, "_fd"));
326 if (bt_ctf_field_get_error()) {
327 fprintf(stderr, "Missing fd context info\n");
328 goto error;
329 }
330
928f18a6 331 tmp = get_proc(&lttngtop, tid, procname, timestamp);
b093de8a
MB
332 tmp->syscall_info = create_syscall_info(__NR_write, cpu_id, tid, fd);
333
334 insert_file(tmp, fd);
1fc22eb4
JD
335
336 return BT_CB_OK;
337
338error:
339 return BT_CB_ERROR_STOP;
340}
341
4adc8274 342enum bt_cb_ret handle_sys_read(struct bt_ctf_event *call_data,
1fc22eb4
JD
343 void *private_data)
344{
345 struct processtop *tmp;
2e0a1190 346 const struct bt_definition *scope;
1fc22eb4
JD
347 unsigned long timestamp;
348 uint64_t cpu_id;
928f18a6
MB
349 int64_t tid;
350 char *procname;
b093de8a 351 int fd;
1fc22eb4 352
c78f2cdc 353 timestamp = bt_ctf_get_timestamp(call_data);
1fc22eb4
JD
354 if (timestamp == -1ULL)
355 goto error;
356
1dec520a 357 tid = get_context_tid(call_data);
d67167cd 358 cpu_id = get_cpu_id(call_data);
1fc22eb4 359
928f18a6 360 procname = get_context_comm(call_data);
59288610 361
b093de8a
MB
362 scope = bt_ctf_get_top_level_scope(call_data,
363 BT_EVENT_FIELDS);
364 fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data,
365 scope, "_fd"));
366 if (bt_ctf_field_get_error()) {
367 fprintf(stderr, "Missing fd context info\n");
368 goto error;
369 }
370
928f18a6 371 tmp = get_proc(&lttngtop, tid, procname, timestamp);
b093de8a
MB
372 tmp->syscall_info = create_syscall_info(__NR_read, cpu_id, tid, fd);
373
374 insert_file(tmp, fd);
375
376 return BT_CB_OK;
377
378error:
379 return BT_CB_ERROR_STOP;
380}
381
382
4adc8274 383enum bt_cb_ret handle_sys_open(struct bt_ctf_event *call_data,
b093de8a
MB
384 void *private_data)
385{
386
387 struct processtop *tmp;
2e0a1190 388 const struct bt_definition *scope;
b093de8a
MB
389 unsigned long timestamp;
390 uint64_t cpu_id;
928f18a6
MB
391 int64_t tid;
392 char *procname;
b093de8a
MB
393 char *file;
394
c78f2cdc 395 timestamp = bt_ctf_get_timestamp(call_data);
b093de8a
MB
396 if (timestamp == -1ULL)
397 goto error;
398
1dec520a 399 tid = get_context_tid(call_data);
d67167cd 400 cpu_id = get_cpu_id(call_data);
b093de8a 401
928f18a6 402 procname = get_context_comm(call_data);
59288610 403
b093de8a
MB
404 scope = bt_ctf_get_top_level_scope(call_data,
405 BT_EVENT_FIELDS);
406 file = bt_ctf_get_string(bt_ctf_get_field(call_data,
407 scope, "_filename"));
408 if (bt_ctf_field_get_error()) {
ceb3a221 409 fprintf(stderr, "Missing file name context info\n");
b093de8a
MB
410 goto error;
411 }
412
928f18a6 413 tmp = get_proc(&lttngtop, tid, procname, timestamp);
b093de8a
MB
414 tmp->syscall_info = create_syscall_info(__NR_open, cpu_id, tid, -1);
415
416 tmp->files_history = create_file(tmp->files_history, file);
1fc22eb4
JD
417
418 return BT_CB_OK;
419
420error:
421 return BT_CB_ERROR_STOP;
422}
423
b093de8a 424
4adc8274 425enum bt_cb_ret handle_sys_close(struct bt_ctf_event *call_data,
b093de8a
MB
426 void *private_data)
427{
2e0a1190 428 const struct bt_definition *scope;
b093de8a 429 struct processtop *tmp;
928f18a6
MB
430 unsigned long timestamp;
431 int64_t tid;
432 char *procname;
b093de8a
MB
433 int fd;
434
c78f2cdc 435 timestamp = bt_ctf_get_timestamp(call_data);
b093de8a
MB
436 if (timestamp == -1ULL)
437 goto error;
438
1dec520a 439 tid = get_context_tid(call_data);
b093de8a 440
928f18a6 441 procname = get_context_comm(call_data);
59288610 442
b093de8a
MB
443 scope = bt_ctf_get_top_level_scope(call_data,
444 BT_EVENT_FIELDS);
445 fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data,
446 scope, "_fd"));
447 if (bt_ctf_field_get_error()) {
448 fprintf(stderr, "Missing fd context info\n");
449 goto error;
450 }
451
928f18a6 452 tmp = get_proc(&lttngtop, tid, procname, timestamp);
ceb3a221 453
b093de8a
MB
454 close_file(tmp, fd);
455
456 return BT_CB_OK;
457
458error:
459 return BT_CB_ERROR_STOP;
460}
59288610 461
4adc8274 462enum bt_cb_ret handle_statedump_file_descriptor(struct bt_ctf_event *call_data,
ceb3a221
MB
463 void *private_data)
464{
2e0a1190 465 const struct bt_definition *scope;
59288610 466 struct processtop *parent;
ceb3a221
MB
467 struct files *file;
468 unsigned long timestamp;
59288610
MB
469 int64_t pid;
470 char *file_name;
ceb3a221
MB
471 int fd;
472
c78f2cdc 473 timestamp = bt_ctf_get_timestamp(call_data);
ceb3a221
MB
474 if (timestamp == -1ULL)
475 goto error;
476
ceb3a221
MB
477 scope = bt_ctf_get_top_level_scope(call_data,
478 BT_EVENT_FIELDS);
59288610
MB
479 pid = bt_ctf_get_int64(bt_ctf_get_field(call_data,
480 scope, "_pid"));
ceb3a221
MB
481 if (bt_ctf_field_get_error()) {
482 fprintf(stderr, "Missing tid context info\n");
483 goto error;
484 }
485
486 scope = bt_ctf_get_top_level_scope(call_data,
487 BT_EVENT_FIELDS);
59288610 488 fd = bt_ctf_get_int64(bt_ctf_get_field(call_data,
ceb3a221
MB
489 scope, "_fd"));
490 if (bt_ctf_field_get_error()) {
491 fprintf(stderr, "Missing fd context info\n");
492 goto error;
493 }
494
495 scope = bt_ctf_get_top_level_scope(call_data,
496 BT_EVENT_FIELDS);
497 file_name = bt_ctf_get_string(bt_ctf_get_field(call_data,
498 scope, "_filename"));
499 if (bt_ctf_field_get_error()) {
500 fprintf(stderr, "Missing file name context info\n");
501 goto error;
502 }
503
59288610
MB
504 parent = get_proc_pid(&lttngtop, pid, pid, timestamp);
505 parent->files_history = create_file(parent->files_history, file_name);
506 file = parent->files_history->file;
507 edit_file(parent, file, fd);
ceb3a221
MB
508
509 return BT_CB_OK;
510
511error:
512 return BT_CB_ERROR_STOP;
513}
This page took 0.049662 seconds and 4 git commands to generate.