19 #pragma GCC visibility push(hidden)
23 #include "libiberty.h"
24 #pragma GCC visibility pop
39 .base_names = TRUE, .syms =
nullptr, .abfd =
nullptr, .section =
nullptr, };
55 static int translate_addresses (bfd *abfd, asection *section,
void *addr,
char *buf_func,
size_t buf_func_len,
56 char *buf_file,
size_t buf_file_len);
62 const char *errmsg = bfd_errmsg(bfd_get_error());
65 fprintf(stderr,
"%s: %s: %s\n",
program_name,
string, errmsg);
73 vfprintf(stderr, format, args);
81 va_start(args, format);
93 if (stat(file_name, &statbuf) < 0) {
95 non_fatal(
"'%s': No such file", file_name);
97 non_fatal(
"Warning: could not locate '%s'. reason: %s", file_name, strerror(errno));
98 }
else if (!S_ISREG (statbuf.st_mode))
99 non_fatal(
"Warning: '%s' is not an ordinary file", file_name);
101 return statbuf.st_size;
114 fprintf(stderr,
"%s: Matching formats: ",
program_name);
116 fprintf(stderr,
" %s", *p++);
128 const char *target = TARGET;
130 if (!bfd_set_default_target(target)) {
131 non_fatal(
"can't set BFD default target to `%s': %s", target, bfd_errmsg(bfd_get_error()));
144 if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
147 symcount = bfd_read_minisymbols(abfd, FALSE, (
void *) &m_libtrace_data.
syms, &size);
149 symcount = bfd_read_minisymbols(abfd, TRUE , (
void *) &m_libtrace_data.
syms, &size);
170 if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
173 vma = bfd_get_section_vma(abfd, section);
177 size = bfd_get_section_size(section);
178 if (psi->
pc >= vma + size)
181 psi->
found = bfd_find_nearest_line(abfd, section, m_libtrace_data.
syms, psi->
pc - vma, &psi->
filename,
193 if ((bfd_get_section_flags(abfd, section) & SEC_ALLOC) == 0)
196 size = bfd_get_section_size(section);
200 psi->
found = bfd_find_nearest_line(abfd, section, m_libtrace_data.
syms, psi->
pc, &psi->
filename,
206 static int translate_addresses (bfd *abfd, asection *section,
void *xaddr,
char *buf_func,
size_t buf_func_len,
207 char *buf_file,
size_t buf_file_len)
209 #define ADDR_BUF_LEN ((CHAR_BIT/4)*(sizeof(void*))+1)
213 sprintf(addr,
"%p", xaddr);
214 si.
pc = bfd_scan_vma(addr,
nullptr, 16);
223 if (buf_func !=
nullptr)
224 snprintf(buf_func, buf_func_len,
"%s ??:0", m_libtrace_data.
with_functions ?
"??" :
"");
229 char *alloc =
nullptr;
232 if (name ==
nullptr || *name ==
'\0')
235 alloc = bfd_demangle(abfd, name, DMGL_ANSI | DMGL_PARAMS);
236 if (alloc !=
nullptr)
240 if (buf_func !=
nullptr)
241 snprintf(buf_func, buf_func_len,
"%s", name);
247 char *h = strrchr(si.
filename,
'/');
252 if (buf_file !=
nullptr)
265 int libtrace_init (
const char *file_name,
const char *section_name,
const char *target)
267 char **matching =
nullptr;
275 m_libtrace_data.
abfd = bfd_openr(file_name, target);
276 if (m_libtrace_data.
abfd ==
nullptr) {
281 if (bfd_check_format(m_libtrace_data.
abfd, bfd_archive)) {
282 non_fatal(
"%s: cannot get addresses from archive", file_name);
286 if (!bfd_check_format_matches(m_libtrace_data.
abfd, bfd_object, &matching)) {
288 if (bfd_get_error() == bfd_error_file_ambiguously_recognized) {
295 if (section_name !=
nullptr) {
296 m_libtrace_data.
section = bfd_get_section_by_name(m_libtrace_data.
abfd, section_name);
297 if (m_libtrace_data.
section ==
nullptr)
298 non_fatal(
"%s: cannot find section %s", file_name, section_name);
300 m_libtrace_data.
section =
nullptr;
310 free(m_libtrace_data.
syms);
311 m_libtrace_data.
syms =
nullptr;
313 bfd_close(m_libtrace_data.
abfd);
318 int libtrace_resolve (
void *addr,
char *buf_func,
size_t buf_func_len,
char *buf_file,
size_t buf_file_len, ...)
333 fprintf(stream,
"Usage: %s image addr <addr...>\n",
program_name);
334 fprintf(stream,
"Ex: %s cpptraced 0x8048f0e 0x8048fd4 \n",
program_name);
346 int main (
int argc,
char **argv)
348 const char *file_name =
nullptr;
349 const char *section_name =
nullptr;
350 char *target =
nullptr;
352 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
353 setlocale (LC_MESSAGES,
"");
355 #if defined (HAVE_SETLOCALE)
356 setlocale (LC_CTYPE,
"");
368 #define FUNC_MAX (PATH_MAX)
372 char func[FUNC_MAX + 1] = { 0 };
373 char file[PATH_MAX + 1] = { 0 };
375 for (i = argc - 2; i < argc; i++) {
376 sscanf(argv[i],
"%p", &sym);
378 printf(
"%s [%s]\n", func, file);
static int slurp_symtab(bfd *)
static libtrace_data m_libtrace_data
int libtrace_resolve(void *addr, char *buf_func, size_t buf_func_len, char *buf_file, size_t buf_file_len,...)
const char * functionname
void bfd_nonfatal(const char *string)
int main(int argc, char **argv)
The entry point for linux server and client. Initializes the program and calls Qcommon_Frame in an in...
bfd_boolean with_functions
void report(const char *format, va_list args)
static int translate_addresses(bfd *abfd, asection *section, void *addr, char *buf_func, size_t buf_func_len, char *buf_file, size_t buf_file_len)
int libtrace_init(const char *file_name, const char *section_name, const char *target)
struct _libtrace_data libtrace_data
off_t get_file_size(const char *file_name)
struct _sym_info sym_info
void list_matching_formats(char **p)
static const char * program_name
void set_default_bfd_target(void)
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
GLsizei const GLvoid GLenum usage
static void find_address_in_section(bfd *, asection *, void *)
GLsizei const GLvoid * data
static void find_offset_in_section(bfd *, asection *, sym_info *)
bfd_boolean unwind_inlines
void non_fatal(const char *format,...)
void format(__printf__, 1, 2)))