mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2026-02-12 16:10:54 +00:00
New file, made out of files from libiberty in binutils.
2006-08-20 Soren Sandmann <sandmann@redhat.com> * Makefile.am, demangle.c: New file, made out of files from libiberty in binutils. * binfile.c: Use the new sysprof_cplus_demangle() function * elfparser.[ch]: Add code to lookup symbols. Add demangling function * testelf.c: Various tests
This commit is contained in:
committed by
Søren Sandmann Pedersen
parent
8967b3148c
commit
6c7afad3ff
12
ChangeLog
12
ChangeLog
@ -1,3 +1,15 @@
|
|||||||
|
2006-08-20 Soren Sandmann <sandmann@redhat.com>
|
||||||
|
|
||||||
|
* Makefile.am, demangle.c: New file, made out of files from
|
||||||
|
libiberty in binutils.
|
||||||
|
|
||||||
|
* binfile.c: Use the new sysprof_cplus_demangle() function
|
||||||
|
|
||||||
|
* elfparser.[ch]: Add code to lookup symbols. Add demangling
|
||||||
|
function
|
||||||
|
|
||||||
|
* testelf.c: Various tests
|
||||||
|
|
||||||
2006-08-19 Soren Sandmann <sandmann@redhat.com>
|
2006-08-19 Soren Sandmann <sandmann@redhat.com>
|
||||||
|
|
||||||
* elfparser.c: Many cleanups.
|
* elfparser.c: Many cleanups.
|
||||||
|
|||||||
@ -4,7 +4,7 @@ DIST_SUBDIRS = module
|
|||||||
bin_PROGRAMS = sysprof-text
|
bin_PROGRAMS = sysprof-text
|
||||||
|
|
||||||
noinst_PROGRAMS = testelf
|
noinst_PROGRAMS = testelf
|
||||||
testelf_SOURCES = testelf.c elfparser.c elfparser.h binparser.c binparser.h
|
testelf_SOURCES = testelf.c demangle.c elfparser.c elfparser.h binparser.c binparser.h
|
||||||
testelf_CPPFLAGS = \
|
testelf_CPPFLAGS = \
|
||||||
$(CORE_DEP_CFLAGS)
|
$(CORE_DEP_CFLAGS)
|
||||||
testelf_LDADD = $(CORE_DEP_LIBS)
|
testelf_LDADD = $(CORE_DEP_LIBS)
|
||||||
@ -18,6 +18,7 @@ SYSPROF_CORE = \
|
|||||||
binfile.c \
|
binfile.c \
|
||||||
collector.c \
|
collector.c \
|
||||||
collector.h \
|
collector.h \
|
||||||
|
demangle.c \
|
||||||
process.h \
|
process.h \
|
||||||
process.c \
|
process.c \
|
||||||
profile.h \
|
profile.h \
|
||||||
|
|||||||
@ -267,7 +267,7 @@ slurp_symtab (bfd *abfd, long *symcount)
|
|||||||
return sy;
|
return sy;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern char *cplus_demangle (const char *mangled, int options);
|
extern char *sysprof_cplus_demangle (const char *mangled, int options);
|
||||||
|
|
||||||
#define DMGL_PARAMS (1 << 0) /* Include function args */
|
#define DMGL_PARAMS (1 << 0) /* Include function args */
|
||||||
#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
|
#define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
|
||||||
@ -280,7 +280,7 @@ demangle (bfd *bfd, const char *name)
|
|||||||
if (bfd_get_symbol_leading_char (bfd) == *name)
|
if (bfd_get_symbol_leading_char (bfd) == *name)
|
||||||
++name;
|
++name;
|
||||||
|
|
||||||
demangled = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
|
demangled = sysprof_cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
|
||||||
return demangled ? demangled : g_strdup (name);
|
return demangled ? demangled : g_strdup (name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
9575
demangle.c
Normal file
9575
demangle.c
Normal file
File diff suppressed because it is too large
Load Diff
290
elfparser.c
290
elfparser.c
@ -1,3 +1,4 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <elf.h>
|
#include <elf.h>
|
||||||
#include "binparser.h"
|
#include "binparser.h"
|
||||||
@ -12,6 +13,8 @@ struct SymbolTable
|
|||||||
|
|
||||||
struct ElfSym
|
struct ElfSym
|
||||||
{
|
{
|
||||||
|
const char *name;
|
||||||
|
gulong address;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Section
|
struct Section
|
||||||
@ -32,6 +35,9 @@ struct ElfParser
|
|||||||
|
|
||||||
int n_sections;
|
int n_sections;
|
||||||
Section ** sections;
|
Section ** sections;
|
||||||
|
|
||||||
|
int n_symbols;
|
||||||
|
ElfSym * symbols;
|
||||||
};
|
};
|
||||||
|
|
||||||
static gboolean parse_elf_signature (const guchar *data, gsize length,
|
static gboolean parse_elf_signature (const guchar *data, gsize length,
|
||||||
@ -159,128 +165,22 @@ elf_parser_free (ElfParser *parser)
|
|||||||
g_free (parser);
|
g_free (parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern char *sysprof_cplus_demangle (const char *name, int options);
|
||||||
|
|
||||||
|
char *
|
||||||
|
elf_demangle (const char *name)
|
||||||
|
{
|
||||||
|
char *demangled = sysprof_cplus_demangle (name, 0);
|
||||||
|
|
||||||
|
if (demangled)
|
||||||
|
return demangled;
|
||||||
|
else
|
||||||
|
return g_strdup (name);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Looking up symbols
|
* Looking up symbols
|
||||||
*/
|
*/
|
||||||
#if 0
|
|
||||||
static int lookup_function_symbol (ElfParser *parser,
|
|
||||||
int begin,
|
|
||||||
int end,
|
|
||||||
gulong address);
|
|
||||||
|
|
||||||
static gboolean
|
|
||||||
check_symbol (ElfParser *parser,
|
|
||||||
int index,
|
|
||||||
gulong address)
|
|
||||||
{
|
|
||||||
bin_parser_index (parser->parser, index);
|
|
||||||
|
|
||||||
/* FIXME */
|
|
||||||
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean
|
|
||||||
is_function (ElfParser *parser, int index)
|
|
||||||
{
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static gulong
|
|
||||||
get_address (ElfParser *parser, int index)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
do_check (ElfParser *parser,
|
|
||||||
int begin,
|
|
||||||
int current,
|
|
||||||
int other,
|
|
||||||
int end,
|
|
||||||
gulong address)
|
|
||||||
{
|
|
||||||
int first = current > other ? current : other;
|
|
||||||
int last = current > other ? other : current;
|
|
||||||
|
|
||||||
/* The invariant here is that nothing between first
|
|
||||||
* and last is a function
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (is_function (parser, current))
|
|
||||||
{
|
|
||||||
gulong addr = get_address (parser, current);
|
|
||||||
|
|
||||||
if (addr == address)
|
|
||||||
{
|
|
||||||
return current;
|
|
||||||
}
|
|
||||||
else if (addr > address)
|
|
||||||
{
|
|
||||||
return lookup_function_symbol (
|
|
||||||
parser, begin, first, address);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return lookup_function_symbol (
|
|
||||||
parser, last, end, address);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
lookup_function_symbol (ElfParser *parser,
|
|
||||||
int begin,
|
|
||||||
int end,
|
|
||||||
gulong address)
|
|
||||||
{
|
|
||||||
g_assert (end - begin > 0);
|
|
||||||
|
|
||||||
if (end - begin < 10)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < end - begin; ++i)
|
|
||||||
{
|
|
||||||
bin_parser_index (parser->parser, i);
|
|
||||||
|
|
||||||
if (is_function (parser, i) &&
|
|
||||||
get_address (parser, i == address))
|
|
||||||
{
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int mid1, mid2;
|
|
||||||
|
|
||||||
mid1 = mid2 = (end - begin) / 2;
|
|
||||||
|
|
||||||
while (mid1 >= begin &&
|
|
||||||
mid2 < end)
|
|
||||||
{
|
|
||||||
int res;
|
|
||||||
|
|
||||||
res = do_check (parser, begin, mid1, mid2, end, address);
|
|
||||||
if (res > 0)
|
|
||||||
return res;
|
|
||||||
|
|
||||||
res = do_check (parser, begin, mid2, mid1, end, address);
|
|
||||||
if (res > 0)
|
|
||||||
return res;
|
|
||||||
|
|
||||||
mid1--;
|
|
||||||
mid2++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
#define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4)
|
#define ELF32_ST_BIND(val) (((unsigned char) (val)) >> 4)
|
||||||
#define ELF32_ST_TYPE(val) ((val) & 0xf)
|
#define ELF32_ST_TYPE(val) ((val) & 0xf)
|
||||||
@ -292,61 +192,155 @@ lookup_function_symbol (ElfParser *parser,
|
|||||||
#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type))
|
#define ELF64_ST_INFO(bind, type) ELF32_ST_INFO ((bind), (type))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static ElfSym *
|
static int
|
||||||
lookup_symbol (ElfParser *parser,
|
compare_sym (const void *a, const void *b)
|
||||||
const Section *sym_table,
|
|
||||||
const Section *str_table,
|
|
||||||
gulong address)
|
|
||||||
{
|
{
|
||||||
int n_symbols = sym_table->size / bin_format_get_size (parser->sym_format);
|
const ElfSym *sym_a = a;
|
||||||
int i;
|
const ElfSym *sym_b = b;
|
||||||
|
|
||||||
g_print ("\ndumping %d symbols from %s\n", n_symbols, sym_table->name);
|
if (sym_a->address < sym_b->address)
|
||||||
|
return -1;
|
||||||
|
else if (sym_a->address == sym_b->address)
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
read_table (ElfParser *parser,
|
||||||
|
const Section *sym_table,
|
||||||
|
const Section *str_table)
|
||||||
|
{
|
||||||
|
int sym_size = bin_format_get_size (parser->sym_format);
|
||||||
|
int i;
|
||||||
|
int n_functions;
|
||||||
|
|
||||||
|
parser->n_symbols = sym_table->size / sym_size;
|
||||||
|
parser->symbols = g_new (ElfSym, parser->n_symbols);
|
||||||
|
|
||||||
|
g_print ("\nreading %d symbols from %s\n",
|
||||||
|
parser->n_symbols, sym_table->name);
|
||||||
|
|
||||||
bin_parser_begin (parser->parser, parser->sym_format, sym_table->offset);
|
bin_parser_begin (parser->parser, parser->sym_format, sym_table->offset);
|
||||||
|
|
||||||
for (i = 0; i < n_symbols; ++i)
|
n_functions = 0;
|
||||||
|
for (i = 0; i < parser->n_symbols; ++i)
|
||||||
{
|
{
|
||||||
const char *name;
|
|
||||||
gulong addr;
|
|
||||||
guint info;
|
guint info;
|
||||||
|
gulong addr;
|
||||||
|
const char *name;
|
||||||
|
|
||||||
bin_parser_index (parser->parser, i);
|
bin_parser_index (parser->parser, i);
|
||||||
name = get_string (parser->parser, str_table->offset, "st_name");
|
|
||||||
info = bin_parser_get_uint (parser->parser, "st_info");
|
info = bin_parser_get_uint (parser->parser, "st_info");
|
||||||
addr = bin_parser_get_uint (parser->parser, "st_value");
|
addr = bin_parser_get_uint (parser->parser, "st_value");
|
||||||
|
name = get_string (parser->parser, str_table->offset, "st_name");
|
||||||
|
|
||||||
if ((info & 0xf) == STT_FUNC)
|
if (addr != 0 &&
|
||||||
g_print ("symbol: %8lx, %s\n", addr, name);
|
(info & 0xf) == STT_FUNC &&
|
||||||
|
((info >> 4) == STB_GLOBAL ||
|
||||||
|
(info >> 4) == STB_LOCAL))
|
||||||
|
{
|
||||||
|
parser->symbols[n_functions].name = name;
|
||||||
|
parser->symbols[n_functions].address = addr;
|
||||||
|
|
||||||
|
n_functions++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bin_parser_end (parser->parser);
|
bin_parser_end (parser->parser);
|
||||||
|
|
||||||
return NULL;
|
g_print ("found %d functions\n", n_functions);
|
||||||
|
|
||||||
|
parser->n_symbols = n_functions;
|
||||||
|
parser->symbols = g_renew (ElfSym, parser->symbols, parser->n_symbols);
|
||||||
|
|
||||||
|
qsort (parser->symbols, parser->n_symbols, sizeof (ElfSym), compare_sym);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
read_symbols (ElfParser *parser)
|
||||||
|
{
|
||||||
|
const Section *symtab = find_section (parser, ".symtab");
|
||||||
|
const Section *strtab = find_section (parser, ".strtab");
|
||||||
|
const Section *dynsym = find_section (parser, ".dynsym");
|
||||||
|
const Section *dynstr = find_section (parser, ".dynstr");
|
||||||
|
|
||||||
|
if (symtab && strtab)
|
||||||
|
{
|
||||||
|
read_table (parser, symtab, strtab);
|
||||||
|
}
|
||||||
|
else if (dynsym && dynstr)
|
||||||
|
{
|
||||||
|
read_table (parser, dynsym, dynstr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* To make sure parser->symbols is non-NULL */
|
||||||
|
parser->n_symbols = 0;
|
||||||
|
parser->symbols = g_new (ElfSym, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static ElfSym *
|
||||||
|
do_lookup (ElfSym *symbols, gulong address, int first, int last)
|
||||||
|
{
|
||||||
|
if (address >= symbols[last].address)
|
||||||
|
{
|
||||||
|
return &(symbols[last]);
|
||||||
|
}
|
||||||
|
else if (last - first < 3)
|
||||||
|
{
|
||||||
|
while (last >= first)
|
||||||
|
{
|
||||||
|
if (address >= symbols[last].address)
|
||||||
|
return &(symbols[last]);
|
||||||
|
|
||||||
|
last--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int mid = (first + last) / 2;
|
||||||
|
|
||||||
|
if (symbols[mid].address > address)
|
||||||
|
{
|
||||||
|
return do_lookup (symbols, address, first, mid);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return do_lookup (symbols, address, mid, last);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const ElfSym *
|
const ElfSym *
|
||||||
elf_parser_lookup_symbol (ElfParser *parser,
|
elf_parser_lookup_symbol (ElfParser *parser,
|
||||||
gulong address)
|
gulong address)
|
||||||
{
|
{
|
||||||
const Section *symtab = find_section (parser, ".symtab");
|
if (!parser->symbols)
|
||||||
const Section *dynsym = find_section (parser, ".dynsym");
|
read_symbols (parser);
|
||||||
const Section *strtab = find_section (parser, ".strtab");
|
|
||||||
const Section *dynstr = find_section (parser, ".dynstr");
|
|
||||||
|
|
||||||
if (strtab && symtab)
|
if (parser->n_symbols == 0)
|
||||||
{
|
return NULL;
|
||||||
lookup_symbol (parser, symtab, strtab, address);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dynsym && dynstr)
|
/* FIXME: we should offset address based on the files load address */
|
||||||
{
|
|
||||||
lookup_symbol (parser, dynsym, dynstr, address);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_print ("HELLO!!\n");
|
return do_lookup (parser->symbols, address, 0, parser->n_symbols - 1);
|
||||||
|
}
|
||||||
|
|
||||||
return NULL;
|
const char *
|
||||||
|
elf_sym_get_name (const ElfSym *sym)
|
||||||
|
{
|
||||||
|
return sym->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
gulong
|
||||||
|
elf_sym_get_address (const ElfSym *sym)
|
||||||
|
{
|
||||||
|
return sym->address;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
18
elfparser.h
18
elfparser.h
@ -1,8 +1,26 @@
|
|||||||
|
#include <glib.h>
|
||||||
|
|
||||||
typedef struct ElfSym ElfSym;
|
typedef struct ElfSym ElfSym;
|
||||||
typedef struct ElfParser ElfParser;
|
typedef struct ElfParser ElfParser;
|
||||||
|
|
||||||
ElfParser *elf_parser_new (const guchar *data,
|
ElfParser *elf_parser_new (const guchar *data,
|
||||||
gsize length);
|
gsize length);
|
||||||
|
|
||||||
|
/* Lookup a symbol in the file.
|
||||||
|
*
|
||||||
|
* The symbol returned is const, so don't free it it or anything. It
|
||||||
|
* will be valid until elf_parser_free() is called on the parser.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* The address should be given in "file coordinates". This means that
|
||||||
|
* if the file is mapped at address m and offset o, then an address a
|
||||||
|
* should be looked up as "a - (m - o)". (m - o) is where the start
|
||||||
|
* of the file would have been mapped, so a - (m - o) is the position
|
||||||
|
* in the file of a.
|
||||||
|
*/
|
||||||
const ElfSym *elf_parser_lookup_symbol (ElfParser *parser,
|
const ElfSym *elf_parser_lookup_symbol (ElfParser *parser,
|
||||||
gulong address);
|
gulong address);
|
||||||
|
|
||||||
|
const char *elf_sym_get_name (const ElfSym *sym);
|
||||||
|
gulong elf_sym_get_address (const ElfSym *sym);
|
||||||
|
char *elf_demangle (const char *name);
|
||||||
|
|||||||
37
testelf.c
37
testelf.c
@ -1,24 +1,37 @@
|
|||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include "elfparser.h"
|
#include "elfparser.h"
|
||||||
|
|
||||||
|
static void
|
||||||
|
check (ElfParser *elf, gulong addr)
|
||||||
|
{
|
||||||
|
const ElfSym *sym = elf_parser_lookup_symbol (elf, addr);
|
||||||
|
|
||||||
|
g_print ("%p => ", addr);
|
||||||
|
|
||||||
|
if (sym)
|
||||||
|
{
|
||||||
|
g_print ("found: %s (%p)\n",
|
||||||
|
elf_sym_get_name (sym),
|
||||||
|
elf_sym_get_address (sym));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
g_print ("not found\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main ()
|
main ()
|
||||||
{
|
{
|
||||||
int i;
|
|
||||||
|
|
||||||
GMappedFile *libgtk = g_mapped_file_new ("/usr/lib/libgtk-x11-2.0.so",
|
GMappedFile *libgtk = g_mapped_file_new ("/usr/lib/libgtk-x11-2.0.so",
|
||||||
FALSE, NULL);
|
FALSE, NULL);
|
||||||
|
ElfParser *elf = elf_parser_new (
|
||||||
|
g_mapped_file_get_contents (libgtk),
|
||||||
|
g_mapped_file_get_length (libgtk));
|
||||||
|
|
||||||
|
check (elf, 0x3e7ef20); /* gtk_handle_box_end_drag */
|
||||||
|
check (elf, 0x3e7ef25); /* same */
|
||||||
|
|
||||||
#if 0
|
return 0;
|
||||||
for (i = 0; i < 50000; ++i)
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
ElfParser *elf = elf_parser_new (
|
|
||||||
g_mapped_file_get_contents (libgtk),
|
|
||||||
g_mapped_file_get_length (libgtk));
|
|
||||||
|
|
||||||
elf_parser_lookup_symbol (elf, 1000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user