Make process_lookup_symbol() return a string. Delete

2006-08-20  Soren Sandmann  <sandmann@daimi.au.dk>

	* process.h: Make process_lookup_symbol() return a string. Delete
	process_lookup_symbol_with_filename().

	* process.c (struct Process): Make "undefined" a string
	(lookup_kernel_symbol): Return a string
	(process_lookup_symbol): Return a string, not a symbol

	* collector.c (unique_dup): Take a string instead of a
	Symbol.
	(lookup_symbol): Make sym a string instead of a Symbol.

	* binfile.c (bin_symbol_get_name): New function.

	* binfile.[ch] (struct BinSymbol): Rename Symbol to BinSymbol and
	make it an opaque structure.

	* binfile.[ch]: Remove unused symbol operations
This commit is contained in:
Soren Sandmann
2006-08-21 02:33:38 +00:00
committed by Søren Sandmann Pedersen
parent 7d8f3c232d
commit a880ee7f60
6 changed files with 67 additions and 105 deletions

View File

@ -1,3 +1,23 @@
2006-08-20 Soren Sandmann <sandmann@daimi.au.dk>
* process.h: Make process_lookup_symbol() return a string. Delete
process_lookup_symbol_with_filename().
* process.c (struct Process): Make "undefined" a string
(lookup_kernel_symbol): Return a string
(process_lookup_symbol): Return a string, not a symbol
* collector.c (unique_dup): Take a string instead of a
Symbol.
(lookup_symbol): Make sym a string instead of a Symbol.
* binfile.c (bin_symbol_get_name): New function.
* binfile.[ch] (struct BinSymbol): Rename Symbol to BinSymbol and
make it an opaque structure.
* binfile.[ch]: Remove unused symbol operations
2006-08-20 Soren Sandmann <sandmann@daimi.au.dk> 2006-08-20 Soren Sandmann <sandmann@daimi.au.dk>
* elfparser.c (elf_parser_new_from_file): New function * elfparser.c (elf_parser_new_from_file): New function

View File

@ -38,13 +38,20 @@
static void bfd_nonfatal (const char *string); static void bfd_nonfatal (const char *string);
static void bfd_fatal (const char *string); static void bfd_fatal (const char *string);
/* Symbol */
struct BinSymbol
{
char * name;
gulong address;
};
/* Binary File */ /* Binary File */
struct BinFile struct BinFile
{ {
char * filename; char * filename;
int n_symbols; int n_symbols;
Symbol *symbols; BinSymbol *symbols;
Symbol undefined; BinSymbol undefined;
int ref_count; int ref_count;
ino_t inode; ino_t inode;
}; };
@ -287,8 +294,8 @@ demangle (bfd *bfd, const char *name)
static gint static gint
compare_address (const void *a, const void *b) compare_address (const void *a, const void *b)
{ {
const Symbol *symbol1 = a; const BinSymbol *symbol1 = a;
const Symbol *symbol2 = b; const BinSymbol *symbol2 = b;
if (symbol1->address < symbol2->address) if (symbol1->address < symbol2->address)
return -1; return -1;
@ -373,13 +380,13 @@ read_symbols (BinFile *bf)
if (!text_section) if (!text_section)
return; return;
symbols = g_array_new (FALSE, FALSE, sizeof (Symbol)); symbols = g_array_new (FALSE, FALSE, sizeof (BinSymbol));
/* g_print ("%s: text vma: %x\n", bf->filename, text_section->vma); */ /* g_print ("%s: text vma: %x\n", bf->filename, text_section->vma); */
for (i = 0; i < n_symbols; i++) for (i = 0; i < n_symbols; i++)
{ {
Symbol symbol; BinSymbol symbol;
if ((bfd_symbols[i]->flags & BSF_FUNCTION) && if ((bfd_symbols[i]->flags & BSF_FUNCTION) &&
(bfd_symbols[i]->section == text_section)) (bfd_symbols[i]->section == text_section))
@ -405,10 +412,10 @@ read_symbols (BinFile *bf)
#endif #endif
/* Sort the symbols by address */ /* Sort the symbols by address */
qsort (symbols->data, symbols->len, sizeof(Symbol), compare_address); qsort (symbols->data, symbols->len, sizeof (BinSymbol), compare_address);
bf->n_symbols = symbols->len; bf->n_symbols = symbols->len;
bf->symbols = (Symbol *)g_array_free (symbols, FALSE); bf->symbols = (BinSymbol *)g_array_free (symbols, FALSE);
} }
static GHashTable *bin_files; static GHashTable *bin_files;
@ -476,15 +483,15 @@ bin_file_free (BinFile *bf)
* Look up a symbol. @address should be in file coordinates * Look up a symbol. @address should be in file coordinates
* *
**/ **/
const Symbol * const BinSymbol *
bin_file_lookup_symbol (BinFile *bf, bin_file_lookup_symbol (BinFile *bf,
gulong address) gulong address)
{ {
int first = 0; int first = 0;
int last = bf->n_symbols - 1; int last = bf->n_symbols - 1;
int middle = last; int middle = last;
Symbol *data; BinSymbol *data;
Symbol *result; BinSymbol *result;
if (!bf->symbols || (bf->n_symbols == 0)) if (!bf->symbols || (bf->n_symbols == 0))
return &(bf->undefined); return &(bf->undefined);
@ -539,61 +546,10 @@ bin_file_lookup_symbol (BinFile *bf,
return result; return result;
} }
const char *
/* Symbol */ bin_symbol_get_name (const BinSymbol *symbol)
Symbol *
symbol_copy (const Symbol *orig)
{ {
Symbol *copy; return symbol->name;
copy = g_new (Symbol, 1);
copy->name = g_strdup (orig->name);
copy->address = orig->address;
return copy;
}
gboolean
symbol_equal (const void *sa,
const void *sb)
{
const Symbol *syma = sa;
const Symbol *symb = sb;
if (symb->address != syma->address)
return FALSE;
/* symbols compare equal if their names are both NULL */
if (!syma->name && !symb->name)
return TRUE;
if (!syma)
return FALSE;
if (!symb)
return FALSE;
return strcmp (syma->name, symb->name) == 0;
}
guint
symbol_hash (const void *s)
{
const Symbol *symbol = s;
if (!s)
return 0;
return symbol->name? g_str_hash (symbol->name) : 0 + symbol->address;
}
void
symbol_free (Symbol *symbol)
{
if (symbol->name)
g_free (symbol->name);
g_free (symbol);
} }
static void static void

View File

@ -28,27 +28,17 @@
#include <sys/types.h> #include <sys/types.h>
typedef struct BinFile BinFile; typedef struct BinFile BinFile;
typedef struct Symbol Symbol; typedef struct BinSymbol BinSymbol;
/* Binary File */ /* Binary File */
BinFile * bin_file_new (const char *filename); BinFile * bin_file_new (const char *filename);
void bin_file_free (BinFile *bin_file); void bin_file_free (BinFile *bin_file);
const Symbol *bin_file_lookup_symbol (BinFile *bin_file, const BinSymbol *bin_file_lookup_symbol (BinFile *bin_file,
gulong address); gulong address);
ino_t bin_file_get_inode (BinFile *bin_file); ino_t bin_file_get_inode (BinFile *bin_file);
/* Symbol */
struct Symbol
{
char * name;
gulong address;
};
Symbol * symbol_copy (const Symbol *orig); const char * bin_symbol_get_name (const BinSymbol *symbol);
gboolean symbol_equal (const void *syma,
const void *symb);
guint symbol_hash (const void *sym);
void symbol_free (Symbol *symbol);
#endif #endif

View File

@ -265,15 +265,15 @@ typedef struct
} ResolveInfo; } ResolveInfo;
static char * static char *
unique_dup (GHashTable *unique_symbols, char *s) unique_dup (GHashTable *unique_symbols, const char *sym)
{ {
char *result; char *result;
result = g_hash_table_lookup (unique_symbols, s); result = g_hash_table_lookup (unique_symbols, sym);
if (!result) if (!result)
{ {
result = g_strdup (s); result = g_strdup (sym);
g_hash_table_insert (unique_symbols, s, result); g_hash_table_insert (unique_symbols, (char *)sym, result);
} }
return result; return result;
@ -282,9 +282,9 @@ unique_dup (GHashTable *unique_symbols, char *s)
static char * static char *
lookup_symbol (Process *process, gpointer address, GHashTable *unique_symbols) lookup_symbol (Process *process, gpointer address, GHashTable *unique_symbols)
{ {
const Symbol *s = process_lookup_symbol (process, (gulong)address); const char *sym = process_lookup_symbol (process, (gulong)address);
return unique_dup (unique_symbols, s->name); return unique_dup (unique_symbols, sym);
} }
static void static void

View File

@ -57,8 +57,8 @@ struct Process
GList *bad_pages; GList *bad_pages;
int pid; int pid;
Symbol undefined; const char *undefined;
}; };
static void static void
@ -152,8 +152,7 @@ create_process (const char *cmdline, int pid)
p->bad_pages = NULL; p->bad_pages = NULL;
p->maps = NULL; p->maps = NULL;
p->pid = pid; p->pid = pid;
p->undefined.name = NULL; p->undefined = NULL;
p->undefined.address = 0x00;
g_assert (!g_hash_table_lookup (processes_by_pid, GINT_TO_POINTER (pid))); g_assert (!g_hash_table_lookup (processes_by_pid, GINT_TO_POINTER (pid)));
g_assert (!g_hash_table_lookup (processes_by_cmdline, cmdline)); g_assert (!g_hash_table_lookup (processes_by_cmdline, cmdline));
@ -532,24 +531,27 @@ get_kernel_symbols (void)
return NULL; return NULL;
} }
static const Symbol * static const char *
lookup_kernel_symbol (gulong address) lookup_kernel_symbol (gulong address)
{ {
static Symbol kernel; static const char *const kernel = "In kernel";
#if 0 #if 0
g_print ("kernel binary: %s\n", find_kernel_binary ()); g_print ("kernel binary: %s\n", find_kernel_binary ());
#endif #endif
return kernel; /* Can we just return "In kernel"? */
#if 0
kernel.name = "In kernel"; kernel.name = "In kernel";
kernel.address = 0x00001337; kernel.address = 0x00001337;
return &kernel; return &kernel;
#endif
} }
const Symbol * const char *
process_lookup_symbol (Process *process, gulong address) process_lookup_symbol (Process *process, gulong address)
{ {
const Symbol *result; const BinSymbol *result;
Map *map = process_locate_map (process, address); Map *map = process_locate_map (process, address);
/* g_print ("addr: %x\n", address); */ /* g_print ("addr: %x\n", address); */
@ -562,14 +564,13 @@ process_lookup_symbol (Process *process, gulong address)
} }
else if (!map) else if (!map)
{ {
if (!process->undefined.name) if (!process->undefined)
{ {
process->undefined.name = process->undefined =
g_strdup_printf ("(??? %s)", process->cmdline); g_strdup_printf ("No map (%s)", process->cmdline);
process->undefined.address = 0xBABE0001;
} }
return &process->undefined; return process->undefined;
} }
address -= map->start; address -= map->start;
@ -599,7 +600,7 @@ process_lookup_symbol (Process *process, gulong address)
/* g_print ("(%x) %x %x name; %s\n", address, map->start, map->offset, result->name); */ /* g_print ("(%x) %x %x name; %s\n", address, map->start, map->offset, result->name); */
return result; return bin_symbol_get_name (result);
} }
const char * const char *

View File

@ -51,12 +51,7 @@ Process * process_get_from_pid (int pid);
void process_ensure_map (Process *process, void process_ensure_map (Process *process,
int pid, int pid,
gulong address); gulong address);
const Symbol *process_lookup_symbol (Process *process, const char * process_lookup_symbol (Process *process,
gulong address);
const Symbol *process_lookup_symbol_with_filename (Process *process,
int pid,
gulong map_start,
const char *filename,
gulong address); gulong address);
const char * process_get_cmdline (Process *process); const char * process_get_cmdline (Process *process);
void process_flush_caches (void); void process_flush_caches (void);