all repos — tint2 @ 6605a1c3c286d0eec6f8a3971931fc58cccb3c36

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

src/tracing.c (raw)

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "timer.h"

#ifdef HAVE_TRACING

#ifdef ENABLE_EXECINFO
#include <execinfo.h>
#endif
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define GREEN "\033[1;32m"
#define YELLOW "\033[1;33m"
#define RED "\033[1;31m"
#define BLUE "\033[1;34m"
#define RESET "\033[0m"

static GList *tracing_events = NULL;
static sig_atomic_t tracing = FALSE;

typedef struct TracingEvent {
    void *address;
    void *caller;
    double time;
    gboolean enter;
} TracingEvent;

void __attribute__ ((constructor)) init_tracing()
{
    tracing_events = NULL;
    tracing = FALSE;
}

void cleanup_tracing()
{
    g_list_free_full(tracing_events, free);
    tracing_events = NULL;
    tracing = FALSE;
}

char *addr2name(void *func)
{
#ifdef ENABLE_EXECINFO
    void *array[1];
    array[0] = func;
    char **strings = backtrace_symbols(array, 1);
    char *result = strdup(strings[0] ? strings[0] : "??");
    free(strings);
    return result;
#else
    char *result = (char*) calloc(32, 1);
    sprintf(result, "%p", func);
    return result;
#endif
}

void add_tracing_event(void *func, void *caller, gboolean enter)
{
    TracingEvent *entry = (TracingEvent *)calloc(sizeof(TracingEvent), 1);
    entry->address = func;
    entry->caller = caller;
    entry->time = get_time();
    entry->enter = enter;
    tracing_events = g_list_append(tracing_events, entry);
}

void start_tracing(void *root)
{
    if (tracing_events)
        cleanup_tracing();
    add_tracing_event(root, NULL, TRUE);
    tracing = TRUE;
}

void stop_tracing()
{
    tracing = FALSE;
}

void __cyg_profile_func_enter(void *func, void *caller)
{
    if (tracing)
        add_tracing_event(func, caller, TRUE);
}

void __cyg_profile_func_exit(void *func, void *caller)
{
    if (tracing)
        add_tracing_event(func, caller, FALSE);
}

void print_tracing_events()
{
    GList *stack = NULL;
    int depth = 0;
    double now = get_time();
    for (GList *i = tracing_events; i; i = i->next) {
        TracingEvent *e = (TracingEvent *)i->data;
        if (e->enter) {
            // Push a new function on the stack
            for (int d = 0; d < depth; d++)
                fprintf(stderr, " ");
            char *name = addr2name(e->address);
            char *caller = addr2name(e->caller);
            fprintf(stderr,
                    "%s called from %s\n",
                    name,
                    caller);
            stack = g_list_append(stack, e);
            depth++;
        } else {
            // Pop a function from the stack, if matching, and print
            if (stack) {
                TracingEvent *old = (TracingEvent *)g_list_last(stack)->data;
                if (old->address == e->address) {
                    depth--;
                    for (int d = 0; d < depth; d++)
                        fprintf(stderr, " ");
                    char *name = addr2name(e->address);
                    double duration = (e->time - old->time) * 1.0e3;
                    fprintf(stderr,
                            "-- %s exited after %.1f ms",
                            name,
                            duration);
                    if (duration >= 1.0) {
                        fprintf(stderr, YELLOW " ");
                        for (int d = 0; d < duration; d++) {
                            fprintf(stderr, "#");
                        }
                        fprintf(stderr, RESET);
                    }
                    fprintf(stderr, "\n");
                    free(name);
                    stack = g_list_delete_link(stack, g_list_last(stack));
                }
            }
        }
    }
    while (stack) {
        TracingEvent *old = (TracingEvent *)g_list_last(stack)->data;
        depth--;
        for (int d = 0; d < depth; d++)
            fprintf(stderr, " ");
        char *name = addr2name(old->address);
        double duration = (now - old->time) * 1.0e3;
        fprintf(stderr,
                "-- %s exited after %.1f ms",
                name,
                duration);
        if (duration >= 1.0) {
            fprintf(stderr, YELLOW " ");
            for (int d = 0; d < duration; d++) {
                fprintf(stderr, "#");
            }
            fprintf(stderr, RESET);
        }
        fprintf(stderr, "\n");
        free(name);
        stack = g_list_delete_link(stack, g_list_last(stack));
    }
}

#endif