summaryrefslogtreecommitdiff
path: root/rts/sm/TraceDump.c
blob: 639a6a8a2651bddaea7135eeeba86c0cc4009201 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "Rts.h"
#include "TraceDump.h"
#include "Trace.h"
#include "Printer.h"

#if defined(TRACE_DUMP)
static int gc_n = 0;
static FILE *trace_dump = NULL;
static char current_src[255] = "unknown";

void 
trace_dump_start_gc(void)
{
    trace_dump_end_gc();

    char fname[255];
    snprintf(fname, 255, "trace-dumps/%05d.dot", gc_n);
    trace_dump = fopen(fname, "w");
    if (trace_dump == NULL) abort();
    fprintf(trace_dump, "digraph {\n");

    debugBelch("trace dump: Starting trace %d\n", gc_n);
    //trace(TRACE_gc, "trace dump: Starting trace %d\n", gc_n);
    gc_n++;
}

void 
trace_dump_end_gc(void)
{
    if (trace_dump) {
        fprintf(trace_dump, "}\n");
        fclose(trace_dump);
    }
    trace_dump = NULL;
}

void
trace_dump_set_source(const char *c)
{
    strncpy(current_src, c, sizeof(current_src));
}

void
trace_dump_set_source_closure(StgClosure *c)
{
    c = UNTAG_CLOSURE(c);
    snprintf(current_src, sizeof(current_src), "%p", c);
    if (!trace_dump)
        return;

    const StgInfoTable *info = get_itbl(c);
    const char *type;
    switch ( info->type ) {
    case CONSTR:
    case CONSTR_1_0: case CONSTR_0_1:
    case CONSTR_1_1: case CONSTR_0_2: case CONSTR_2_0:
    case CONSTR_NOCAF:
    {
        const StgConInfoTable *con_info = get_con_itbl (c);
        type = GET_CON_DESC(con_info);
        break;
    }
    default:
        type = closure_type_names[info->type];
    }

    fprintf(trace_dump, "  \"%p\" [label=\"%p\\n%s\" info=\"%p\" type=\"%s\"];\n", 
            UNTAG_CLOSURE(c), UNTAG_CLOSURE(c), type, info, type);
}

void
trace_dump_edge(StgClosure *tgt)
{
    if (!trace_dump)
        return;
    fprintf(trace_dump, "  \"%s\" -> \"%p\";\n", current_src, UNTAG_CLOSURE(tgt));
}

#endif