Add tags

Add tags to the following comment
__builtin_return_address() と libbfd の bfd_find_nearest_line() を使ってみました。 % gcc -g caller.c -lbfd でコンパイルできます。
 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
#include <stdio.h>
#include <stdlib.h>
#include <bfd.h>

static bfd *abfd;
static int size;
static asymbol **symbols;
static asection *section;

const char *foo()
{
    int ret;
    void *addr;
    const char *file;
    const char *func;
    unsigned int line;

    addr = __builtin_return_address(0);
    ret = bfd_find_nearest_line(abfd, section, symbols, (long)addr,
                                &file, &func, &line);
    return func;
}

const char *bar()
{
    return foo();
}

const char *baz()
{
    return foo();
}

int main()
{
    abfd = bfd_openr("/proc/self/exe", NULL);
    bfd_check_format(abfd, bfd_object);
    size = bfd_get_symtab_upper_bound(abfd);
    symbols = (asymbol**)malloc(size);
    section = bfd_get_section_by_name(abfd, ".debug_info");
    printf("%s\n", bar());
    printf("%s\n", baz());
    free(symbols);
    return 0;
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...