MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / init_symtab

Function init_symtab

db/memdebug.c:206–268  ·  view source on GitHub ↗

this gets run pre-init, so it can use the default system allocators */

Source from the content-addressed store, hash-verified

204
205/* this gets run pre-init, so it can use the default system allocators */
206static void init_symtab(void) {
207 FILE *f;
208 char cmd[1024];
209 char *prog;
210 char *symfile;
211 struct symbol *s;
212 intptr_t addr;
213 int sz;
214 int allocsyms = 0;
215
216 /* There's no good way to get at this that I know of. get_progname_by_pid
217 is the best I could fine, but it doesn't work for longer paths. */
218 symfile = getenv("MEMDEBUG_SYMBOLS");
219 if (symfile == NULL) {
220 prog = getenv("MEMDEBUG_TASKNAME");
221 /* kludgeit */
222 if (prog == NULL)
223 prog = "/proc/self/exe";
224
225 /* on sun... */
226#ifdef __sun
227 snprintf(cmd, sizeof(cmd), "nm -ev '%s' | grep FUNC | awk -F\\| '{ print $2,$3,$8 }'", prog);
228#else
229 snprintf(cmd, sizeof(cmd), "nm -dPp '%s' | grep ' [Tt] ' | egrep -v '\\.bf|\\.ef|\\.eb|\\.bb' | awk '{ print $3,$4,$1 }'", prog);
230#endif
231 f = popen(cmd, "r");
232 }
233 else {
234 f = fopen(symfile, "r");
235 }
236
237 if (f == NULL) {
238 logmsg(LOGMSG_WARN, "Can't open symbol file\n");
239 }
240
241 while (fgets(cmd, sizeof(cmd), f)) {
242 char *tok;
243 tok = strtok(cmd, " \t\n");
244 if (tok == NULL)
245 continue;
246 addr = (intptr_t) strtoul(tok, NULL, 10);
247 tok = strtok(NULL, " \t\n");
248 if (tok == NULL)
249 continue;
250 sz = (int) strtol(tok, NULL, 10);
251 tok = strtok(NULL, " \t\t");
252 if (tok == NULL)
253 continue;
254 if (numsyms >= allocsyms) {
255 if (symtab)
256 symtab = dl_realloc(symtab, sizeof(struct symbol) * (numsyms + 1000));
257 else
258 symtab = dl_malloc(sizeof(struct symbol) * (numsyms + 1000));
259 allocsyms = numsyms + 1000;
260 }
261 symtab[numsyms].addr = addr;
262 symtab[numsyms].sz = sz;
263 symtab[numsyms].sym = strdup(tok);

Callers 1

initFunction · 0.85

Calls 5

dl_reallocFunction · 0.85
dl_mallocFunction · 0.85
strdupFunction · 0.70
chompFunction · 0.70
logmsgFunction · 0.50

Tested by

no test coverage detected