2 * Copyright (C) 2011 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
;
35 size
= proc
->process_files_table
->len
;
36 parent
= proc
->threadparent
;
38 insert_file(parent
, fd
);
40 g_ptr_array_set_size(proc
->process_files_table
, fd
);
41 g_ptr_array_add(proc
->process_files_table
, file
);
43 tmp_file
= g_ptr_array_index(proc
->process_files_table
, fd
);
45 g_ptr_array_index(proc
->process_files_table
, fd
) = file
;
47 if (strcmp(tmp_file
->name
, file
->name
) != 0) {
48 size
= proc
->process_files_table
->len
;
49 g_ptr_array_set_size(proc
->process_files_table
,
51 g_ptr_array_index(proc
->process_files_table
,
53 g_ptr_array_index(proc
->process_files_table
,
56 tmp_file
->flag
= __NR_open
;
60 * The file may have be created in the parent
62 if (file
->flag
== -1) {
64 file
->flag
= __NR_open
;
66 lttngtop
.nbnewfiles
++;
72 * Called by handled_statedump_filename
74 void edit_file(struct processtop
*proc
, struct files
*file
, int fd
)
76 int size
= proc
->process_files_table
->len
;
77 struct files
*tmpfile
;
80 add_file(proc
, file
, fd
);
82 tmpfile
= g_ptr_array_index(proc
->process_files_table
, fd
);
84 tmpfile
->name
= strdup(file
->name
);
87 add_file(proc
, file
, fd
);
91 void insert_file(struct processtop
*proc
, int fd
)
94 struct files
*tmp_parent
;
95 struct processtop
*parent
;
99 if (fd
>= proc
->process_files_table
->len
) {
100 tmp
= g_new0(struct files
, 1);
101 tmp
->name
= "Unknown";
106 add_file(proc
, tmp
, fd
);
108 tmp
= g_ptr_array_index(proc
->process_files_table
, fd
);
110 tmp
= g_new0(struct files
, 1);
111 tmp
->name
= "Unknown";
116 add_file(proc
, tmp
, fd
);
118 parent
= proc
->threadparent
;
120 tmp_parent
= g_ptr_array_index(
121 parent
->process_files_table
, fd
);
123 (strcmp(tmp
->name
, tmp_parent
->name
)) != 0)
124 tmp
->name
= strdup(tmp_parent
->name
);
130 void close_file(struct processtop
*proc
, int fd
)
134 file
= get_file(proc
, fd
);
136 file
->flag
= __NR_close
;
139 lttngtop
.nbclosedfiles
++;
142 struct files
*get_file(struct processtop
*proc
, int fd
)
145 struct files
*tmp
= NULL
;
147 len
= proc
->process_files_table
->len
;
150 * It is possible that a file was open before taking the trace
151 * and its fd could be greater than all of the others fd
152 * used by the process
154 if (fd
< len
&& fd
>= 0)
155 tmp
= g_ptr_array_index(proc
->process_files_table
, fd
);
160 void show_table(GPtrArray
*tab
)
165 for (i
= 0 ; i
< tab
->len
; i
++) {
166 file
= g_ptr_array_index(tab
, i
);
168 fprintf(stderr
, "NULL, ");
170 fprintf(stderr
, "%s, ", file
->name
);
172 fprintf(stderr
, "]\n\n");
175 void show_history(struct file_history
*history
)
177 struct file_history
*tmp
= history
;
179 while (tmp
!= NULL
) {
180 fprintf(stderr
, "fd = %d, name = %s\n", tmp
->file
->fd
,
187 int update_iostream_ret(struct lttngtop
*ctx
, int tid
, char *comm
,
188 unsigned long timestamp
, uint64_t cpu_id
, int ret
)
190 struct processtop
*tmp
;
191 struct files
*tmpfile
;
194 tmp
= get_proc(ctx
, tid
, comm
, timestamp
);
196 if (tmp
->syscall_info
!= NULL
) {
197 if (tmp
->syscall_info
->type
== __NR_read
199 tmp
->totalfileread
+= ret
;
200 tmp
->fileread
+= ret
;
201 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
202 tmpfile
->read
+= ret
;
203 } else if (tmp
->syscall_info
->type
== __NR_write
205 tmp
->totalfilewrite
+= ret
;
206 tmp
->filewrite
+= ret
;
207 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
208 tmpfile
->write
+= ret
;
209 } else if (tmp
->syscall_info
->type
== __NR_open
211 tmpfile
= tmp
->files_history
->file
;
212 add_file(tmp
, tmpfile
, ret
);
217 g_free(tmp
->syscall_info
);
218 tmp
->syscall_info
= NULL
;
223 struct syscalls
*create_syscall_info(unsigned int type
, uint64_t cpu_id
,
224 unsigned int tid
, int fd
)
226 struct syscalls
*syscall_info
;
228 syscall_info
= g_new0(struct syscalls
, 1);
229 syscall_info
->type
= type
;
230 syscall_info
->cpu_id
= cpu_id
;
231 syscall_info
->tid
= tid
;
232 syscall_info
->fd
= fd
;
237 struct file_history
*create_file(struct file_history
*history
, char *file_name
)
239 struct files
*new_file
;
240 struct file_history
*new_history
;
242 new_file
= g_new0(struct files
, 1);
243 new_history
= g_new0(struct file_history
, 1);
244 new_file
->name
= strdup(file_name
);
248 new_history
->file
= new_file
;
249 new_history
->next
= history
;
254 enum bt_cb_ret
handle_exit_syscall(struct bt_ctf_event
*call_data
,
257 const struct definition
*scope
;
258 unsigned long timestamp
;
263 timestamp
= bt_ctf_get_timestamp(call_data
);
264 if (timestamp
== -1ULL)
267 comm
= get_context_comm(call_data
);
268 tid
= get_context_tid(call_data
);
270 scope
= bt_ctf_get_top_level_scope(call_data
,
272 ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
274 if (bt_ctf_field_get_error()) {
275 fprintf(stderr
, "Missing ret context info\n");
279 cpu_id
= get_cpu_id(call_data
);
282 * if we encounter an exit_syscall and
283 * it is not for a syscall read or write
284 * we just abort the execution of this callback
286 if ((update_iostream_ret(<tngtop
, tid
, comm
, timestamp
, cpu_id
, ret
)) < 0)
287 return BT_CB_ERROR_CONTINUE
;
292 return BT_CB_ERROR_STOP
;
296 enum bt_cb_ret
handle_sys_write(struct bt_ctf_event
*call_data
,
299 const struct definition
*scope
;
300 struct processtop
*tmp
;
301 unsigned long timestamp
;
306 timestamp
= bt_ctf_get_timestamp(call_data
);
307 if (timestamp
== -1ULL)
310 tid
= get_context_tid(call_data
);
311 cpu_id
= get_cpu_id(call_data
);
313 pid
= get_context_pid(call_data
);
315 scope
= bt_ctf_get_top_level_scope(call_data
,
317 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
319 if (bt_ctf_field_get_error()) {
320 fprintf(stderr
, "Missing fd context info\n");
324 tmp
= get_proc_pid(<tngtop
, tid
, pid
, timestamp
);
325 tmp
->syscall_info
= create_syscall_info(__NR_write
, cpu_id
, tid
, fd
);
327 insert_file(tmp
, fd
);
332 return BT_CB_ERROR_STOP
;
335 enum bt_cb_ret
handle_sys_read(struct bt_ctf_event
*call_data
,
338 struct processtop
*tmp
;
339 const struct definition
*scope
;
340 unsigned long timestamp
;
345 timestamp
= bt_ctf_get_timestamp(call_data
);
346 if (timestamp
== -1ULL)
349 tid
= get_context_tid(call_data
);
350 cpu_id
= get_cpu_id(call_data
);
352 pid
= get_context_pid(call_data
);
354 scope
= bt_ctf_get_top_level_scope(call_data
,
356 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
358 if (bt_ctf_field_get_error()) {
359 fprintf(stderr
, "Missing fd context info\n");
363 tmp
= get_proc_pid(<tngtop
, tid
, pid
, timestamp
);
364 tmp
->syscall_info
= create_syscall_info(__NR_read
, cpu_id
, tid
, fd
);
366 insert_file(tmp
, fd
);
371 return BT_CB_ERROR_STOP
;
375 enum bt_cb_ret
handle_sys_open(struct bt_ctf_event
*call_data
,
379 struct processtop
*tmp
;
380 const struct definition
*scope
;
381 unsigned long timestamp
;
386 timestamp
= bt_ctf_get_timestamp(call_data
);
387 if (timestamp
== -1ULL)
390 tid
= get_context_tid(call_data
);
391 cpu_id
= get_cpu_id(call_data
);
393 pid
= get_context_pid(call_data
);
395 scope
= bt_ctf_get_top_level_scope(call_data
,
397 file
= bt_ctf_get_string(bt_ctf_get_field(call_data
,
398 scope
, "_filename"));
399 if (bt_ctf_field_get_error()) {
400 fprintf(stderr
, "Missing file name context info\n");
404 tmp
= get_proc_pid(<tngtop
, tid
, pid
, timestamp
);
405 tmp
->syscall_info
= create_syscall_info(__NR_open
, cpu_id
, tid
, -1);
407 tmp
->files_history
= create_file(tmp
->files_history
, file
);
412 return BT_CB_ERROR_STOP
;
416 enum bt_cb_ret
handle_sys_close(struct bt_ctf_event
*call_data
,
419 const struct definition
*scope
;
420 unsigned long timestamp
;
422 struct processtop
*tmp
;
425 timestamp
= bt_ctf_get_timestamp(call_data
);
426 if (timestamp
== -1ULL)
429 tid
= get_context_tid(call_data
);
431 pid
= get_context_pid(call_data
);
433 scope
= bt_ctf_get_top_level_scope(call_data
,
435 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
437 if (bt_ctf_field_get_error()) {
438 fprintf(stderr
, "Missing fd context info\n");
442 tmp
= get_proc_pid(<tngtop
, tid
, pid
, timestamp
);
449 return BT_CB_ERROR_STOP
;
452 enum bt_cb_ret
handle_statedump_file_descriptor(struct bt_ctf_event
*call_data
,
455 const struct definition
*scope
;
456 struct processtop
*parent
;
458 unsigned long timestamp
;
463 timestamp
= bt_ctf_get_timestamp(call_data
);
464 if (timestamp
== -1ULL)
467 scope
= bt_ctf_get_top_level_scope(call_data
,
469 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
471 if (bt_ctf_field_get_error()) {
472 fprintf(stderr
, "Missing tid context info\n");
476 scope
= bt_ctf_get_top_level_scope(call_data
,
478 fd
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
480 if (bt_ctf_field_get_error()) {
481 fprintf(stderr
, "Missing fd context info\n");
485 scope
= bt_ctf_get_top_level_scope(call_data
,
487 file_name
= bt_ctf_get_string(bt_ctf_get_field(call_data
,
488 scope
, "_filename"));
489 if (bt_ctf_field_get_error()) {
490 fprintf(stderr
, "Missing file name context info\n");
494 parent
= get_proc_pid(<tngtop
, pid
, pid
, timestamp
);
495 parent
->files_history
= create_file(parent
->files_history
, file_name
);
496 file
= parent
->files_history
->file
;
497 edit_file(parent
, file
, fd
);
502 return BT_CB_ERROR_STOP
;
This page took 0.05512 seconds and 4 git commands to generate.