| 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 | |
| 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> |
| 12 | #include <ltt/ltt.h> |
| 13 | #include <ltt/event.h> |
| 14 | #include <ltt/type.h> |
| 15 | #include <ltt/trace.h> |
| 16 | #include <stdio.h> |
| 17 | |
| 18 | static gboolean |
| 19 | a_field_names, |
| 20 | a_state; |
| 21 | |
| 22 | static char |
| 23 | *a_file_name; |
| 24 | |
| 25 | static LttvHooks |
| 26 | *before_traceset, |
| 27 | *after_traceset, |
| 28 | *before_trace, |
| 29 | *before_event; |
| 30 | |
| 31 | |
| 32 | void print_field(LttEvent *e, LttField *f, GString *s, gboolean field_names) { |
| 33 | |
| 34 | LttType *type; |
| 35 | |
| 36 | LttField *element; |
| 37 | |
| 38 | char *name; |
| 39 | |
| 40 | int nb, i; |
| 41 | |
| 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; |
| 47 | |
| 48 | case LTT_UINT: |
| 49 | g_string_append_printf(s, " %lu", ltt_event_get_long_unsigned(e,f)); |
| 50 | break; |
| 51 | |
| 52 | case LTT_FLOAT: |
| 53 | g_string_append_printf(s, " %g", ltt_event_get_double(e,f)); |
| 54 | break; |
| 55 | |
| 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, |
| 62 | ltt_event_get_unsigned(e,f)-1)); |
| 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); |
| 82 | if(field_names) { |
| 83 | ltt_type_member_type(type, i, &name); |
| 84 | g_string_append_printf(s, " %s = ", name); |
| 85 | } |
| 86 | print_field(e, element, s, field_names); |
| 87 | } |
| 88 | g_string_append_printf(s, " }"); |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void lttv_event_to_string(LttEvent *e, LttTracefile *tf, GString *s, |
| 95 | gboolean mandatory_fields, gboolean field_names, LttvTracefileState *tfs) |
| 96 | { |
| 97 | LttFacility *facility; |
| 98 | |
| 99 | LttEventType *event_type; |
| 100 | |
| 101 | LttType *type; |
| 102 | |
| 103 | LttField *field; |
| 104 | |
| 105 | LttTime time; |
| 106 | |
| 107 | g_string_set_size(s,0); |
| 108 | |
| 109 | facility = ltt_event_facility(e); |
| 110 | event_type = ltt_event_eventtype(e); |
| 111 | field = ltt_event_field(e); |
| 112 | |
| 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 */ |
| 119 | g_string_append_printf(s,", %d, %s", tfs->process->pid, |
| 120 | g_quark_to_string(tfs->process->state->t)); |
| 121 | } |
| 122 | |
| 123 | if(field) |
| 124 | print_field(e, field, s, field_names); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /* Insert the hooks before and after each trace and tracefile, and for each |
| 129 | event. Print a global header. */ |
| 130 | |
| 131 | static FILE *a_file; |
| 132 | |
| 133 | static GString *a_string; |
| 134 | |
| 135 | static gboolean write_traceset_header(void *hook_data, void *call_data) |
| 136 | { |
| 137 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 138 | |
| 139 | if(a_file_name == NULL) a_file = stdout; |
| 140 | else a_file = fopen(a_file_name, "w"); |
| 141 | |
| 142 | if(a_file == NULL) g_error("cannot open file %s", a_file_name); |
| 143 | |
| 144 | /* Print the trace set header */ |
| 145 | fprintf(a_file,"Trace set contains %d traces\n\n", |
| 146 | lttv_traceset_number(tc->ts)); |
| 147 | |
| 148 | return FALSE; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static gboolean write_traceset_footer(void *hook_data, void *call_data) |
| 153 | { |
| 154 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 155 | |
| 156 | fprintf(a_file,"End trace set\n\n"); |
| 157 | |
| 158 | if(a_file_name != NULL) fclose(a_file); |
| 159 | |
| 160 | return FALSE; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static gboolean write_trace_header(void *hook_data, void *call_data) |
| 165 | { |
| 166 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
| 167 | |
| 168 | LttSystemDescription *system = ltt_trace_system_description(tc->t); |
| 169 | |
| 170 | fprintf(a_file," Trace from %s in %s\n%s\n\n", system->node_name, |
| 171 | system->domain_name, system->description); |
| 172 | return FALSE; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | static int write_event_content(void *hook_data, void *call_data) |
| 177 | { |
| 178 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; |
| 179 | |
| 180 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
| 181 | |
| 182 | LttEvent *e; |
| 183 | |
| 184 | e = tfc->e; |
| 185 | |
| 186 | lttv_event_to_string(e, tfc->tf, a_string, TRUE, a_field_names, tfs); |
| 187 | g_string_append_printf(a_string,"\n"); |
| 188 | |
| 189 | if(a_state) { |
| 190 | g_string_append_printf(a_string, " %s", |
| 191 | g_quark_to_string(tfs->process->state->s)); |
| 192 | } |
| 193 | |
| 194 | fputs(a_string->str, a_file); |
| 195 | return FALSE; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | //void init(int argc, char **argv) |
| 200 | G_MODULE_EXPORT void init(LttvModule *self, int argc, char **argv) |
| 201 | { |
| 202 | LttvAttributeValue value; |
| 203 | |
| 204 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 205 | |
| 206 | a_string = g_string_new(""); |
| 207 | |
| 208 | a_file_name = NULL; |
| 209 | lttv_option_add("output", 'o', |
| 210 | "output file where the text is written", |
| 211 | "file name", |
| 212 | LTTV_OPT_STRING, &a_file_name, NULL, NULL); |
| 213 | |
| 214 | a_field_names = FALSE; |
| 215 | lttv_option_add("field_names", 'l', |
| 216 | "write the field names for each event", |
| 217 | "", |
| 218 | LTTV_OPT_NONE, &a_field_names, NULL, NULL); |
| 219 | |
| 220 | a_state = FALSE; |
| 221 | lttv_option_add("process_state", 's', |
| 222 | "write the pid and state for each event", |
| 223 | "", |
| 224 | LTTV_OPT_NONE, &a_state, NULL, NULL); |
| 225 | |
| 226 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before", |
| 227 | LTTV_POINTER, &value)); |
| 228 | g_assert((before_event = *(value.v_pointer)) != NULL); |
| 229 | lttv_hooks_add(before_event, write_event_content, NULL); |
| 230 | |
| 231 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
| 232 | LTTV_POINTER, &value)); |
| 233 | g_assert((before_trace = *(value.v_pointer)) != NULL); |
| 234 | lttv_hooks_add(before_trace, write_trace_header, NULL); |
| 235 | |
| 236 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
| 237 | LTTV_POINTER, &value)); |
| 238 | g_assert((before_traceset = *(value.v_pointer)) != NULL); |
| 239 | lttv_hooks_add(before_traceset, write_traceset_header, NULL); |
| 240 | |
| 241 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
| 242 | LTTV_POINTER, &value)); |
| 243 | g_assert((after_traceset = *(value.v_pointer)) != NULL); |
| 244 | lttv_hooks_add(after_traceset, write_traceset_footer, NULL); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | G_MODULE_EXPORT void destroy() |
| 249 | { |
| 250 | lttv_option_remove("output"); |
| 251 | |
| 252 | lttv_option_remove("field_names"); |
| 253 | |
| 254 | lttv_option_remove("process_state"); |
| 255 | |
| 256 | g_string_free(a_string, TRUE); |
| 257 | |
| 258 | lttv_hooks_remove_data(before_event, write_event_content, NULL); |
| 259 | |
| 260 | lttv_hooks_remove_data(before_trace, write_trace_header, NULL); |
| 261 | |
| 262 | lttv_hooks_remove_data(before_trace, write_traceset_header, NULL); |
| 263 | |
| 264 | lttv_hooks_remove_data(before_trace, write_traceset_footer, NULL); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | |
| 269 | |