| 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 | |
| 13 | static gboolean |
| 14 | a_field_names, |
| 15 | a_state; |
| 16 | |
| 17 | static char |
| 18 | *a_file_name; |
| 19 | |
| 20 | static LttvHooks |
| 21 | *before_traceset, |
| 22 | *after_traceset, |
| 23 | *before_trace, |
| 24 | *before_event; |
| 25 | |
| 26 | |
| 27 | void init(int argc, char **argv) |
| 28 | { |
| 29 | LttvAttributeValue *value; |
| 30 | |
| 31 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
| 32 | |
| 33 | a_file_name = NULL; |
| 34 | lttv_option_add("output", 'o', |
| 35 | "output file where the text is written", |
| 36 | "file name", |
| 37 | LTTV_OPT_STRING, &a_file_name, NULL, NULL); |
| 38 | |
| 39 | a_field_names = FALSE; |
| 40 | lttv_option_add("field_names", 'l', |
| 41 | "write the field names for each event", |
| 42 | "", |
| 43 | LTTV_OPT_NONE, &a_field_names, NULL, NULL); |
| 44 | |
| 45 | a_state = FALSE; |
| 46 | lttv_option_add("process_state", 's', |
| 47 | "write the pid and state for each event", |
| 48 | "", |
| 49 | LTTV_OPT_NONE, &a_state, NULL, NULL); |
| 50 | |
| 51 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event/before", |
| 52 | LTTV_POINTER, &value)); |
| 53 | g_assert((before_event = *(value->v_pointer)) != NULL); |
| 54 | lttv_hooks_add(before_event, write_event_content, NULL); |
| 55 | |
| 56 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/trace/before", |
| 57 | LTTV_POINTER, &value)); |
| 58 | g_assert((before_trace = *(value->v_pointer)) != NULL); |
| 59 | lttv_hooks_add(before_trace, write_trace_header, NULL); |
| 60 | |
| 61 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/before", |
| 62 | LTTV_POINTER, &value)); |
| 63 | g_assert((before_traceset = *(value->v_pointer)) != NULL); |
| 64 | lttv_hooks_add(before_traceset, write_traceset_header, NULL); |
| 65 | |
| 66 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/traceset/after", |
| 67 | LTTV_POINTER, &value)); |
| 68 | g_assert((after_traceset = *(value->v_pointer)) != NULL); |
| 69 | lttv_hooks_add(after_traceset, write_traceset_footer, NULL); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | void destroy() |
| 74 | { |
| 75 | lttv_option_remove("output"); |
| 76 | |
| 77 | lttv_option_remove("field_names"); |
| 78 | |
| 79 | lttv_option_remove("process_state"); |
| 80 | |
| 81 | lttv_hooks_remove(before_event, write_event, NULL); |
| 82 | |
| 83 | lttv_hooks_remove(before_trace, write_trace_header, NULL); |
| 84 | |
| 85 | lttv_hooks_remove(before_trace, write_traceset_header, NULL); |
| 86 | |
| 87 | lttv_hooks_remove(before_trace, write_traceset_footer, NULL); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /* Insert the hooks before and after each trace and tracefile, and for each |
| 92 | event. Print a global header. */ |
| 93 | |
| 94 | static FILE *a_file; |
| 95 | |
| 96 | static GString *a_string; |
| 97 | |
| 98 | static static gboolean write_traceset_header(void *hook_data, void *call_data) |
| 99 | { |
| 100 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 101 | |
| 102 | if(a_file_name == NULL) a_file = stdout; |
| 103 | else a_file = fopen(a_file_name, "w"); |
| 104 | |
| 105 | if(a_file == NULL) g_error("cannot open file %s", a_file_name); |
| 106 | |
| 107 | /* Print the trace set header */ |
| 108 | fprintf(a_file,"Trace set contains %d traces\n\n", |
| 109 | lttv_traceset_number(tc->ta); |
| 110 | |
| 111 | return FALSE; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | static static gboolean write_traceset_footer(void *hook_data, void *call_data) |
| 116 | { |
| 117 | LttvTracesetContext *tc = (LttvTracesetContext *)call_data; |
| 118 | |
| 119 | if(a_file_name != NULL) a_file = fclose(a_file); |
| 120 | |
| 121 | fprintf(a_file,"End trace set\n\n"); |
| 122 | |
| 123 | return FALSE; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static gboolean write_trace_header(void *hook_data, void *call_data) |
| 128 | { |
| 129 | LttvTraceContext *tc = (LttvTraceContext *)call_data; |
| 130 | |
| 131 | LttSystemDescription *system = ltt_trace_system_description(tc->t); |
| 132 | |
| 133 | fprintf(a_file," Trace from %s in %s\n%s\n\n", system->node_name, |
| 134 | system->domain_name, system->description); |
| 135 | return FALSE; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | static int write_event_content(void *hook_data, void *call_data) |
| 140 | { |
| 141 | LttvTracefileContext *tfc = (LttvTracefileContext *)call_data; |
| 142 | |
| 143 | LttvTracefileState *tfs = (LttvTracefileState *)call_data; |
| 144 | |
| 145 | LttEvent *e; |
| 146 | |
| 147 | e = tfc->e; |
| 148 | |
| 149 | lttv_event_to_string(e, tfc->tf, a_string, TRUE, a_field_names); |
| 150 | |
| 151 | if(a_state) { |
| 152 | g_string_append_printf(a_string, " %s", |
| 153 | g_quark_to_string(tfs->process->state->s); |
| 154 | } |
| 155 | |
| 156 | fputs(s, c->fd); |
| 157 | return FALSE; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | void lttv_event_to_string(LttEvent *e, LttTracefile *tf, g_string *s, |
| 162 | gboolean mandatory_fields, gboolean field_names) |
| 163 | { |
| 164 | LttFacility *facility; |
| 165 | |
| 166 | LttEventType *event_type; |
| 167 | |
| 168 | LttType *type; |
| 169 | |
| 170 | LttField *field; |
| 171 | |
| 172 | LttTime time; |
| 173 | |
| 174 | g_string_set_size(s,0); |
| 175 | |
| 176 | facility = lttv_event_facility(e); |
| 177 | eventtype = ltt_event_eventtype(e); |
| 178 | field = ltt_event_field(e); |
| 179 | |
| 180 | if(mandatory_fields) { |
| 181 | time = ltt_event_time(e); |
| 182 | g_string_append_printf(s,"%s.%s: %ld.%ld (%s)",ltt_facility_name(facility), |
| 183 | ltt_eventtype_name(eventtype), (long)time.tv_sec, time.tv_nsec, |
| 184 | ltt_tracefile_name(tf)); |
| 185 | } |
| 186 | |
| 187 | print_field(e,f,s, field_names); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void print_field(LttEvent *e, LttField *f, g_string *s, gboolean field_names) { |
| 192 | |
| 193 | LttType *type; |
| 194 | |
| 195 | LttField *element; |
| 196 | |
| 197 | char *name; |
| 198 | |
| 199 | int nb, i; |
| 200 | |
| 201 | type = ltt_field_type(f); |
| 202 | switch(ltt_type_class(type)) { |
| 203 | case LTT_INT: |
| 204 | g_string_append_printf(s, " %ld", ltt_event_get_long_int(e,f)); |
| 205 | break; |
| 206 | |
| 207 | case LTT_UINT: |
| 208 | g_string_append_printf(s, " %lu", ltt_event_get_long_unsigned(e,f)); |
| 209 | break; |
| 210 | |
| 211 | case LTT_FLOAT: |
| 212 | g_string_append_printf(s, " %g", ltt_event_get_double(e,f)); |
| 213 | break; |
| 214 | |
| 215 | case LTT_STRING: |
| 216 | g_string_append_printf(s, " \"%s\"", ltt_event_get_string(e,f)); |
| 217 | break; |
| 218 | |
| 219 | case LTT_ENUM: |
| 220 | g_string_append_printf(s, " %s", ltt_enum_string_get(type, |
| 221 | event_get_unsigned(e,f)); |
| 222 | break; |
| 223 | |
| 224 | case LTT_ARRAY: |
| 225 | case LTT_SEQUENCE: |
| 226 | g_string_append_printf(s, " {"); |
| 227 | nb = ltt_event_field_element_number(e,f); |
| 228 | element = ltt_field_element(f); |
| 229 | for(i = 0 ; i < nb ; i++) { |
| 230 | ltt_event_field_element_select(e,f,i); |
| 231 | print_field(e,element,s); |
| 232 | } |
| 233 | g_string_append_printf(s, " }"); |
| 234 | break; |
| 235 | |
| 236 | case LTT_STRUCT: |
| 237 | g_string_append_printf(s, " {"); |
| 238 | nb = ltt_type_member_number(type); |
| 239 | for(i = 0 ; i < nb ; i++) { |
| 240 | element = ltt_field_member(f,i); |
| 241 | if(name) { |
| 242 | ltt_type_member_type(type, &name); |
| 243 | g_string_append_printf(s, " %s = ", field_names); |
| 244 | } |
| 245 | print_field(e,element,s); |
| 246 | } |
| 247 | g_string_append_printf(s, " }"); |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | |
| 253 | |
| 254 | |