1 /* The text dump facility needs to print headers before the trace set and
2 before each trace, to print each event, and to print statistics
6 #include <lttv/option.h>
7 #include <lttv/module.h>
9 #include <lttv/attribute.h>
10 #include <lttv/iattribute.h>
11 #include <lttv/state.h>
13 #include <ltt/event.h>
15 #include <ltt/trace.h>
32 void print_field(LttEvent
*e
, LttField
*f
, GString
*s
, gboolean field_names
) {
42 type
= ltt_field_type(f
);
43 switch(ltt_type_class(type
)) {
45 g_string_append_printf(s
, " %ld", ltt_event_get_long_int(e
,f
));
49 g_string_append_printf(s
, " %lu", ltt_event_get_long_unsigned(e
,f
));
53 g_string_append_printf(s
, " %g", ltt_event_get_double(e
,f
));
57 g_string_append_printf(s
, " \"%s\"", ltt_event_get_string(e
,f
));
61 g_string_append_printf(s
, " %s", ltt_enum_string_get(type
,
62 ltt_event_get_unsigned(e
,f
)-1));
67 g_string_append_printf(s
, " {");
68 nb
= ltt_event_field_element_number(e
,f
);
69 element
= ltt_field_element(f
);
70 for(i
= 0 ; i
< nb
; i
++) {
71 ltt_event_field_element_select(e
,f
,i
);
72 print_field(e
, element
, s
, field_names
);
74 g_string_append_printf(s
, " }");
78 g_string_append_printf(s
, " {");
79 nb
= ltt_type_member_number(type
);
80 for(i
= 0 ; i
< nb
; i
++) {
81 element
= ltt_field_member(f
,i
);
83 ltt_type_member_type(type
, i
, &name
);
84 g_string_append_printf(s
, " %s = ", name
);
86 print_field(e
, element
, s
, field_names
);
88 g_string_append_printf(s
, " }");
94 void lttv_event_to_string(LttEvent
*e
, LttTracefile
*tf
, GString
*s
,
95 gboolean mandatory_fields
, gboolean field_names
)
97 LttFacility
*facility
;
99 LttEventType
*event_type
;
107 g_string_set_size(s
,0);
109 facility
= ltt_event_facility(e
);
110 event_type
= ltt_event_eventtype(e
);
111 field
= ltt_event_field(e
);
113 if(mandatory_fields
) {
114 time
= ltt_event_time(e
);
115 g_string_append_printf(s
,"%s.%s: %ld.%ld (%s)",ltt_facility_name(facility
),
116 ltt_eventtype_name(event_type
), (long)time
.tv_sec
, time
.tv_nsec
,
117 ltt_tracefile_name(tf
));
118 /* Print the process id and the state/interrupt type of the process */
122 print_field(e
, field
, s
, field_names
);
126 /* Insert the hooks before and after each trace and tracefile, and for each
127 event. Print a global header. */
131 static GString
*a_string
;
133 static gboolean
write_traceset_header(void *hook_data
, void *call_data
)
135 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
137 if(a_file_name
== NULL
) a_file
= stdout
;
138 else a_file
= fopen(a_file_name
, "w");
140 if(a_file
== NULL
) g_error("cannot open file %s", a_file_name
);
142 /* Print the trace set header */
143 fprintf(a_file
,"Trace set contains %d traces\n\n",
144 lttv_traceset_number(tc
->ts
));
150 static gboolean
write_traceset_footer(void *hook_data
, void *call_data
)
152 LttvTracesetContext
*tc
= (LttvTracesetContext
*)call_data
;
154 fprintf(a_file
,"End trace set\n\n");
156 if(a_file_name
!= NULL
) fclose(a_file
);
162 static gboolean
write_trace_header(void *hook_data
, void *call_data
)
164 LttvTraceContext
*tc
= (LttvTraceContext
*)call_data
;
166 LttSystemDescription
*system
= ltt_trace_system_description(tc
->t
);
168 fprintf(a_file
," Trace from %s in %s\n%s\n\n", system
->node_name
,
169 system
->domain_name
, system
->description
);
174 static int write_event_content(void *hook_data
, void *call_data
)
176 LttvTracefileContext
*tfc
= (LttvTracefileContext
*)call_data
;
178 LttvTracefileState
*tfs
= (LttvTracefileState
*)call_data
;
184 lttv_event_to_string(e
, tfc
->tf
, a_string
, TRUE
, a_field_names
);
185 g_string_append_printf(a_string
,"\n");
188 g_string_append_printf(a_string
, " %s",
189 g_quark_to_string(tfs
->process
->state
->s
));
192 fputs(a_string
->str
, a_file
);
197 //void init(int argc, char **argv)
198 G_MODULE_EXPORT
void init(LttvModule
*self
, int argc
, char **argv
)
200 LttvAttributeValue value
;
202 LttvIAttribute
*attributes
= LTTV_IATTRIBUTE(lttv_global_attributes());
204 a_string
= g_string_new("");
207 lttv_option_add("output", 'o',
208 "output file where the text is written",
210 LTTV_OPT_STRING
, &a_file_name
, NULL
, NULL
);
212 a_field_names
= FALSE
;
213 lttv_option_add("field_names", 'l',
214 "write the field names for each event",
216 LTTV_OPT_NONE
, &a_field_names
, NULL
, NULL
);
219 lttv_option_add("process_state", 's',
220 "write the pid and state for each event",
222 LTTV_OPT_NONE
, &a_state
, NULL
, NULL
);
224 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/event/before",
225 LTTV_POINTER
, &value
));
226 g_assert((before_event
= *(value
.v_pointer
)) != NULL
);
227 lttv_hooks_add(before_event
, write_event_content
, NULL
);
229 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/trace/before",
230 LTTV_POINTER
, &value
));
231 g_assert((before_trace
= *(value
.v_pointer
)) != NULL
);
232 lttv_hooks_add(before_trace
, write_trace_header
, NULL
);
234 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/before",
235 LTTV_POINTER
, &value
));
236 g_assert((before_traceset
= *(value
.v_pointer
)) != NULL
);
237 lttv_hooks_add(before_traceset
, write_traceset_header
, NULL
);
239 g_assert(lttv_iattribute_find_by_path(attributes
, "hooks/traceset/after",
240 LTTV_POINTER
, &value
));
241 g_assert((after_traceset
= *(value
.v_pointer
)) != NULL
);
242 lttv_hooks_add(after_traceset
, write_traceset_footer
, NULL
);
246 G_MODULE_EXPORT
void destroy()
248 lttv_option_remove("output");
250 lttv_option_remove("field_names");
252 lttv_option_remove("process_state");
254 g_string_free(a_string
, TRUE
);
256 lttv_hooks_remove_data(before_event
, write_event_content
, NULL
);
258 lttv_hooks_remove_data(before_trace
, write_trace_header
, NULL
);
260 lttv_hooks_remove_data(before_trace
, write_traceset_header
, NULL
);
262 lttv_hooks_remove_data(before_trace
, write_traceset_footer
, NULL
);