Files
sysprof/binfile.c
Soren Sandmann a1bddd8d9f Pass map->bin_file to bin_symbol_get_name.
2006-08-21  Soren Sandmann <sandmann@redhat.com>

	* process.c (process_lookup_symbol): Pass map->bin_file to
	bin_symbol_get_name.

	* binfile.h (bin_symbol_get_name): Add BinFile parameter

	* elfparser.c (elf_demangle): Use options DMGL_PARAMS | DMGL_ANSI

	* TODO: Updates

	* binfile.c: Rewrite this file to use ElfParser instead of libbfd.

	* configure.ac: Drop dependencies on libbfd and libiberty.
2006-08-21 06:00:57 +00:00

186 lines
4.0 KiB
C

/* MemProf -- memory profiler and leak detector
* Copyright 1999, 2000, 2001, Red Hat, Inc.
* Copyright 2002, Kristian Rietveld
*
* Sysprof -- Sampling, systemwide CPU profiler
* Copyright 2004, 2005, Soeren Sandmann
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* Most interesting code in this file is lifted from bfdutils.c
* and process.c from Memprof,
*/
#include <glib.h>
#include "binfile.h"
#include <bfd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "elfparser.h"
struct BinFile
{
ElfParser *elf;
ino_t inode;
char *undefined_name;
};
/* FIXME: error handling */
static ino_t
read_inode (const char *filename)
{
struct stat statbuf;
stat (filename, &statbuf);
return statbuf.st_ino;
}
static ElfParser *
separate_debug_file_exists (const char *name, guint32 crc)
{
guint32 file_crc;
ElfParser *parser = elf_parser_new_from_file (name, NULL);
if (!parser)
return NULL;
file_crc = elf_parser_get_crc32 (parser);
if (file_crc != crc)
{
g_print ("warning: %s has wrong crc\n", name);
elf_parser_free (parser);
return NULL;
}
return parser;
}
/* FIXME - not10: this should probably be detected by config.h -- find out what gdb does*/
static const char *const debug_file_directory = "/usr/lib/debug";
static ElfParser *
find_separate_debug_file (ElfParser *elf,
const char *filename)
{
const char *basename;
char *dir;
guint32 crc32;
char *tries[3];
int i;
ElfParser *result;
if (!elf)
return NULL;
basename = elf_parser_get_debug_link (elf, &crc32);
if (!basename)
return elf;
dir = g_path_get_dirname (filename);
tries[0] = g_build_filename (dir, basename, NULL);
tries[1] = g_build_filename (dir, ".debug", NULL);
tries[2] = g_build_filename (debug_file_directory, dir, basename, NULL);
for (i = 0; i < 3; ++i)
{
result = separate_debug_file_exists (tries[i], crc32);
if (result)
break;
}
g_free (dir);
for (i = 0; i < 3; ++i)
g_free (tries[i]);
if (result)
{
elf_parser_free (elf);
return result;
}
else
{
return elf;
}
}
BinFile *
bin_file_new (const char *filename)
{
/* FIXME: should be able to return an error */
BinFile *bf;
bf = g_new0 (BinFile, 1);
bf->elf = elf_parser_new_from_file (filename, NULL);
bf->elf = find_separate_debug_file (bf->elf, filename);
bf->inode = read_inode (filename);
bf->undefined_name = g_strdup_printf ("In file %s", filename);
return bf;
}
void
bin_file_free (BinFile *bin_file)
{
if (bin_file->elf)
elf_parser_free (bin_file->elf);
g_free (bin_file->undefined_name);
g_free (bin_file);
}
const BinSymbol *
bin_file_lookup_symbol (BinFile *bin_file,
gulong address)
{
if (bin_file->elf)
{
const ElfSym *sym = elf_parser_lookup_symbol (bin_file->elf, address);
if (sym)
return (const BinSymbol *)sym;
}
return (const BinSymbol *)bin_file->undefined_name;
}
ino_t
bin_file_get_inode (BinFile *bin_file)
{
return bin_file->inode;
}
const char *
bin_symbol_get_name (BinFile *file, const BinSymbol *symbol)
{
if (file->undefined_name == (char *)symbol)
return file->undefined_name;
else
return elf_sym_get_name ((const ElfSym *)symbol);
}