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
,
195 struct processtop
*tmp
;
196 struct files
*tmpfile
;
199 tmp
= get_proc(ctx
, tid
, comm
, timestamp
, hostname
);
205 if (tmp
->syscall_info
!= NULL
) {
206 if (tmp
->syscall_info
->type
== __NR_read
208 tmp
->totalfileread
+= ret
;
209 tmp
->fileread
+= ret
;
210 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
212 tmpfile
->read
+= ret
;
213 } else if (tmp
->syscall_info
->type
== __NR_write
215 tmp
->totalfilewrite
+= ret
;
216 tmp
->filewrite
+= ret
;
217 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
219 tmpfile
->write
+= ret
;
220 } else if (tmp
->syscall_info
->type
== __NR_open
222 tmpfile
= tmp
->files_history
->file
;
223 add_file(tmp
, tmpfile
, ret
);
228 g_free(tmp
->syscall_info
);
229 tmp
->syscall_info
= NULL
;
236 struct syscalls
*create_syscall_info(unsigned int type
, uint64_t cpu_id
,
237 unsigned int tid
, int fd
)
239 struct syscalls
*syscall_info
;
241 syscall_info
= g_new0(struct syscalls
, 1);
242 syscall_info
->type
= type
;
243 syscall_info
->cpu_id
= cpu_id
;
244 syscall_info
->tid
= tid
;
245 syscall_info
->fd
= fd
;
250 struct file_history
*create_file(struct file_history
*history
, char *file_name
)
252 struct files
*new_file
;
253 struct file_history
*new_history
;
255 new_file
= g_new0(struct files
, 1);
256 new_history
= g_new0(struct file_history
, 1);
257 new_file
->name
= strdup(file_name
);
261 new_history
->file
= new_file
;
262 new_history
->next
= history
;
267 enum bt_cb_ret
handle_exit_syscall(struct bt_ctf_event
*call_data
,
270 const struct bt_definition
*scope
;
271 unsigned long timestamp
;
277 timestamp
= bt_ctf_get_timestamp(call_data
);
278 if (timestamp
== -1ULL)
281 comm
= get_context_comm(call_data
);
282 tid
= get_context_tid(call_data
);
284 scope
= bt_ctf_get_top_level_scope(call_data
,
286 ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
288 if (bt_ctf_field_get_error()) {
289 fprintf(stderr
, "Missing ret context info\n");
293 cpu_id
= get_cpu_id(call_data
);
294 hostname
= get_context_hostname(call_data
);
297 * if we encounter an exit_syscall and
298 * it is not for a syscall read or write
299 * we just abort the execution of this callback
301 if ((update_iostream_ret(<tngtop
, tid
, comm
, timestamp
, cpu_id
,
303 return BT_CB_ERROR_CONTINUE
;
308 return BT_CB_ERROR_STOP
;
312 enum bt_cb_ret
handle_sys_write(struct bt_ctf_event
*call_data
,
315 const struct bt_definition
*scope
;
316 struct processtop
*tmp
;
317 unsigned long timestamp
;
320 char *procname
, *hostname
;
323 timestamp
= bt_ctf_get_timestamp(call_data
);
324 if (timestamp
== -1ULL)
327 tid
= get_context_tid(call_data
);
328 cpu_id
= get_cpu_id(call_data
);
330 procname
= get_context_comm(call_data
);
331 hostname
= get_context_hostname(call_data
);
333 scope
= bt_ctf_get_top_level_scope(call_data
,
335 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
337 if (bt_ctf_field_get_error()) {
338 fprintf(stderr
, "Missing fd context info\n");
342 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
, hostname
);
346 tmp
->syscall_info
= create_syscall_info(__NR_write
, cpu_id
, tid
, fd
);
348 insert_file(tmp
, fd
);
354 return BT_CB_ERROR_STOP
;
357 enum bt_cb_ret
handle_sys_read(struct bt_ctf_event
*call_data
,
360 struct processtop
*tmp
;
361 const struct bt_definition
*scope
;
362 unsigned long timestamp
;
369 timestamp
= bt_ctf_get_timestamp(call_data
);
370 if (timestamp
== -1ULL)
373 tid
= get_context_tid(call_data
);
374 cpu_id
= get_cpu_id(call_data
);
376 procname
= get_context_comm(call_data
);
377 hostname
= get_context_hostname(call_data
);
379 scope
= bt_ctf_get_top_level_scope(call_data
,
381 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
383 if (bt_ctf_field_get_error()) {
384 fprintf(stderr
, "Missing fd context info\n");
388 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
, hostname
);
392 tmp
->syscall_info
= create_syscall_info(__NR_read
, cpu_id
, tid
, fd
);
394 insert_file(tmp
, fd
);
400 return BT_CB_ERROR_STOP
;
404 enum bt_cb_ret
handle_sys_open(struct bt_ctf_event
*call_data
,
408 struct processtop
*tmp
;
409 const struct bt_definition
*scope
;
410 unsigned long timestamp
;
413 char *procname
, *hostname
;
416 timestamp
= bt_ctf_get_timestamp(call_data
);
417 if (timestamp
== -1ULL)
420 tid
= get_context_tid(call_data
);
421 cpu_id
= get_cpu_id(call_data
);
423 procname
= get_context_comm(call_data
);
424 hostname
= get_context_hostname(call_data
);
426 scope
= bt_ctf_get_top_level_scope(call_data
,
428 file
= bt_ctf_get_string(bt_ctf_get_field(call_data
,
429 scope
, "_filename"));
430 if (bt_ctf_field_get_error()) {
431 fprintf(stderr
, "Missing file name context info\n");
435 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
, hostname
);
439 tmp
->syscall_info
= create_syscall_info(__NR_open
, cpu_id
, tid
, -1);
441 tmp
->files_history
= create_file(tmp
->files_history
, file
);
447 return BT_CB_ERROR_STOP
;
451 enum bt_cb_ret
handle_sys_close(struct bt_ctf_event
*call_data
,
454 const struct bt_definition
*scope
;
455 struct processtop
*tmp
;
456 unsigned long timestamp
;
462 timestamp
= bt_ctf_get_timestamp(call_data
);
463 if (timestamp
== -1ULL)
466 tid
= get_context_tid(call_data
);
468 procname
= get_context_comm(call_data
);
469 hostname
= get_context_hostname(call_data
);
471 scope
= bt_ctf_get_top_level_scope(call_data
,
473 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
475 if (bt_ctf_field_get_error()) {
476 fprintf(stderr
, "Missing fd context info\n");
480 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
, hostname
);
490 return BT_CB_ERROR_STOP
;
493 enum bt_cb_ret
handle_statedump_file_descriptor(struct bt_ctf_event
*call_data
,
496 const struct bt_definition
*scope
;
497 struct processtop
*parent
;
499 unsigned long timestamp
;
501 char *file_name
, *hostname
;
504 timestamp
= bt_ctf_get_timestamp(call_data
);
505 if (timestamp
== -1ULL)
508 scope
= bt_ctf_get_top_level_scope(call_data
,
510 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
512 if (bt_ctf_field_get_error()) {
513 fprintf(stderr
, "Missing tid context info\n");
517 scope
= bt_ctf_get_top_level_scope(call_data
,
519 fd
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
521 if (bt_ctf_field_get_error()) {
522 fprintf(stderr
, "Missing fd context info\n");
526 scope
= bt_ctf_get_top_level_scope(call_data
,
528 file_name
= bt_ctf_get_string(bt_ctf_get_field(call_data
,
529 scope
, "_filename"));
530 if (bt_ctf_field_get_error()) {
531 fprintf(stderr
, "Missing file name context info\n");
534 hostname
= get_context_hostname(call_data
);
536 parent
= get_proc_pid(<tngtop
, pid
, pid
, timestamp
, hostname
);
540 parent
->files_history
= create_file(parent
->files_history
, file_name
);
541 file
= parent
->files_history
->file
;
542 edit_file(parent
, file
, fd
);
548 return BT_CB_ERROR_STOP
;
This page took 0.040926 seconds and 4 git commands to generate.