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
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 #include <sys/types.h>
24 #include <babeltrace/babeltrace.h>
26 #include "lttngtoptypes.h"
28 #include "iostreamtop.h"
30 void add_file(struct processtop
*proc
, struct files
*file
, int fd
)
32 if (proc
->process_files_table
->len
<= fd
) {
33 g_ptr_array_set_size(proc
->process_files_table
, fd
);
34 g_ptr_array_add(proc
->process_files_table
, file
);
36 g_ptr_array_index(proc
->process_files_table
, fd
) = file
;
42 void insert_file(struct processtop
*proc
, int fd
)
46 if (fd
>= proc
->process_files_table
->len
) {
47 tmp
= g_new0(struct files
, 1);
48 tmp
->name
= "Unknown";
49 add_file(proc
, tmp
, fd
);
52 tmp
= g_ptr_array_index(proc
->process_files_table
, fd
);
54 tmp
= g_new0(struct files
, 1);
55 tmp
->name
= "Unknown";
59 add_file(proc
, tmp
, fd
);
64 void close_file(struct processtop
*proc
, int fd
)
68 len
= proc
->process_files_table
->len
;
71 * It is possible that a file was open before taking the trace
72 * and its fd could be greater than all of the others fd
76 g_ptr_array_remove_index_fast(proc
->process_files_table
, fd
);
77 g_ptr_array_set_size(proc
->process_files_table
, len
+ 1);
81 struct files
*get_file(struct processtop
*proc
, int fd
)
84 tmp
= g_ptr_array_index(proc
->process_files_table
, fd
);
88 void show_table(GPtrArray
*tab
)
93 for (i
= 0 ; i
< tab
->len
; i
++) {
94 file
= g_ptr_array_index(tab
, i
);
96 fprintf(stderr
, "NULL, ");
98 fprintf(stderr
, "%s, ", file
->name
);
100 fprintf(stderr
, "]\n\n");
103 int update_iostream_ret(struct lttngtop
*ctx
, int tid
, char *comm
,
104 unsigned long timestamp
, uint64_t cpu_id
, int ret
)
106 struct processtop
*tmp
;
107 struct files
*tmpfile
;
110 tmp
= get_proc(ctx
, tid
, comm
, timestamp
);
112 if (tmp
->syscall_info
!= NULL
) {
113 if (tmp
->syscall_info
->type
== __NR_read
115 tmp
->totalfileread
+= ret
;
116 tmp
->fileread
+= ret
;
117 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
118 tmpfile
->read
+= ret
;
119 } else if (tmp
->syscall_info
->type
== __NR_write
121 tmp
->totalfilewrite
+= ret
;
122 tmp
->filewrite
+= ret
;
123 tmpfile
= get_file(tmp
, tmp
->syscall_info
->fd
);
124 tmpfile
->write
+= ret
;
125 } else if (tmp
->syscall_info
->type
== __NR_open
127 add_file(tmp
, tmp
->files_history
->file
, ret
);
131 g_free(tmp
->syscall_info
);
132 tmp
->syscall_info
= NULL
;
137 struct syscalls
*create_syscall_info(unsigned int type
, uint64_t cpu_id
,
138 unsigned int tid
, int fd
)
140 struct syscalls
*syscall_info
;
142 syscall_info
= g_new0(struct syscalls
, 1);
143 syscall_info
->type
= type
;
144 syscall_info
->cpu_id
= cpu_id
;
145 syscall_info
->tid
= tid
;
146 syscall_info
->fd
= fd
;
151 struct file_history
*create_file(struct file_history
*history
, char *file_name
)
153 struct files
*new_file
;
154 struct file_history
*new_history
;
156 new_file
= g_new0(struct files
, 1);
157 new_history
= g_new0(struct file_history
, 1);
158 new_file
->name
= strdup(file_name
);
161 new_history
->file
= new_file
;
162 new_history
->next
= history
;
167 enum bt_cb_ret
handle_exit_syscall(struct bt_ctf_event
*call_data
,
170 struct definition
*scope
;
171 unsigned long timestamp
;
176 timestamp
= bt_ctf_get_timestamp(call_data
);
177 if (timestamp
== -1ULL)
180 scope
= bt_ctf_get_top_level_scope(call_data
,
181 BT_STREAM_EVENT_CONTEXT
);
182 comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
183 scope
, "_procname"));
184 if (bt_ctf_field_get_error()) {
185 fprintf(stderr
, "Missing procname context info\n");
189 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
191 if (bt_ctf_field_get_error()) {
192 fprintf(stderr
, "Missing tid context info\n");
196 scope
= bt_ctf_get_top_level_scope(call_data
,
198 ret
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
200 if (bt_ctf_field_get_error()) {
201 fprintf(stderr
, "Missing ret context info\n");
205 cpu_id
= get_cpu_id(call_data
);
208 * if we encounter an exit_syscall and
209 * it is not for a syscall read or write
210 * we just abort the execution of this callback
212 if ((update_iostream_ret(<tngtop
, tid
, comm
, timestamp
, cpu_id
, ret
)) < 0)
213 return BT_CB_ERROR_CONTINUE
;
218 return BT_CB_ERROR_STOP
;
222 enum bt_cb_ret
handle_sys_write(struct bt_ctf_event
*call_data
,
225 struct definition
*scope
;
226 struct processtop
*tmp
;
227 unsigned long timestamp
;
233 timestamp
= bt_ctf_get_timestamp(call_data
);
234 if (timestamp
== -1ULL)
237 scope
= bt_ctf_get_top_level_scope(call_data
,
238 BT_STREAM_EVENT_CONTEXT
);
239 comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
240 scope
, "_procname"));
241 if (bt_ctf_field_get_error()) {
242 fprintf(stderr
, "Missing procname context info\n");
246 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
248 if (bt_ctf_field_get_error()) {
249 fprintf(stderr
, "Missing tid context info\n");
253 cpu_id
= get_cpu_id(call_data
);
255 scope
= bt_ctf_get_top_level_scope(call_data
,
257 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
259 if (bt_ctf_field_get_error()) {
260 fprintf(stderr
, "Missing fd context info\n");
264 tmp
= get_proc(<tngtop
, tid
, comm
, timestamp
);
265 tmp
->syscall_info
= create_syscall_info(__NR_write
, cpu_id
, tid
, fd
);
267 insert_file(tmp
, fd
);
272 return BT_CB_ERROR_STOP
;
275 enum bt_cb_ret
handle_sys_read(struct bt_ctf_event
*call_data
,
278 struct processtop
*tmp
;
279 struct definition
*scope
;
280 unsigned long timestamp
;
286 timestamp
= bt_ctf_get_timestamp(call_data
);
287 if (timestamp
== -1ULL)
290 scope
= bt_ctf_get_top_level_scope(call_data
,
291 BT_STREAM_EVENT_CONTEXT
);
292 comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
293 scope
, "_procname"));
294 if (bt_ctf_field_get_error()) {
295 fprintf(stderr
, "Missing procname context info\n");
299 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
301 if (bt_ctf_field_get_error()) {
302 fprintf(stderr
, "Missing tid context info\n");
306 cpu_id
= get_cpu_id(call_data
);
308 scope
= bt_ctf_get_top_level_scope(call_data
,
310 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
312 if (bt_ctf_field_get_error()) {
313 fprintf(stderr
, "Missing fd context info\n");
317 tmp
= get_proc(<tngtop
, tid
, comm
, timestamp
);
318 tmp
->syscall_info
= create_syscall_info(__NR_read
, cpu_id
, tid
, fd
);
320 insert_file(tmp
, fd
);
325 return BT_CB_ERROR_STOP
;
329 enum bt_cb_ret
handle_sys_open(struct bt_ctf_event
*call_data
,
333 struct processtop
*tmp
;
334 struct definition
*scope
;
335 unsigned long timestamp
;
341 timestamp
= bt_ctf_get_timestamp(call_data
);
342 if (timestamp
== -1ULL)
345 scope
= bt_ctf_get_top_level_scope(call_data
,
346 BT_STREAM_EVENT_CONTEXT
);
347 comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
348 scope
, "_procname"));
349 if (bt_ctf_field_get_error()) {
350 fprintf(stderr
, "Missing procname context info\n");
354 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
356 if (bt_ctf_field_get_error()) {
357 fprintf(stderr
, "Missing tid context info\n");
361 cpu_id
= get_cpu_id(call_data
);
363 scope
= bt_ctf_get_top_level_scope(call_data
,
365 file
= bt_ctf_get_string(bt_ctf_get_field(call_data
,
366 scope
, "_filename"));
367 if (bt_ctf_field_get_error()) {
368 fprintf(stderr
, "Missing fd context info\n");
372 tmp
= get_proc(<tngtop
, tid
, comm
, timestamp
);
373 tmp
->syscall_info
= create_syscall_info(__NR_open
, cpu_id
, tid
, -1);
375 tmp
->files_history
= create_file(tmp
->files_history
, file
);
380 return BT_CB_ERROR_STOP
;
384 enum bt_cb_ret
handle_sys_close(struct bt_ctf_event
*call_data
,
387 struct definition
*scope
;
388 unsigned long timestamp
;
390 struct processtop
*tmp
;
394 timestamp
= bt_ctf_get_timestamp(call_data
);
395 if (timestamp
== -1ULL)
398 scope
= bt_ctf_get_top_level_scope(call_data
,
399 BT_STREAM_EVENT_CONTEXT
);
400 comm
= bt_ctf_get_char_array(bt_ctf_get_field(call_data
,
401 scope
, "_procname"));
402 if (bt_ctf_field_get_error()) {
403 fprintf(stderr
, "Missing procname context info\n");
407 tid
= bt_ctf_get_int64(bt_ctf_get_field(call_data
,
409 if (bt_ctf_field_get_error()) {
410 fprintf(stderr
, "Missing tid context info\n");
414 scope
= bt_ctf_get_top_level_scope(call_data
,
416 fd
= bt_ctf_get_uint64(bt_ctf_get_field(call_data
,
418 if (bt_ctf_field_get_error()) {
419 fprintf(stderr
, "Missing fd context info\n");
423 tmp
= get_proc(<tngtop
, tid
, comm
, timestamp
);
429 return BT_CB_ERROR_STOP
;
This page took 0.041237 seconds and 4 git commands to generate.