git-svn-id: http://ltt.polymtl.ca/svn@289 04897980-b3bd-0310-b5e0-8ef037075253
[lttv.git] / ltt / branches / poly / lttv / textDump.c
CommitLineData
48f6f3c2 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
3 after each trace. */
4
996acd92 5#include <lttv/lttv.h>
6#include <lttv/option.h>
7#include <lttv/module.h>
8#include <lttv/hook.h>
9#include <lttv/attribute.h>
10#include <lttv/iattribute.h>
11#include <lttv/state.h>
ffd54a90 12#include <ltt/ltt.h>
13#include <ltt/event.h>
14#include <ltt/type.h>
15#include <ltt/trace.h>
16#include <stdio.h>
996acd92 17
dc877563 18static gboolean
19 a_field_names,
20 a_state;
21
22static char
23 *a_file_name;
24
25static LttvHooks
26 *before_traceset,
27 *after_traceset,
28 *before_trace,
29 *before_event;
30
f60d7a47 31
ffd54a90 32void print_field(LttEvent *e, LttField *f, GString *s, gboolean field_names) {
dc877563 33
ffd54a90 34 LttType *type;
dc877563 35
ffd54a90 36 LttField *element;
dc877563 37
ffd54a90 38 char *name;
dc877563 39
ffd54a90 40 int nb, i;
dc877563 41
ffd54a90 42 type = ltt_field_type(f);
43 switch(ltt_type_class(type)) {
44 case LTT_INT:
45 g_string_append_printf(s, " %ld", ltt_event_get_long_int(e,f));
46 break;
dc877563 47
ffd54a90 48 case LTT_UINT:
49 g_string_append_printf(s, " %lu", ltt_event_get_long_unsigned(e,f));
50 break;
dc877563 51
ffd54a90 52 case LTT_FLOAT:
53 g_string_append_printf(s, " %g", ltt_event_get_double(e,f));
54 break;
dc877563 55
ffd54a90 56 case LTT_STRING:
57 g_string_append_printf(s, " \"%s\"", ltt_event_get_string(e,f));
58 break;
59
60 case LTT_ENUM:
61 g_string_append_printf(s, " %s", ltt_enum_string_get(type,
c6bc9cb9 62 ltt_event_get_unsigned(e,f)-1));
ffd54a90 63 break;
64
65 case LTT_ARRAY:
66 case LTT_SEQUENCE:
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);
73 }
74 g_string_append_printf(s, " }");
75 break;
76
77 case LTT_STRUCT:
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);
c432246e 82 if(field_names) {
ffd54a90 83 ltt_type_member_type(type, i, &name);
c432246e 84 g_string_append_printf(s, " %s = ", name);
ffd54a90 85 }
86 print_field(e, element, s, field_names);
87 }
88 g_string_append_printf(s, " }");
89 break;
90 }
48f6f3c2 91}
92
93
ffd54a90 94void lttv_event_to_string(LttEvent *e, LttTracefile *tf, GString *s,
3d27549e 95 gboolean mandatory_fields, gboolean field_names, LttvTracefileState *tfs)
96{
ffd54a90 97 LttFacility *facility;
48f6f3c2 98
ffd54a90 99 LttEventType *event_type;
48f6f3c2 100
ffd54a90 101 LttType *type;
102
103 LttField *field;
48f6f3c2 104
ffd54a90 105 LttTime time;
48f6f3c2 106
ffd54a90 107 g_string_set_size(s,0);
dc877563 108
ffd54a90 109 facility = ltt_event_facility(e);
110 event_type = ltt_event_eventtype(e);
111 field = ltt_event_field(e);
48f6f3c2 112
ffd54a90 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));
c432246e 118 /* Print the process id and the state/interrupt type of the process */
07bb8c8f 119 g_string_append_printf(s,", %u, %u, %s", tfs->process->pid,
120 tfs->process->ppid,
121 g_quark_to_string(tfs->process->state->t));
ffd54a90 122 }
123
c6bc9cb9 124 if(field)
125 print_field(e, field, s, field_names);
ffd54a90 126}
48f6f3c2 127
128
dc877563 129/* Insert the hooks before and after each trace and tracefile, and for each
130 event. Print a global header. */
131
132static FILE *a_file;
48f6f3c2 133
dc877563 134static GString *a_string;
135
ffd54a90 136static gboolean write_traceset_header(void *hook_data, void *call_data)
48f6f3c2 137{
dc877563 138 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
48f6f3c2 139
dc877563 140 if(a_file_name == NULL) a_file = stdout;
141 else a_file = fopen(a_file_name, "w");
48f6f3c2 142
dc877563 143 if(a_file == NULL) g_error("cannot open file %s", a_file_name);
48f6f3c2 144
dc877563 145 /* Print the trace set header */
146 fprintf(a_file,"Trace set contains %d traces\n\n",
ffd54a90 147 lttv_traceset_number(tc->ts));
48f6f3c2 148
dc877563 149 return FALSE;
48f6f3c2 150}
151
152
ffd54a90 153static gboolean write_traceset_footer(void *hook_data, void *call_data)
48f6f3c2 154{
dc877563 155 LttvTracesetContext *tc = (LttvTracesetContext *)call_data;
48f6f3c2 156
dc877563 157 fprintf(a_file,"End trace set\n\n");
48f6f3c2 158
ffd54a90 159 if(a_file_name != NULL) fclose(a_file);
160
dc877563 161 return FALSE;
48f6f3c2 162}
163
164
dc877563 165static gboolean write_trace_header(void *hook_data, void *call_data)
48f6f3c2 166{
dc877563 167 LttvTraceContext *tc = (LttvTraceContext *)call_data;
168
169 LttSystemDescription *system = ltt_trace_system_description(tc->t);
48f6f3c2 170
dc877563 171 fprintf(a_file," Trace from %s in %s\n%s\n\n", system->node_name,
172 system->domain_name, system->description);
173 return FALSE;
48f6f3c2 174}
175
176
dc877563 177static int write_event_content(void *hook_data, void *call_data)
48f6f3c2 178{
dc877563 179 LttvTracefileContext *tfc = (LttvTracefileContext *)call_data;
48f6f3c2 180
dc877563 181 LttvTracefileState *tfs = (LttvTracefileState *)call_data;
48f6f3c2 182
dc877563 183 LttEvent *e;
48f6f3c2 184
dc877563 185 e = tfc->e;
48f6f3c2 186
3d27549e 187 lttv_event_to_string(e, tfc->tf, a_string, TRUE, a_field_names, tfs);
c6bc9cb9 188 g_string_append_printf(a_string,"\n");
dc877563 189
190 if(a_state) {
191 g_string_append_printf(a_string, " %s",
ffd54a90 192 g_quark_to_string(tfs->process->state->s));
dc877563 193 }
48f6f3c2 194
ffd54a90 195 fputs(a_string->str, a_file);
dc877563 196 return FALSE;
48f6f3c2 197}
198
199
d83f6739 200G_MODULE_EXPORT void init(LttvModule *self, int argc, char **argv)
48f6f3c2 201{
ffd54a90 202 LttvAttributeValue value;
48f6f3c2 203
ffd54a90 204 LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes());
48f6f3c2 205
9402662d 206 LttvModule *batchAnalysis =
207 lttv_module_require(self, "batchAnalysis", argc, argv);
208
209 if(batchAnalysis == NULL)
210 {
211 g_error("Can't load required module batchAnalysis");
212 return;
213 }
214
215
c6bc9cb9 216 a_string = g_string_new("");
217
ffd54a90 218 a_file_name = NULL;
219 lttv_option_add("output", 'o',
220 "output file where the text is written",
221 "file name",
222 LTTV_OPT_STRING, &a_file_name, NULL, NULL);
48f6f3c2 223
ffd54a90 224 a_field_names = FALSE;
225 lttv_option_add("field_names", 'l',
226 "write the field names for each event",
227 "",
228 LTTV_OPT_NONE, &a_field_names, NULL, NULL);
48f6f3c2 229
ffd54a90 230 a_state = FALSE;
231 lttv_option_add("process_state", 's',
232 "write the pid and state for each event",
233 "",
234 LTTV_OPT_NONE, &a_state, NULL, NULL);
dc877563 235
ffd54a90 236 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before",
237 LTTV_POINTER, &value));
238 g_assert((before_event = *(value.v_pointer)) != NULL);
239 lttv_hooks_add(before_event, write_event_content, NULL);
dc877563 240
ffd54a90 241 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before",
242 LTTV_POINTER, &value));
243 g_assert((before_trace = *(value.v_pointer)) != NULL);
244 lttv_hooks_add(before_trace, write_trace_header, NULL);
dc877563 245
ffd54a90 246 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before",
247 LTTV_POINTER, &value));
248 g_assert((before_traceset = *(value.v_pointer)) != NULL);
249 lttv_hooks_add(before_traceset, write_traceset_header, NULL);
dc877563 250
ffd54a90 251 g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after",
252 LTTV_POINTER, &value));
253 g_assert((after_traceset = *(value.v_pointer)) != NULL);
254 lttv_hooks_add(after_traceset, write_traceset_footer, NULL);
255}
48f6f3c2 256
48f6f3c2 257
d83f6739 258G_MODULE_EXPORT void destroy()
ffd54a90 259{
260 lttv_option_remove("output");
48f6f3c2 261
ffd54a90 262 lttv_option_remove("field_names");
48f6f3c2 263
ffd54a90 264 lttv_option_remove("process_state");
48f6f3c2 265
c6bc9cb9 266 g_string_free(a_string, TRUE);
267
ffd54a90 268 lttv_hooks_remove_data(before_event, write_event_content, NULL);
48f6f3c2 269
ffd54a90 270 lttv_hooks_remove_data(before_trace, write_trace_header, NULL);
48f6f3c2 271
ffd54a90 272 lttv_hooks_remove_data(before_trace, write_traceset_header, NULL);
48f6f3c2 273
ffd54a90 274 lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL);
48f6f3c2 275}
276
277
278
279
This page took 0.037907 seconds and 4 git commands to generate.