Add some experimental (and unused) code to look up kernel symbols.

2005-11-13  Soeren Sandmann  <sandmann@redhat.com>

        * process.c: Add some experimental (and unused) code to look up
        kernel symbols.
This commit is contained in:
Soeren Sandmann
2005-11-14 02:44:15 +00:00
committed by Søren Sandmann Pedersen
parent b6e9105cd2
commit 7414ddd5db
2 changed files with 196 additions and 35 deletions

View File

@ -1,3 +1,8 @@
2005-11-13 Soeren Sandmann <sandmann@redhat.com>
* process.c: Add some experimental (and unused) code to look up
kernel symbols.
Sat Nov 12 23:39:29 2005 Soeren Sandmann <sandmann@redhat.com> Sat Nov 12 23:39:29 2005 Soeren Sandmann <sandmann@redhat.com>
* sysprof.c (set_sizes): Put the vertical splitter at 3/8 * * sysprof.c (set_sizes): Put the vertical splitter at 3/8 *

168
process.c
View File

@ -343,20 +343,176 @@ process_get_from_pid (int pid)
return p; return p;
} }
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
static gboolean
file_exists (const char *name)
{
int fd;
fd = open (name, O_RDONLY);
g_print ("trying: %s\n", name);
if (fd > 0)
{
close (fd);
return TRUE;
}
return FALSE;
}
static gchar *
look_for_vmlinux (void)
{
struct utsname utsname;
char *result;
GList *names;
GList *list;
uname (&utsname);
names = NULL;
names = g_list_prepend (
names, g_strdup_printf (
"/usr/lib/debug/lib/modules/%s/vmlinux", utsname.release));
names = g_list_prepend (
names, g_strdup_printf (
"/boot/vmlinux-%s", utsname.release));
result = NULL;
for (list = names; list != NULL; list = list->next)
{
char *name = list->data;
if (file_exists (name))
{
result = g_strdup (name);
break;
}
}
g_list_foreach (names, (GFunc)g_free, NULL);
g_list_free (names);
return result;
}
static const gchar *
find_kernel_binary (void)
{
static gboolean looked_for_vmlinux;
static gchar *binary = NULL;
if (!looked_for_vmlinux)
{
binary = look_for_vmlinux ();
looked_for_vmlinux = TRUE;
}
return binary;
}
static void
parse_kallsym_line (const char *line,
GHashTable *table)
{
char **tokens = g_strsplit_set (line, " \t", -1);
if (tokens[0] && tokens[1] && tokens[2])
{
glong address;
char *endptr;
address = strtoul (tokens[0], &endptr, 16);
if (*endptr == '\0')
{
g_hash_table_insert (
table, GUINT_TO_POINTER (address), g_strdup (tokens[2]));
}
}
g_strfreev (tokens);
}
static void
parse_kallsyms (const char *kallsyms,
GHashTable *table)
{
const char *sol;
const char *eol;
sol = kallsyms;
eol = strchr (sol, '\n');
while (eol)
{
char *line = g_strndup (sol, eol - sol);
parse_kallsym_line (line, table);
g_free (line);
sol = eol + 1;
eol = strchr (sol, '\n');
}
}
static GHashTable *
get_kernel_symbols (void)
{
static GHashTable *kernel_syms;
if (!kernel_syms)
{
char *kallsyms;
g_file_get_contents ("/proc/kallsyms", &kallsyms, NULL, NULL);
if (kallsyms)
{
kernel_syms = g_hash_table_new_full (g_direct_hash, g_direct_equal,
NULL, g_free);
parse_kallsyms (kallsyms, kernel_syms);
g_free (kallsyms);
g_hash_table_destroy (kernel_syms);
}
}
return NULL;
}
static const Symbol *
lookup_kernel_symbol (gulong address)
{
static Symbol kernel;
#if 0
g_print ("kernel binary: %s\n", find_kernel_binary ());
#endif
kernel.name = "In kernel";
kernel.address = 0x00001337;
return &kernel;
}
const Symbol * const Symbol *
process_lookup_symbol (Process *process, gulong address) process_lookup_symbol (Process *process, gulong address)
{ {
const Symbol *result; const Symbol *result;
static Symbol kernel;
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); */
if (address == 0x1) if (address == 0x1)
{ {
kernel.name = "In kernel"; return lookup_kernel_symbol (address);
kernel.address = 0x00001337;
return &kernel;
} }
else if (!map) else if (!map)
{ {
@ -379,10 +535,10 @@ process_lookup_symbol (Process *process, gulong address)
/* g_print ("%s: start: %p, load: %p\n", */ /* g_print ("%s: start: %p, load: %p\n", */
/* map->filename, map->start, bin_file_get_load_address (map->bin_file)); */ /* map->filename, map->start, bin_file_get_load_address (map->bin_file)); */
result = bin_file_lookup_symbol (map->bin_file, address); result = bin_file_lookup_symbol (map->bin_file, address);
#if 0 #if 0
g_print (" ---> %s\n", result->name); g_print (" ---> %s\n", result->name);
#endif #endif
/* 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); */