2 * Copyright (C) 2011-2012 Mathieu Bain <mathieu.bain@polymtl.ca>
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;
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.
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.
20 #include <sys/types.h>
23 #include <babeltrace/babeltrace.h>
25 #include "lttngtoptypes.h"
27 #include "iostreamtop.h"
29 void add_file(struct processtop
*proc
, struct files
*file
, int fd
)
31 struct files
*tmp_file
;
32 struct processtop
*parent
;
36 size
= proc
->process_files_table
->len
;
37 parent
= proc
->threadparent
;
39 insert_file(parent
, 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
);
45 g_ptr_array_add(proc
->process_files_table
, file
);
47 tmp_file
= g_ptr_array_index(proc
->process_files_table
, fd
);
49 g_ptr_array_index(proc
->process_files_table
, fd
) = file
;
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
,
55 g_ptr_array_index(proc
->process_files_table
,
57 g_ptr_array_index(proc
->process_files_table
,
60 tmp_file
->flag
= __NR_open
;
64 * The file may have be created in the parent
66 if (file
->flag
== -1) {
68 file
->flag
= __NR_open
;
70 lttngtop
.nbnewfiles
++;
76 * Called by handled_statedump_filename
78 void edit_file(struct processtop
*proc
, struct files
*file
, int fd
)
80 int size
= proc
->process_files_table
->len
;
81 struct files
*tmpfile
;
84 add_file(proc
, file
, fd
);
86 tmpfile
= g_ptr_array_index(proc
->process_files_table
, fd
);
88 tmpfile
->name
= strdup(file
->name
);
91 add_file(proc
, file
, fd
);
95 void insert_file(struct processtop
*proc
, int fd
)
98 struct files
*tmp_parent
;
99 struct processtop
*parent
;
103 if (fd
>= proc
->process_files_table
->len
) {
104 tmp
= g_new0(struct files
, 1);
105 tmp
->name
= "Unknown";
110 add_file(proc
, tmp
, fd
);
112 tmp
= g_ptr_array_index(proc
->process_files_table
, fd
);
114 tmp
= g_new0(struct files
, 1);
115 tmp
->name
= "Unknown";
120 add_file(proc
, tmp
, fd
);
122 parent
= proc
->threadparent
;
124 tmp_parent
= g_ptr_array_index(
125 parent
->process_files_table
, fd
);
127 (strcmp(tmp
->name
, tmp_parent
->name
)) != 0)
128 tmp
->name
= strdup(tmp_parent
->name
);
134 void close_file(struct processtop
*proc
, int fd
)
138 file
= get_file(proc
, fd
);
140 file
->flag
= __NR_close
;
143 lttngtop
.nbclosedfiles
++;
146 struct files
*get_file(struct processtop
*proc
, int fd
)
149 struct files
*tmp
= NULL
;
151 len
= proc
->process_files_table
->len
;
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
158 if (fd
< len
&& fd
>= 0)
159 tmp
= g_ptr_array_index(proc
->process_files_table
, fd
);
164 void show_table(GPtrArray
*tab
)
169 for (i
= 0 ; i
< tab
->len
; i
++) {
170 file
= g_ptr_array_index(tab
, i
);
172 fprintf(stderr
, "NULL, ");
174 fprintf(stderr
, "%s, ", file
->name
);
176 fprintf(stderr
, "]\n\n");
179 void show_history(struct file_history
*history
)
181 struct file_history
*tmp
= history
;
183 while (tmp
!= NULL
) {
184 fprintf(stderr
, "fd = %d, name = %s\n", tmp
->file
->fd
,
191 int update_iostream_ret(struct lttngtop
*ctx
, int tid
, char *comm
,
192 unsigned long timestamp
, uint64_t cpu_id
, int ret
)
194 struct processtop
*tmp
;
195 struct files
*tmpfile
;
198 tmp
= get_proc(ctx
, tid
, comm
, timestamp
);
204 if (tmp
->syscall_info
!= NULL
) {
205 if (tmp
->syscall_info
->type
== __NR_read
207 tmp
->totalfileread
+= ret
;
208 tmp
->fileread
+= ret
;
209 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
211 tmpfile
->read
+= ret
;
212 } else if (tmp
->syscall_info
->type
== __NR_write
214 tmp
->totalfilewrite
+= ret
;
215 tmp
->filewrite
+= ret
;
216 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
218 tmpfile
->write
+= ret
;
219 } else if (tmp
->syscall_info
->type
== __NR_open
221 tmpfile
= tmp
->files_history
->file
;
222 add_file(tmp
, tmpfile
, ret
);
227 g_free(tmp
->syscall_info
);
228 tmp
->syscall_info
= NULL
;
235 struct syscalls
*create_syscall_info(unsigned int type
, uint64_t cpu_id
,
236 unsigned int tid
, int fd
)
238 struct syscalls
*syscall_info
;
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
;
249 struct file_history
*create_file(struct file_history
*history
, char *file_name
)
251 struct files
*new_file
;
252 struct file_history
*new_history
;
254 new_file
= g_new0(struct files
, 1);
255 new_history
= g_new0(struct file_history
, 1);
256 new_file
->name
= strdup(file_name
);
260 new_history
->file
= new_file
;
261 new_history
->next
= history
;
266 enum bt_cb_ret
handle_exit_syscall(struct bt_ctf_event
*call_data
,
269 const struct bt_definition
*scope
;
270 unsigned long timestamp
;
275 timestamp
= bt_ctf_get_timestamp(call_data
);
276 if (timestamp
== -1ULL)
279 comm
= get_context_comm(call_data
);
280 tid
= get_context_tid(call_data
);
282 scope
= bt_ctf_get_top_level_scope(call_data
,
284 ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
286 if (bt_ctf_field_get_error()) {
287 fprintf(stderr
, "Missing ret context info\n");
291 cpu_id
= get_cpu_id(call_data
);
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
298 if ((update_iostream_ret(<tngtop
, tid
, comm
, timestamp
, cpu_id
, ret
)) < 0)
299 return BT_CB_ERROR_CONTINUE
;
304 return BT_CB_ERROR_STOP
;
308 enum bt_cb_ret
handle_sys_write(struct bt_ctf_event
*call_data
,
311 const struct bt_definition
*scope
;
312 struct processtop
*tmp
;
313 unsigned long timestamp
;
319 timestamp
= bt_ctf_get_timestamp(call_data
);
320 if (timestamp
== -1ULL)
323 tid
= get_context_tid(call_data
);
324 cpu_id
= get_cpu_id(call_data
);
326 procname
= get_context_comm(call_data
);
328 scope
= bt_ctf_get_top_level_scope(call_data
,
330 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
332 if (bt_ctf_field_get_error()) {
333 fprintf(stderr
, "Missing fd context info\n");
337 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
);
341 tmp
->syscall_info
= create_syscall_info(__NR_write
, cpu_id
, tid
, fd
);
343 insert_file(tmp
, fd
);
349 return BT_CB_ERROR_STOP
;
352 enum bt_cb_ret
handle_sys_read(struct bt_ctf_event
*call_data
,
355 struct processtop
*tmp
;
356 const struct bt_definition
*scope
;
357 unsigned long timestamp
;
363 timestamp
= bt_ctf_get_timestamp(call_data
);
364 if (timestamp
== -1ULL)
367 tid
= get_context_tid(call_data
);
368 cpu_id
= get_cpu_id(call_data
);
370 procname
= get_context_comm(call_data
);
372 scope
= bt_ctf_get_top_level_scope(call_data
,
374 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
376 if (bt_ctf_field_get_error()) {
377 fprintf(stderr
, "Missing fd context info\n");
381 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
);
385 tmp
->syscall_info
= create_syscall_info(__NR_read
, cpu_id
, tid
, fd
);
387 insert_file(tmp
, fd
);
393 return BT_CB_ERROR_STOP
;
397 enum bt_cb_ret
handle_sys_open(struct bt_ctf_event
*call_data
,
401 struct processtop
*tmp
;
402 const struct bt_definition
*scope
;
403 unsigned long timestamp
;
409 timestamp
= bt_ctf_get_timestamp(call_data
);
410 if (timestamp
== -1ULL)
413 tid
= get_context_tid(call_data
);
414 cpu_id
= get_cpu_id(call_data
);
416 procname
= get_context_comm(call_data
);
418 scope
= bt_ctf_get_top_level_scope(call_data
,
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");
427 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
);
431 tmp
->syscall_info
= create_syscall_info(__NR_open
, cpu_id
, tid
, -1);
433 tmp
->files_history
= create_file(tmp
->files_history
, file
);
439 return BT_CB_ERROR_STOP
;
443 enum bt_cb_ret
handle_sys_close(struct bt_ctf_event
*call_data
,
446 const struct bt_definition
*scope
;
447 struct processtop
*tmp
;
448 unsigned long timestamp
;
453 timestamp
= bt_ctf_get_timestamp(call_data
);
454 if (timestamp
== -1ULL)
457 tid
= get_context_tid(call_data
);
459 procname
= get_context_comm(call_data
);
461 scope
= bt_ctf_get_top_level_scope(call_data
,
463 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
465 if (bt_ctf_field_get_error()) {
466 fprintf(stderr
, "Missing fd context info\n");
470 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
);
480 return BT_CB_ERROR_STOP
;
483 enum bt_cb_ret
handle_statedump_file_descriptor(struct bt_ctf_event
*call_data
,
486 const struct bt_definition
*scope
;
487 struct processtop
*parent
;
489 unsigned long timestamp
;
494 timestamp
= bt_ctf_get_timestamp(call_data
);
495 if (timestamp
== -1ULL)
498 scope
= bt_ctf_get_top_level_scope(call_data
,
500 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
502 if (bt_ctf_field_get_error()) {
503 fprintf(stderr
, "Missing tid context info\n");
507 scope
= bt_ctf_get_top_level_scope(call_data
,
509 fd
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
511 if (bt_ctf_field_get_error()) {
512 fprintf(stderr
, "Missing fd context info\n");
516 scope
= bt_ctf_get_top_level_scope(call_data
,
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");
525 parent
= get_proc_pid(<tngtop
, pid
, pid
, timestamp
);
529 parent
->files_history
= create_file(parent
->files_history
, file_name
);
530 file
= parent
->files_history
->file
;
531 edit_file(parent
, file
, fd
);
537 return BT_CB_ERROR_STOP
;
This page took 0.038884 seconds and 4 git commands to generate.