bda9c7ce9d8c098b86b6116ba85385e92b084c00
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
);
200 if (tmp
->syscall_info
!= NULL
) {
201 if (tmp
->syscall_info
->type
== __NR_read
203 tmp
->totalfileread
+= ret
;
204 tmp
->fileread
+= ret
;
205 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
207 tmpfile
->read
+= ret
;
208 } else if (tmp
->syscall_info
->type
== __NR_write
210 tmp
->totalfilewrite
+= ret
;
211 tmp
->filewrite
+= ret
;
212 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
214 tmpfile
->write
+= ret
;
215 } else if (tmp
->syscall_info
->type
== __NR_open
217 tmpfile
= tmp
->files_history
->file
;
218 add_file(tmp
, tmpfile
, ret
);
223 g_free(tmp
->syscall_info
);
224 tmp
->syscall_info
= NULL
;
229 struct syscalls
*create_syscall_info(unsigned int type
, uint64_t cpu_id
,
230 unsigned int tid
, int fd
)
232 struct syscalls
*syscall_info
;
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
;
243 struct file_history
*create_file(struct file_history
*history
, char *file_name
)
245 struct files
*new_file
;
246 struct file_history
*new_history
;
248 new_file
= g_new0(struct files
, 1);
249 new_history
= g_new0(struct file_history
, 1);
250 new_file
->name
= strdup(file_name
);
254 new_history
->file
= new_file
;
255 new_history
->next
= history
;
260 enum bt_cb_ret
handle_exit_syscall(struct bt_ctf_event
*call_data
,
263 const struct definition
*scope
;
264 unsigned long timestamp
;
269 timestamp
= bt_ctf_get_timestamp(call_data
);
270 if (timestamp
== -1ULL)
273 comm
= get_context_comm(call_data
);
274 tid
= get_context_tid(call_data
);
276 scope
= bt_ctf_get_top_level_scope(call_data
,
278 ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
280 if (bt_ctf_field_get_error()) {
281 fprintf(stderr
, "Missing ret context info\n");
285 cpu_id
= get_cpu_id(call_data
);
288 * if we encounter an exit_syscall and
289 * it is not for a syscall read or write
290 * we just abort the execution of this callback
292 if ((update_iostream_ret(<tngtop
, tid
, comm
, timestamp
, cpu_id
, ret
)) < 0)
293 return BT_CB_ERROR_CONTINUE
;
298 return BT_CB_ERROR_STOP
;
302 enum bt_cb_ret
handle_sys_write(struct bt_ctf_event
*call_data
,
305 const struct definition
*scope
;
306 struct processtop
*tmp
;
307 unsigned long timestamp
;
313 timestamp
= bt_ctf_get_timestamp(call_data
);
314 if (timestamp
== -1ULL)
317 tid
= get_context_tid(call_data
);
318 cpu_id
= get_cpu_id(call_data
);
320 procname
= get_context_comm(call_data
);
322 scope
= bt_ctf_get_top_level_scope(call_data
,
324 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
326 if (bt_ctf_field_get_error()) {
327 fprintf(stderr
, "Missing fd context info\n");
331 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
);
332 tmp
->syscall_info
= create_syscall_info(__NR_write
, cpu_id
, tid
, fd
);
334 insert_file(tmp
, fd
);
339 return BT_CB_ERROR_STOP
;
342 enum bt_cb_ret
handle_sys_read(struct bt_ctf_event
*call_data
,
345 struct processtop
*tmp
;
346 const struct definition
*scope
;
347 unsigned long timestamp
;
353 timestamp
= bt_ctf_get_timestamp(call_data
);
354 if (timestamp
== -1ULL)
357 tid
= get_context_tid(call_data
);
358 cpu_id
= get_cpu_id(call_data
);
360 procname
= get_context_comm(call_data
);
362 scope
= bt_ctf_get_top_level_scope(call_data
,
364 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
366 if (bt_ctf_field_get_error()) {
367 fprintf(stderr
, "Missing fd context info\n");
371 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
);
372 tmp
->syscall_info
= create_syscall_info(__NR_read
, cpu_id
, tid
, fd
);
374 insert_file(tmp
, fd
);
379 return BT_CB_ERROR_STOP
;
383 enum bt_cb_ret
handle_sys_open(struct bt_ctf_event
*call_data
,
387 struct processtop
*tmp
;
388 const struct definition
*scope
;
389 unsigned long timestamp
;
395 timestamp
= bt_ctf_get_timestamp(call_data
);
396 if (timestamp
== -1ULL)
399 tid
= get_context_tid(call_data
);
400 cpu_id
= get_cpu_id(call_data
);
402 procname
= get_context_comm(call_data
);
404 scope
= bt_ctf_get_top_level_scope(call_data
,
406 file
= bt_ctf_get_string(bt_ctf_get_field(call_data
,
407 scope
, "_filename"));
408 if (bt_ctf_field_get_error()) {
409 fprintf(stderr
, "Missing file name context info\n");
413 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
);
414 tmp
->syscall_info
= create_syscall_info(__NR_open
, cpu_id
, tid
, -1);
416 tmp
->files_history
= create_file(tmp
->files_history
, file
);
421 return BT_CB_ERROR_STOP
;
425 enum bt_cb_ret
handle_sys_close(struct bt_ctf_event
*call_data
,
428 const struct definition
*scope
;
429 struct processtop
*tmp
;
430 unsigned long timestamp
;
435 timestamp
= bt_ctf_get_timestamp(call_data
);
436 if (timestamp
== -1ULL)
439 tid
= get_context_tid(call_data
);
441 procname
= get_context_comm(call_data
);
443 scope
= bt_ctf_get_top_level_scope(call_data
,
445 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
447 if (bt_ctf_field_get_error()) {
448 fprintf(stderr
, "Missing fd context info\n");
452 tmp
= get_proc(<tngtop
, tid
, procname
, timestamp
);
459 return BT_CB_ERROR_STOP
;
462 enum bt_cb_ret
handle_statedump_file_descriptor(struct bt_ctf_event
*call_data
,
465 const struct definition
*scope
;
466 struct processtop
*parent
;
468 unsigned long timestamp
;
473 timestamp
= bt_ctf_get_timestamp(call_data
);
474 if (timestamp
== -1ULL)
477 scope
= bt_ctf_get_top_level_scope(call_data
,
479 pid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
481 if (bt_ctf_field_get_error()) {
482 fprintf(stderr
, "Missing tid context info\n");
486 scope
= bt_ctf_get_top_level_scope(call_data
,
488 fd
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
490 if (bt_ctf_field_get_error()) {
491 fprintf(stderr
, "Missing fd context info\n");
495 scope
= bt_ctf_get_top_level_scope(call_data
,
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");
504 parent
= get_proc_pid(<tngtop
, 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
);
512 return BT_CB_ERROR_STOP
;
This page took 0.041729 seconds and 3 git commands to generate.