Compilation errors are done. Some makefile.am fixes are required for linking
[lttv.git] / ltt / branches / poly / lttv / state.c
1
2 #include <lttv/state.h>
3 #include <ltt/facility.h>
4 #include <ltt/trace.h>
5
6 LttvInterruptType
7 LTTV_STATE_USER_MODE,
8 LTTV_STATE_SYSCALL,
9 LTTV_STATE_TRAP,
10 LTTV_STATE_IRQ;
11
12
13 LttvProcessStatus
14 LTTV_STATE_UNNAMED,
15 LTTV_STATE_WAIT_FORK,
16 LTTV_STATE_WAIT_CPU,
17 LTTV_STATE_EXIT,
18 LTTV_STATE_WAIT,
19 LTTV_STATE_RUN;
20
21 static GQuark
22 LTTV_STATE_HOOKS;
23
24 void remove_all_processes(GHashTable *processes);
25
26
27 static void
28 init(LttvTracesetState *self, LttvTraceset *ts)
29 {
30 guint i, j, nb_trace, nb_tracefile;
31
32 LttvTraceContext *tc;
33
34 LttvTraceState *tcs;
35
36 LttvTracefileContext *tfc;
37
38 LttvTracefileState *tfcs;
39
40 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek_parent(LTTV_TRACESET_STATE_GET_CLASS(self)))->init((LttvTracesetContext *)self, ts);
41
42 nb_trace = lttv_traceset_number(ts);
43 for(i = 0 ; i < nb_trace ; i++) {
44 tcs = (LttvTraceState *)tc = (LTTV_TRACESET_CONTEXT(self)->traces[i]);
45 tcs->processes = g_hash_table_new(g_direct_hash, g_direct_equal);
46
47 nb_tracefile = ltt_trace_control_tracefile_number(tc->t);
48 for(j = 0 ; j < nb_tracefile ; j++) {
49 tfcs = (LttvTracefileState *)tfc = tc->control_tracefiles[j];
50 tfcs->process = NULL;
51 }
52
53 nb_tracefile = ltt_trace_per_cpu_tracefile_number(tc->t);
54 for(j = 0 ; j < nb_tracefile ; j++) {
55 tfcs = (LttvTracefileState *)tfc = tc->per_cpu_tracefiles[j];
56 tfcs->process = NULL;
57 }
58 }
59 }
60
61
62 static void
63 fini(LttvTracesetState *self)
64 {
65 guint i, j, nb_trace, nb_tracefile;
66
67 LttvTraceState *tcs;
68
69 LttvTracefileState *tfcs;
70
71 nb_trace = lttv_traceset_number(LTTV_TRACESET_CONTEXT(self)->ts);
72 for(i = 0 ; i < nb_trace ; i++) {
73 tcs = (LttvTraceState *)(LTTV_TRACESET_CONTEXT(self)->traces[i]);
74 remove_all_processes(tcs->processes);
75 g_hash_table_destroy(tcs->processes);
76 }
77 LTTV_TRACESET_CONTEXT_CLASS(g_type_class_peek_parent(LTTV_TRACESET_STATE_GET_CLASS(self)))->fini((LttvTracesetContext *)self);
78 }
79
80
81 LttvTracesetContext *
82 new_traceset_context(LttvTracesetContext *self)
83 {
84 return LTTV_TRACESET_CONTEXT(g_object_new(LTTV_TRACESET_STATE_TYPE, NULL));
85 }
86
87
88 LttvTraceContext *
89 new_trace_context(LttvTracesetContext *self)
90 {
91 return LTTV_TRACE_CONTEXT(g_object_new(LTTV_TRACE_STATE_TYPE, NULL));
92 }
93
94
95 LttvTracefileContext *
96 new_tracefile_context(LttvTracesetContext *self)
97 {
98 return LTTV_TRACEFILE_CONTEXT(g_object_new(LTTV_TRACEFILE_STATE_TYPE, NULL));
99 }
100
101
102 static void
103 traceset_state_instance_init (GTypeInstance *instance, gpointer g_class)
104 {
105 }
106
107
108 static void
109 traceset_state_finalize (LttvTracesetContext *self)
110 {
111 G_OBJECT_CLASS(g_type_class_peek_parent(LTTV_TRACESET_STATE_GET_CLASS(self)))->finalize(G_OBJECT(self));
112 }
113
114
115 static void
116 traceset_state_class_init (LttvTracesetContextClass *klass)
117 {
118 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
119
120 gobject_class->finalize = (void (*)(GObject *self)) traceset_state_finalize;
121 klass->init = (void (*)(LttvTracesetContext *self, LttvTraceset *ts))init;
122 klass->fini = (void (*)(LttvTracesetContext *self))fini;
123 klass->new_traceset_context = new_traceset_context;
124 klass->new_trace_context = new_trace_context;
125 klass->new_tracefile_context = new_tracefile_context;
126 }
127
128
129 GType
130 lttv_traceset_state_get_type(void)
131 {
132 static GType type = 0;
133 if (type == 0) {
134 static const GTypeInfo info = {
135 sizeof (LttvTracesetStateClass),
136 NULL, /* base_init */
137 NULL, /* base_finalize */
138 (GClassInitFunc) traceset_state_class_init, /* class_init */
139 NULL, /* class_finalize */
140 NULL, /* class_data */
141 sizeof (LttvTracesetContext),
142 0, /* n_preallocs */
143 (GInstanceInitFunc) traceset_state_instance_init /* instance_init */
144 };
145
146 type = g_type_register_static (LTTV_TRACESET_CONTEXT_TYPE, "LttvTracesetStateType",
147 &info, 0);
148 }
149 return type;
150 }
151
152
153 static void
154 trace_state_instance_init (GTypeInstance *instance, gpointer g_class)
155 {
156 }
157
158
159 static void
160 trace_state_finalize (LttvTraceContext *self)
161 {
162 G_OBJECT_CLASS(g_type_class_peek_parent(LTTV_TRACE_STATE_GET_CLASS(self)))->finalize(G_OBJECT(self));
163 }
164
165
166 static void
167 trace_state_class_init (LttvTraceContextClass *klass)
168 {
169 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
170
171 gobject_class->finalize = (void (*)(GObject *self)) trace_state_finalize;
172 }
173
174
175 GType
176 lttv_trace_state_get_type(void)
177 {
178 static GType type = 0;
179 if (type == 0) {
180 static const GTypeInfo info = {
181 sizeof (LttvTraceStateClass),
182 NULL, /* base_init */
183 NULL, /* base_finalize */
184 (GClassInitFunc) trace_state_class_init, /* class_init */
185 NULL, /* class_finalize */
186 NULL, /* class_data */
187 sizeof (LttvTracesetState),
188 0, /* n_preallocs */
189 (GInstanceInitFunc) trace_state_instance_init /* instance_init */
190 };
191
192 type = g_type_register_static (LTTV_TRACE_CONTEXT_TYPE,
193 "LttvTraceStateType", &info, 0);
194 }
195 return type;
196 }
197
198
199 static void
200 tracefile_state_instance_init (GTypeInstance *instance, gpointer g_class)
201 {
202 }
203
204
205 static void
206 tracefile_state_finalize (LttvTracefileState *self)
207 {
208 G_OBJECT_CLASS(g_type_class_peek_parent(LTTV_TRACEFILE_STATE_GET_CLASS(self)))->finalize(G_OBJECT(self));
209 }
210
211
212 static void
213 tracefile_state_class_init (LttvTracefileStateClass *klass)
214 {
215 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
216
217 gobject_class->finalize = (void (*)(GObject *self)) tracefile_state_finalize;
218 }
219
220
221 GType
222 lttv_tracefile_state_get_type(void)
223 {
224 static GType type = 0;
225 if (type == 0) {
226 static const GTypeInfo info = {
227 sizeof (LttvTracefileStateClass),
228 NULL, /* base_init */
229 NULL, /* base_finalize */
230 (GClassInitFunc) tracefile_state_class_init, /* class_init */
231 NULL, /* class_finalize */
232 NULL, /* class_data */
233 sizeof (LttvTracefileState),
234 0, /* n_preallocs */
235 (GInstanceInitFunc) tracefile_state_instance_init /* instance_init */
236 };
237
238 type = g_type_register_static (LTTV_TRACEFILE_CONTEXT_TYPE,
239 "LttvTracefileStateType", &info, 0);
240 }
241 return type;
242 }
243
244
245 struct HookData {
246 LttField *f1;
247 LttField *f2;
248 LttField *f3;
249 };
250
251
252 struct HookId {
253 LttvHook h;
254 guint id;
255 void *hook_data;
256 gboolean free_hook_data;
257 };
258
259
260 static void push_state(LttvTracefileState *tfs, LttvInterruptType t,
261 guint state_id)
262 {
263 LttvInterruptState *intr;
264
265 LttvProcessState *process = tfs->process;
266
267 guint depth = process->interrupt_stack->len;
268
269 g_array_set_size(process->interrupt_stack, depth + 1);
270 intr = &g_array_index(process->interrupt_stack, LttvInterruptState, depth);
271 intr->t = t;
272 intr->n = state_id;
273 intr->entry = intr->last_change = LTTV_TRACEFILE_CONTEXT(tfs)->timestamp;
274 intr->s = process->state->s;
275 }
276
277
278 static void pop_state(LttvTracefileState *tfs, LttvInterruptType t)
279 {
280 LttvProcessState *process = tfs->process;
281
282 guint depth = process->interrupt_stack->len - 1;
283
284 g_assert(process->state->t == t);
285 g_array_remove_index(process->interrupt_stack, depth);
286 depth--;
287 process->state = &g_array_index(process->interrupt_stack, LttvInterruptState,
288 depth);
289 }
290
291
292 LttvProcessState *create_process(LttvTracefileState *tfs,
293 LttvProcessState *parent, guint pid)
294 {
295 LttvProcessState *process = g_new(LttvProcessState, 1);
296
297 LttvInterruptState *intr;
298
299 LttvTraceContext *tc;
300
301 LttvTraceState *tcs;
302
303 LttvTracefileContext *tfc = LTTV_TRACEFILE_CONTEXT(tfs);
304
305 tcs = (LttvTraceState *)tc = tfc->t_context;
306
307 g_hash_table_insert(tcs->processes, GUINT_TO_POINTER(pid), process);
308 process->pid = pid;
309 process->birth = tfc->timestamp;
310 process->name = LTTV_STATE_UNNAMED;
311 process->interrupt_stack = g_array_new(FALSE, FALSE,
312 sizeof(LttvInterruptState));
313 g_array_set_size(process->interrupt_stack, 1);
314 intr = process->state = &g_array_index(process->interrupt_stack,
315 LttvInterruptState, 0);
316 intr->t = LTTV_STATE_USER_MODE;
317 intr->n = 0;
318 intr->entry = tfc->timestamp;
319 intr->last_change = tfc->timestamp;
320 intr->s = LTTV_STATE_WAIT_FORK;
321 }
322
323
324 LttvProcessState *find_process(LttvTracefileState *tfs, guint pid)
325 {
326 LttvTraceState *ts =(LttvTraceState *)LTTV_TRACEFILE_CONTEXT(tfs)->t_context;
327 LttvProcessState *process = g_hash_table_lookup(ts->processes,
328 GUINT_TO_POINTER(pid));
329 if(process == NULL) process = create_process(tfs, NULL, pid);
330 return process;
331 }
332
333
334 void exit_process(LttvTracefileState *tfs, LttvProcessState *process)
335 {
336 LttvTraceState *ts = LTTV_TRACE_STATE(tfs->parent.t_context);
337
338 g_hash_table_remove(ts->processes, GUINT_TO_POINTER(process->pid));
339 g_array_free(process->interrupt_stack, TRUE);
340 g_free(process);
341 }
342
343
344 void free_process_state(gpointer key, gpointer value, gpointer user_data)
345 {
346 g_array_free(((LttvProcessState *)value)->interrupt_stack, TRUE);
347 g_free(value);
348 }
349
350
351 void remove_all_processes(GHashTable *processes)
352 {
353 g_hash_table_foreach(processes, free_process_state, NULL);
354 }
355
356
357 gboolean syscall_entry(void *hook_data, void *call_data)
358 {
359 LttField *f = (LttField *)hook_data;
360
361 LttvTracefileState *s = (LttvTracefileState *)call_data;
362
363 push_state(s, LTTV_STATE_SYSCALL, ltt_event_get_unsigned(
364 LTTV_TRACEFILE_CONTEXT(s)->e, f));
365 return FALSE;
366 }
367
368
369 gboolean syscall_exit(void *hook_data, void *call_data)
370 {
371 LttvTracefileState *s = (LttvTracefileState *)call_data;
372
373 pop_state(s, LTTV_STATE_SYSCALL);
374 return FALSE;
375 }
376
377
378 gboolean trap_entry(void *hook_data, void *call_data)
379 {
380 LttField *f = (LttField *)hook_data;
381
382 LttvTracefileState *s = (LttvTracefileState *)call_data;
383
384 push_state(s, LTTV_STATE_TRAP, ltt_event_get_unsigned(s->parent.e, f));
385 return FALSE;
386 }
387
388
389 gboolean trap_exit(void *hook_data, void *call_data)
390 {
391 LttvTracefileState *s = (LttvTracefileState *)call_data;
392
393 pop_state(s, LTTV_STATE_TRAP);
394 return FALSE;
395 }
396
397
398 gboolean irq_entry(void *hook_data, void *call_data)
399 {
400 LttField *f = (LttField *)hook_data;
401
402 LttvTracefileState *s = (LttvTracefileState *)call_data;
403
404 /* Do something with the info about being in user or system mode when int? */
405 push_state(s, LTTV_STATE_IRQ, ltt_event_get_unsigned(s->parent.e, f));
406 return FALSE;
407 }
408
409
410 gboolean irq_exit(void *hook_data, void *call_data)
411 {
412 LttvTracefileState *s = (LttvTracefileState *)call_data;
413
414 pop_state(s, LTTV_STATE_IRQ);
415 return FALSE;
416 }
417
418
419 gboolean schedchange(void *hook_data, void *call_data)
420 {
421 struct HookData *h = (struct HookData *)hook_data;
422
423 LttvTracefileState *s = (LttvTracefileState *)call_data;
424
425 guint pid_in, pid_out, state_out;
426
427 pid_in = ltt_event_get_int(s->parent.e, h->f1);
428 pid_out = ltt_event_get_int(s->parent.e, h->f2);
429 state_out = ltt_event_get_int(s->parent.e, h->f3);
430 if(s->process != NULL) {
431 if(state_out == 0) s->process->state->s = LTTV_STATE_WAIT_CPU;
432 else if(s->process->state->s == LTTV_STATE_EXIT)
433 exit_process(s, s->process);
434 else s->process->state->s = LTTV_STATE_WAIT;
435 }
436 s->process = find_process(s, pid_in);
437 s->process->state->s = LTTV_STATE_RUN;
438 return FALSE;
439 }
440
441
442 gboolean process_fork(void *hook_data, void *call_data)
443 {
444 LttField *f = (LttField *)hook_data;
445
446 LttvTracefileState *s = (LttvTracefileState *)call_data;
447
448 guint child_pid;
449
450 child_pid = ltt_event_get_int(s->parent.e, f);
451 create_process(s, s->process, child_pid);
452 return FALSE;
453 }
454
455
456 gboolean process_exit(void *hook_data, void *call_data)
457 {
458 LttvTracefileState *s = (LttvTracefileState *)call_data;
459
460 if(s->process != NULL) {
461 s->process->state->s = LTTV_STATE_EXIT;
462 }
463 return FALSE;
464 }
465
466
467 static LttField *
468 find_field(LttEventType *et, const char *field)
469 {
470 LttType *t;
471
472 LttField *f;
473
474 guint i, nb;
475
476 char *name;
477
478 if(field == NULL) return NULL;
479
480 f = ltt_eventtype_field(et);
481 t = ltt_eventtype_type(et);
482 g_assert(ltt_type_class(t) == LTT_STRUCT);
483 nb = ltt_type_member_number(t);
484 for(i = 0 ; i < nb ; i++) {
485 ltt_type_member_type(t, i, &name);
486 if(strcmp(name, field) == 0) break;
487 }
488 g_assert(i < nb);
489 return ltt_field_member(f, i);
490 }
491
492
493 static struct HookId
494 find_hook(LttTrace *t, char *facility, char *event,
495 char *field1, char *field2, char *field3, LttvHook h)
496 {
497 LttFacility *f;
498
499 LttEventType *et;
500
501 guint nb, pos, i;
502
503 struct HookId hook_id;
504
505 struct HookData hook_data, *phook_data;
506
507 char *name;
508
509 nb = ltt_trace_facility_find(t, facility, &pos);
510 if(nb < 1) g_error("No %s facility", facility);
511 f = ltt_trace_facility_get(t, pos);
512 et = ltt_facility_eventtype_get_by_name(f, event);
513 if(et == NULL) g_error("Event %s does not exist", event);
514
515 hook_id.id = *(ltt_eventtype_id(et)); /* CHECK */
516 hook_id.h = h;
517 hook_id.free_hook_data = FALSE;
518 hook_data.f1 = find_field(et, field1);
519 hook_data.f2 = find_field(et, field2);
520 hook_data.f3 = find_field(et, field3);
521 if(hook_data.f1 == NULL) hook_id.hook_data = NULL;
522 else if(hook_data.f2 == NULL) hook_id.hook_data = hook_data.f1;
523 else {
524 phook_data = g_new(struct HookData, 1);
525 *phook_data = hook_data;
526 hook_id.hook_data = phook_data;
527 hook_id.free_hook_data = TRUE;
528 }
529 return hook_id;
530 }
531
532
533 lttv_state_add_event_hooks(LttvTracesetState *self)
534 {
535 LttvTraceset *traceset = self->parent.ts;
536
537 guint i, j, k, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
538
539 LttFacility *f;
540
541 LttEventType *et;
542
543 LttvTraceState *ts;
544
545 LttvTracefileState *tfs;
546
547 void *hook_data;
548
549 GArray *hooks;
550
551 struct HookId hook_id;
552
553 LttvAttributeValue val;
554
555 nb_trace = lttv_traceset_number(traceset);
556 for(i = 0 ; i < nb_trace ; i++) {
557 ts = (LttvTraceState *)self->parent.traces[i];
558
559 /* Find the eventtype id for the following events and register the
560 associated by id hooks. */
561
562 hooks = g_array_new(FALSE, FALSE, sizeof(struct HookId));
563 g_array_add(hooks, find_hook(ts->parent.t, "core","syscall_entry",
564 "syscall_id", NULL, NULL, syscall_entry));
565 g_array_add(hooks, find_hook(ts->parent.t, "core", "syscall_exit",
566 NULL, NULL, NULL, syscall_exit));
567 g_array_add(hooks, find_hook(ts->parent.t, "core", "trap_entry", "trap_id",
568 NULL, NULL, trap_entry));
569 g_array_add(hooks, find_hook(ts->parent.t, "core", "trap_exit", NULL, NULL,
570 NULL, trap_exit));
571 g_array_add(hooks, find_hook(ts->parent.t, "core", "irq_entry", "irq_id",
572 NULL, NULL, irq_entry));
573 g_array_add(hooks, find_hook(ts->parent.t, "core", "irq_exit", NULL, NULL,
574 NULL, irq_exit));
575 g_array_add(hooks, find_hook(ts->parent.t, "core", "schedchange",
576 "in", "out", "out_state", schedchange));
577 g_array_add(hooks, find_hook(ts->parent.t, "core", "process_fork",
578 "child_pid", NULL, NULL, process_fork));
579 g_array_add(hooks, find_hook(ts->parent.t, "core", "process_exit",
580 NULL, NULL, NULL, process_exit));
581
582 /* Add these hooks to each before_event_by_id hooks list */
583
584 nb_control = ltt_trace_control_tracefile_number(ts->parent.t);
585 nb_per_cpu = ltt_trace_control_tracefile_number(ts->parent.t);
586 nb_tracefile = nb_control + nb_per_cpu;
587 for(j = 0 ; j < nb_tracefile ; j++) {
588 if(j < nb_control) {
589 tfs = LTTV_TRACEFILE_STATE(ts->parent.control_tracefiles[j]);
590 }
591 else {
592 tfs = LTTV_TRACEFILE_STATE(ts->parent.per_cpu_tracefiles[j]);
593 }
594
595 for(k = 0 ; k < hooks->len ; k++) {
596 hook_id = g_array_index(hooks, struct HookId, k);
597 lttv_hooks_add(lttv_hooks_by_id_find(tfs->parent.before_event_by_id,
598 hook_id.id), hook_id.h, hook_id.hook_data);
599 }
600 }
601 lttv_attribute_find(self->parent.a, LTTV_STATE_HOOKS, LTTV_POINTER, &val);
602 *(val.v_pointer) = hooks;
603 }
604 }
605
606
607 lttv_state_remove_event_hooks(LttvTracesetState *self)
608 {
609 LttvTraceset *traceset = self->parent.ts;
610
611 guint i, j, k, nb_trace, nb_control, nb_per_cpu, nb_tracefile;
612
613 LttvTraceState *ts;
614
615 LttvTracefileState *tfs;
616
617 void *hook_data;
618
619 GArray *hooks;
620
621 struct HookId hook_id;
622
623 LttvAttributeValue val;
624
625 nb_trace = lttv_traceset_number(traceset);
626 for(i = 0 ; i < nb_trace ; i++) {
627 ts = LTTV_TRACE_STATE(self->parent.traces[i]);
628 lttv_attribute_find(self->parent.a, LTTV_STATE_HOOKS, LTTV_POINTER, &val);
629 hooks = *(val.v_pointer);
630
631 /* Add these hooks to each before_event_by_id hooks list */
632
633 nb_control = ltt_trace_control_tracefile_number(ts->parent.t);
634 nb_per_cpu = ltt_trace_control_tracefile_number(ts->parent.t);
635 nb_tracefile = nb_control + nb_per_cpu;
636 for(j = 0 ; j < nb_tracefile ; j++) {
637 if(j < nb_control) {
638 tfs = LTTV_TRACEFILE_STATE(ts->parent.control_tracefiles[j]);
639 }
640 else {
641 tfs = LTTV_TRACEFILE_STATE(ts->parent.per_cpu_tracefiles[j]);
642 }
643
644 for(k = 0 ; k < hooks->len ; k++) {
645 hook_id = g_array_index(hooks, struct HookId, k);
646 lttv_hooks_remove_data(
647 lttv_hooks_by_id_find(tfs->parent.before_event_by_id,
648 hook_id.id), hook_id.h, hook_id.hook_data);
649
650 }
651 for(k = 0 ; k < hooks->len ; k++) {
652 hook_id = g_array_index(hooks, struct HookId, k);
653 if(hook_id.free_hook_data) g_free(hook_id.hook_data);
654 }
655 }
656 g_array_free(hooks, TRUE);
657 }
658 }
659
660
661 void lttv_state_init(int argc, char **argv)
662 {
663 LTTV_STATE_UNNAMED = g_quark_from_string("unnamed");
664 LTTV_STATE_USER_MODE = g_quark_from_string("user mode");
665 LTTV_STATE_WAIT_FORK = g_quark_from_string("wait fork");
666 LTTV_STATE_SYSCALL = g_quark_from_string("system call");
667 LTTV_STATE_TRAP = g_quark_from_string("trap");
668 LTTV_STATE_IRQ = g_quark_from_string("irq");
669 LTTV_STATE_WAIT_CPU = g_quark_from_string("wait for cpu");
670 LTTV_STATE_EXIT = g_quark_from_string("exiting");
671 LTTV_STATE_WAIT = g_quark_from_string("wait for I/O");
672 LTTV_STATE_RUN = g_quark_from_string("running");
673 LTTV_STATE_HOOKS = g_quark_from_string("saved state hooks");
674 }
675
676 void lttv_state_destroy()
677 {
678 }
679
680
681
682
This page took 0.043494 seconds and 4 git commands to generate.