35a58a414f8e6274ac7898e9e18d099bb75bb390
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
;
34 size
= proc
->process_files_table
->len
;
37 g_ptr_array_set_size(proc
->process_files_table
, fd
);
38 g_ptr_array_add(proc
->process_files_table
, file
);
40 tmp_file
= g_ptr_array_index(proc
->process_files_table
, fd
);
42 g_ptr_array_index(proc
->process_files_table
, fd
) = file
;
44 if (strcmp(tmp_file
->name
, file
->name
) != 0) {
45 size
= proc
->process_files_table
->len
;
46 g_ptr_array_set_size(proc
->process_files_table
,
48 g_ptr_array_index(proc
->process_files_table
,
50 g_ptr_array_index(proc
->process_files_table
,
53 tmp_file
->flag
= __NR_open
;
57 file
->flag
= __NR_open
;
59 lttngtop
.nbnewfiles
++;
64 void edit_file(struct processtop
*proc
, struct files
*file
, int fd
)
66 int size
= proc
->process_files_table
->len
;
67 struct files
*tmpfile
;
72 tmpfile
= g_ptr_array_index(proc
->process_files_table
, fd
);
73 tmpfile
->name
= strdup(file
->name
);
78 void insert_file(struct processtop
*proc
, int fd
)
82 if (fd
>= proc
->process_files_table
->len
) {
83 tmp
= g_new0(struct files
, 1);
84 tmp
->name
= "Unknown";
88 add_file(proc
, tmp
, fd
);
90 tmp
= g_ptr_array_index(proc
->process_files_table
, fd
);
92 tmp
= g_new0(struct files
, 1);
93 tmp
->name
= "Unknown";
97 add_file(proc
, tmp
, fd
);
102 void close_file(struct processtop
*proc
, int fd
)
106 file
= get_file(proc
, fd
);
108 file
->flag
= __NR_close
;
111 lttngtop
.nbclosedfiles
++;
114 struct files
*get_file(struct processtop
*proc
, int fd
)
117 struct files
*tmp
= NULL
;
119 len
= proc
->process_files_table
->len
;
122 * It is possible that a file was open before taking the trace
123 * and its fd could be greater than all of the others fd
124 * used by the process
126 if (fd
< len
&& fd
>= 0)
127 tmp
= g_ptr_array_index(proc
->process_files_table
, fd
);
132 void show_table(GPtrArray
*tab
)
137 for (i
= 0 ; i
< tab
->len
; i
++) {
138 file
= g_ptr_array_index(tab
, i
);
140 fprintf(stderr
, "NULL, ");
142 fprintf(stderr
, "%s, ", file
->name
);
144 fprintf(stderr
, "]\n\n");
147 void show_history(struct file_history
*history
)
149 struct file_history
*tmp
= history
;
151 while (tmp
!= NULL
) {
152 fprintf(stderr
, "fd = %d, name = %s\n", tmp
->file
->fd
,
159 int update_iostream_ret(struct lttngtop
*ctx
, int tid
, char *comm
,
160 unsigned long timestamp
, uint64_t cpu_id
, int ret
)
162 struct processtop
*tmp
;
163 struct files
*tmpfile
;
166 tmp
= get_proc(ctx
, tid
, comm
, timestamp
);
168 if (tmp
->syscall_info
!= NULL
) {
169 if (tmp
->syscall_info
->type
== __NR_read
171 tmp
->totalfileread
+= ret
;
172 tmp
->fileread
+= ret
;
173 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
174 tmpfile
->read
+= ret
;
175 } else if (tmp
->syscall_info
->type
== __NR_write
177 tmp
->totalfilewrite
+= ret
;
178 tmp
->filewrite
+= ret
;
179 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
180 tmpfile
->write
+= ret
;
181 } else if (tmp
->syscall_info
->type
== __NR_open
183 tmpfile
= tmp
->files_history
->file
;
184 add_file(tmp
, tmpfile
, ret
);
189 g_free(tmp
->syscall_info
);
190 tmp
->syscall_info
= NULL
;
195 struct syscalls
*create_syscall_info(unsigned int type
, uint64_t cpu_id
,
196 unsigned int tid
, int fd
)
198 struct syscalls
*syscall_info
;
200 syscall_info
= g_new0(struct syscalls
, 1);
201 syscall_info
->type
= type
;
202 syscall_info
->cpu_id
= cpu_id
;
203 syscall_info
->tid
= tid
;
204 syscall_info
->fd
= fd
;
209 struct file_history
*create_file(struct file_history
*history
, char *file_name
)
211 struct files
*new_file
;
212 struct file_history
*new_history
;
214 new_file
= g_new0(struct files
, 1);
215 new_history
= g_new0(struct file_history
, 1);
216 new_file
->name
= strdup(file_name
);
219 new_history
->file
= new_file
;
220 new_history
->next
= history
;
225 enum bt_cb_ret
handle_exit_syscall(struct bt_ctf_event
*call_data
,
228 const struct definition
*scope
;
229 unsigned long timestamp
;
234 timestamp
= bt_ctf_get_timestamp(call_data
);
235 if (timestamp
== -1ULL)
238 comm
= get_context_comm(call_data
);
239 tid
= get_context_tid(call_data
);
241 scope
= bt_ctf_get_top_level_scope(call_data
,
243 ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
245 if (bt_ctf_field_get_error()) {
246 fprintf(stderr
, "Missing ret context info\n");
250 cpu_id
= get_cpu_id(call_data
);
253 * if we encounter an exit_syscall and
254 * it is not for a syscall read or write
255 * we just abort the execution of this callback
257 if ((update_iostream_ret(<tngtop
, tid
, comm
, timestamp
, cpu_id
, ret
)) < 0)
258 return BT_CB_ERROR_CONTINUE
;
263 return BT_CB_ERROR_STOP
;
267 enum bt_cb_ret
handle_sys_write(struct bt_ctf_event
*call_data
,
270 const struct definition
*scope
;
271 struct processtop
*tmp
;
272 unsigned long timestamp
;
278 timestamp
= bt_ctf_get_timestamp(call_data
);
279 if (timestamp
== -1ULL)
282 comm
= get_context_comm(call_data
);
283 tid
= get_context_tid(call_data
);
284 cpu_id
= get_cpu_id(call_data
);
286 scope
= bt_ctf_get_top_level_scope(call_data
,
288 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
290 if (bt_ctf_field_get_error()) {
291 fprintf(stderr
, "Missing fd context info\n");
295 tmp
= get_proc(<tngtop
, tid
, comm
, timestamp
);
296 tmp
->syscall_info
= create_syscall_info(__NR_write
, cpu_id
, tid
, fd
);
298 insert_file(tmp
, fd
);
303 return BT_CB_ERROR_STOP
;
306 enum bt_cb_ret
handle_sys_read(struct bt_ctf_event
*call_data
,
309 struct processtop
*tmp
;
310 const struct definition
*scope
;
311 unsigned long timestamp
;
317 timestamp
= bt_ctf_get_timestamp(call_data
);
318 if (timestamp
== -1ULL)
321 comm
= get_context_comm(call_data
);
322 tid
= get_context_tid(call_data
);
323 cpu_id
= get_cpu_id(call_data
);
325 scope
= bt_ctf_get_top_level_scope(call_data
,
327 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
329 if (bt_ctf_field_get_error()) {
330 fprintf(stderr
, "Missing fd context info\n");
334 tmp
= get_proc(<tngtop
, tid
, comm
, timestamp
);
335 tmp
->syscall_info
= create_syscall_info(__NR_read
, cpu_id
, tid
, fd
);
337 insert_file(tmp
, fd
);
342 return BT_CB_ERROR_STOP
;
346 enum bt_cb_ret
handle_sys_open(struct bt_ctf_event
*call_data
,
350 struct processtop
*tmp
;
351 const struct definition
*scope
;
352 unsigned long timestamp
;
358 timestamp
= bt_ctf_get_timestamp(call_data
);
359 if (timestamp
== -1ULL)
362 comm
= get_context_comm(call_data
);
363 tid
= get_context_tid(call_data
);
364 cpu_id
= get_cpu_id(call_data
);
366 scope
= bt_ctf_get_top_level_scope(call_data
,
368 file
= bt_ctf_get_string(bt_ctf_get_field(call_data
,
369 scope
, "_filename"));
370 if (bt_ctf_field_get_error()) {
371 fprintf(stderr
, "Missing file name context info\n");
375 tmp
= get_proc(<tngtop
, tid
, comm
, timestamp
);
376 tmp
->syscall_info
= create_syscall_info(__NR_open
, cpu_id
, tid
, -1);
378 tmp
->files_history
= create_file(tmp
->files_history
, file
);
383 return BT_CB_ERROR_STOP
;
387 enum bt_cb_ret
handle_sys_close(struct bt_ctf_event
*call_data
,
390 const struct definition
*scope
;
391 unsigned long timestamp
;
393 struct processtop
*tmp
;
397 timestamp
= bt_ctf_get_timestamp(call_data
);
398 if (timestamp
== -1ULL)
401 comm
= get_context_comm(call_data
);
402 tid
= get_context_tid(call_data
);
404 scope
= bt_ctf_get_top_level_scope(call_data
,
406 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
408 if (bt_ctf_field_get_error()) {
409 fprintf(stderr
, "Missing fd context info\n");
413 tmp
= get_proc(<tngtop
, tid
, comm
, timestamp
);
420 return BT_CB_ERROR_STOP
;
423 enum bt_cb_ret handle_statedump_file_descriptor(struct bt_ctf_event *call_data,
426 struct definition *scope;
428 unsigned long timestamp;
430 struct processtop *tmp;
431 char *comm, *file_name;
434 timestamp = bt_ctf_get_timestamp(call_data);
435 if (timestamp == -1ULL)
438 comm = get_context_comm(call_data);
440 scope = bt_ctf_get_top_level_scope(call_data,
442 tid = bt_ctf_get_uint64(bt_ctf_get_field(call_data,
444 if (bt_ctf_field_get_error()) {
445 fprintf(stderr, "Missing tid context info\n");
449 scope = bt_ctf_get_top_level_scope(call_data,
451 fd = bt_ctf_get_uint64(bt_ctf_get_field(call_data,
453 if (bt_ctf_field_get_error()) {
454 fprintf(stderr, "Missing fd context info\n");
458 scope = bt_ctf_get_top_level_scope(call_data,
460 file_name = bt_ctf_get_string(bt_ctf_get_field(call_data,
461 scope, "_filename"));
462 if (bt_ctf_field_get_error()) {
463 fprintf(stderr, "Missing file name context info\n");
467 file = g_new0(struct files, 1);
468 file->name = strdup(file_name);
470 tmp = find_process_tid(<tngtop, tid, comm);
471 edit_file(tmp, file, fd);
473 fprintf(stderr, "%lu %s\n", tmp->tid, file_name);
478 return BT_CB_ERROR_STOP;
This page took 0.037242 seconds and 3 git commands to generate.