mirror of
https://github.com/ioacademy-jikim/debugging
synced 2026-08-11 16:32:58 +00:00
first commit
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
#include <execinfo.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
void print_gnu_backtrace(void);
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <execinfo.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void print_gnu_backtrace(void)
|
||||
{
|
||||
void * frame_addrs[16];
|
||||
char** frame_strings;
|
||||
size_t backtrace_size;
|
||||
int i;
|
||||
|
||||
backtrace_size = backtrace( frame_addrs, 16 );
|
||||
frame_strings = backtrace_symbols( frame_addrs, backtrace_size );
|
||||
|
||||
for( i=0; i<backtrace_size; ++i )
|
||||
{
|
||||
printf("%d: [0x%p] %s\n", i, frame_addrs[i], frame_strings[i] );
|
||||
}
|
||||
free( frame_strings );
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct layout
|
||||
{
|
||||
struct layout* ebp;
|
||||
void* ret;
|
||||
} layout;
|
||||
|
||||
void print_gnu_backtrace()
|
||||
{
|
||||
layout* ebp = __builtin_frame_address(0);
|
||||
while( ebp )
|
||||
{
|
||||
printf("0x%08x\n", (unsigned int)ebp->ret );
|
||||
ebp = ebp->ebp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
void **getEBP(int dummy)
|
||||
{
|
||||
void **ebp = (void**)&dummy - 2 ;
|
||||
return (ebp);
|
||||
}
|
||||
void **save_ebp;
|
||||
void print_gnu_backtrace(void)
|
||||
{
|
||||
int dummy;
|
||||
void **ebp = getEBP(dummy);
|
||||
void **ret = *(ebp + 1);
|
||||
ebp = *ebp;
|
||||
while(ebp)
|
||||
{
|
||||
printf("%p\n", ret );
|
||||
ret = *(ebp + 1);
|
||||
ebp = *ebp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdio.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
void **getEBP(int dummy)
|
||||
{
|
||||
void **ebp = (void**)&dummy - 2 ;
|
||||
return ebp;
|
||||
}
|
||||
|
||||
void print_gnu_backtrace(void)
|
||||
{
|
||||
int dummy;
|
||||
int frame = 0;
|
||||
Dl_info dlip;
|
||||
void **ebp = getEBP(dummy);
|
||||
void **ret = *(ebp+1);
|
||||
ebp = *ebp;
|
||||
printf("Stack backtrace_3:\n");
|
||||
|
||||
while(ebp)
|
||||
{
|
||||
if( dladdr(ret, &dlip) == 0 )
|
||||
break;
|
||||
printf("Frame %d : [ebp=%p] [ret=%p] %s\n", frame++, ebp, ret, dlip.dli_sname );
|
||||
ret = *(ebp + 1);
|
||||
ebp = *ebp;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#define UNW_LOCAL_ONLY
|
||||
#define _GNU_SOURCE
|
||||
#include <libunwind.h>
|
||||
#include <dlfcn.h>
|
||||
#include "backtrace.h"
|
||||
|
||||
void print_gnu_backtrace (void) {
|
||||
unw_word_t ip, sp;
|
||||
unw_cursor_t cursor; unw_context_t uc;
|
||||
Dl_info dlip;
|
||||
int frame=0;
|
||||
|
||||
unw_getcontext(&uc);
|
||||
unw_init_local(&cursor, &uc);
|
||||
while (unw_step(&cursor) > 0) {
|
||||
unw_get_reg(&cursor, UNW_REG_IP, &ip);
|
||||
unw_get_reg(&cursor, UNW_REG_SP, &sp);
|
||||
if( dladdr((void*)ip, &dlip) == 0 )
|
||||
break;
|
||||
printf("Frame %d : [ebp=%p] [ret=%p] %s\n", frame++,
|
||||
(void*)sp, (void*)ip, dlip.dli_sname );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#define UNW_LOCAL_ONLY
|
||||
#include <libunwind.h>
|
||||
#include "backtrace.h"
|
||||
|
||||
void print_gnu_backtrace()
|
||||
{
|
||||
unw_cursor_t cursor;
|
||||
unw_context_t context;
|
||||
|
||||
unw_getcontext(&context);
|
||||
unw_init_local(&cursor, &context);
|
||||
|
||||
while (unw_step(&cursor) > 0) {
|
||||
unw_word_t offset, pc;
|
||||
char fname[64];
|
||||
|
||||
unw_get_reg(&cursor, UNW_REG_IP, &pc);
|
||||
|
||||
fname[0] = '\0';
|
||||
(void) unw_get_proc_name(&cursor, fname, sizeof(fname), &offset);
|
||||
|
||||
printf ("%p : (%s+0x%x) [%p]\n", (void*)pc, fname, offset, (void*)pc);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#include "backtrace.h"
|
||||
int foo(void)
|
||||
{
|
||||
print_gnu_backtrace();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bar(void)
|
||||
{
|
||||
foo();
|
||||
return 0;
|
||||
}
|
||||
int boo(void)
|
||||
{
|
||||
bar();
|
||||
return 0;
|
||||
}
|
||||
int baz(void)
|
||||
{
|
||||
boo();
|
||||
return 0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
baz();
|
||||
return 0;
|
||||
}
|
||||
Submodule
+1
Submodule 01_day/backtrace/libunwind added at 8afc33ce9f
Executable
BIN
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,82 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <elf.h>
|
||||
#include <sys/procfs.h>
|
||||
#include <asm/ldt.h>
|
||||
|
||||
static void * read_note(void *addr, void **pdata)
|
||||
{
|
||||
#define ALIGN(pos, size) ((((pos) + (size-1)) / size) * size)
|
||||
|
||||
Elf32_Nhdr *note_hdr = addr;
|
||||
|
||||
/* print note name and type */
|
||||
printf("note: %-8s (0x%03x)\n", (char *)(note_hdr+1), note_hdr->n_type);
|
||||
|
||||
/* calculate data position */
|
||||
*pdata = (void *) ((unsigned long) (note_hdr+1) + ALIGN(note_hdr->n_namesz, 4));
|
||||
|
||||
/* return pointer of next note header */
|
||||
return (void *) ((unsigned long) *pdata + ALIGN(note_hdr->n_descsz, 4));
|
||||
}
|
||||
int main(void)
|
||||
{
|
||||
prstatus_t *status;
|
||||
prpsinfo_t *psinfo;
|
||||
struct elf_siginfo *siginfo;
|
||||
Elf32_auxv_t *auxv;
|
||||
struct user_desc *ldt;
|
||||
void *files;
|
||||
|
||||
FILE *fp;
|
||||
Elf32_Ehdr elf_hdr;
|
||||
Elf32_Phdr pgrm_hdr;
|
||||
char buf[4096];
|
||||
void *note;
|
||||
|
||||
fp = fopen("core", "r");
|
||||
|
||||
/* read ELF header */
|
||||
fread(&elf_hdr, sizeof(elf_hdr), 1, fp);
|
||||
|
||||
/* seek to first program header (for NOTE) */
|
||||
fseek(fp, elf_hdr.e_phoff, SEEK_SET);
|
||||
|
||||
/* read program header */
|
||||
fread(&pgrm_hdr, sizeof(pgrm_hdr), 1, fp);
|
||||
|
||||
/* seek to note */
|
||||
fseek(fp, pgrm_hdr.p_offset, SEEK_SET);
|
||||
fread(buf, pgrm_hdr.p_filesz, 1, fp);
|
||||
|
||||
note = buf;
|
||||
note = read_note(note, (void **) &status);
|
||||
note = read_note(note, (void **) &psinfo);
|
||||
note = read_note(note, (void **) &siginfo);
|
||||
note = read_note(note, (void **) &auxv);
|
||||
note = read_note(note, (void **) &files);
|
||||
note = read_note(note, (void **) &ldt);
|
||||
|
||||
#define EAX 6
|
||||
#define EIP 12
|
||||
|
||||
printf("program name = %s\n", psinfo->pr_fname);
|
||||
printf("signo = %d\n", status->pr_info.si_signo);
|
||||
printf("EIP = 0x%08lx\nEAX = 0x%08lx\n", status->pr_reg[EIP], status->pr_reg[EAX]);
|
||||
|
||||
/* print auxiliary vector */
|
||||
for ( ; auxv->a_type != AT_NULL; auxv++) {
|
||||
if (auxv->a_type == AT_BASE)
|
||||
printf("address of dynamic loader: %p\n", (void *) auxv->a_un.a_val);
|
||||
if (auxv->a_type == AT_SYSINFO_EHDR)
|
||||
printf("address of vdso: %p\n", (void *) auxv->a_un.a_val);
|
||||
}
|
||||
|
||||
printf("gdt entry: %u, base: 0x%08x, limit: 0x%08lx\n",
|
||||
ldt->entry_number, ldt->base_addr & ~0xfff,
|
||||
ldt->limit_in_pages ? ldt->limit * PAGE_SIZE : ldt->limit);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
getchar();
|
||||
*(int *) 0 = 1;
|
||||
return 0;
|
||||
}
|
||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,17 @@
|
||||
#include <stdio.h>
|
||||
void bar(int a)
|
||||
{
|
||||
printf("bar(), &a=%p\n", &a);
|
||||
}
|
||||
void foo()
|
||||
{
|
||||
bar(1);
|
||||
// printf("foo()\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
foo();
|
||||
//printf("main()\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user