-
Notifications
You must be signed in to change notification settings - Fork 5
/
atag.c
158 lines (134 loc) · 2.99 KB
/
atag.c
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
/**
* LTTng to GTKwave trace conversion
*
* Authors:
* Ivan Djelic <[email protected]>
* Matthieu Castet <[email protected]>
*
* Copyright (C) 2013 Parrot S.A.
*/
#include "lttng2lxt.h"
#define HASHTAB_SIZE (100000)
#define ADDR2LINE_MAX (128)
#define ADDR_SIZE (12)
int atag_enabled;
static const char *exefile;
static void snprintf_up(char **p, int size, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsnprintf(*p, size, fmt, ap);
va_end(ap);
assert(ret < size);
*p += ret;
}
void atag_init(const char *name)
{
int res;
exefile = name;
if (exefile) {
/* create a new hash table */
res = hcreate(HASHTAB_SIZE);
assert(res);
atag_enabled = 1;
}
}
static void atag_enqueue_addr(uint32_t addr, int flush)
{
static char cmdbuf[2*LINEBUF_MAX + ADDR2LINE_MAX*ADDR_SIZE];
static char funcname[LINEBUF_MAX];
static char basename[LINEBUF_MAX];
static char tag[LINEBUF_MAX];
static int index;
static uint32_t addrbuf[ADDR2LINE_MAX];
char key[16];
ENTRY item, *rentry;
char *ret, *str, *p;
FILE *fp;
int i;
if (!exefile)
return;
if (!flush) {
/* check that this address is not already queued */
for (i = 0; i < index; i++) {
if (addrbuf[i] == addr)
/* already in */
return;
}
addrbuf[index++] = addr;
}
if (!index || ((index < ADDR2LINE_MAX) && !flush))
/* we can stop here */
return;
/* prepare command string */
assert(index <= ADDR2LINE_MAX);
p = cmdbuf;
snprintf_up(&p, LINEBUF_MAX, "addr2line -C -f -s --exe=%s ", exefile);
for (i = 0; i < index; i++)
snprintf_up(&p, ADDR_SIZE, "0x%08x ", (uint32_t)addrbuf[i]);
/* call addr2line now */
fp = popen(cmdbuf, "r");
if (!fp) {
index = 0;
return;
}
for (i = 0; i < index; i++) {
ret = NULL;
if (fgets(funcname, LINEBUF_MAX, fp) &&
fgets(basename, LINEBUF_MAX, fp)) {
/* remove newline characters */
str = strchr(funcname, '\n');
if (str)
*str = '\0';
str = strchr(basename, '\n');
if (str)
*str = '\0';
if (funcname[0] != '?')
snprintf(tag, sizeof(tag), "%s() [%s]",
funcname, basename);
else
snprintf(tag, sizeof(tag), "0x%08x",
addrbuf[i]);
ret = strdup(tag);
}
if (!ret) {
fprintf(stderr, PFX "no symbol for addr 0x%08x: %s\n",
(unsigned int)addrbuf[i], strerror(errno));
continue;
}
/* add entry into hash table */
snprintf(key, sizeof(key), "%08x", addrbuf[i]);
item.key = strdup(key);
assert(item.key);
item.data = (void *)ret;
rentry = hsearch(item, ENTER);
assert(rentry);
}
pclose(fp);
index = 0;
}
char *atag_get(uint32_t addr)
{
char *ret = NULL;
char key[16];
ENTRY item, *fitem;
if (exefile) {
snprintf(key, sizeof(key), "%08x", addr);
/* lookup address info in hash table */
item.key = key;
fitem = hsearch(item, FIND);
if (fitem)
ret = (char *)fitem->data;
}
return ret;
}
void atag_store(uint32_t addr)
{
if (!atag_get(addr))
atag_enqueue_addr(addr, 0);
}
void atag_flush(void)
{
atag_enqueue_addr(0, 1);
}