| Juan Cespedes | 1c2be91 | 1997-06-09 01:12:01 +0200 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <errno.h> |
| 3 | #include <unistd.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/ptrace.h> |
| 6 | #include <sys/resource.h> |
| 7 | #include <sys/wait.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <linux/elf.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <string.h> |
| 13 | |
| 14 | u_long strtab; |
| 15 | u_long symtab; |
| 16 | u_long symtab_len; |
| 17 | |
| 18 | int read_elf(char *filename) |
| 19 | { |
| 20 | struct stat sbuf; |
| 21 | int fd; |
| 22 | void * addr; |
| 23 | struct elf32_hdr * hdr; |
| 24 | Elf32_Shdr * shdr; |
| 25 | int i; |
| 26 | |
| 27 | strtab = symtab = symtab_len = 0; |
| 28 | |
| 29 | fd = open(filename, O_RDONLY); |
| 30 | if (fd==-1) { |
| 31 | fprintf(stderr, "Can't open \"%s\": %s\n", filename, sys_errlist[errno]); |
| 32 | exit(1); |
| 33 | } |
| 34 | if (fstat(fd, &sbuf)==-1) { |
| 35 | fprintf(stderr, "Can't stat \"%s\": %s\n", filename, sys_errlist[errno]); |
| 36 | exit(1); |
| 37 | } |
| 38 | if (sbuf.st_size < sizeof(struct elf32_hdr)) { |
| 39 | fprintf(stderr, "\"%s\" is not an ELF object\n", filename); |
| 40 | exit(1); |
| 41 | } |
| 42 | addr = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 43 | if (addr==(void*)-1) { |
| 44 | fprintf(stderr, "Can't mmap \"%s\": %s\n", filename, sys_errlist[errno]); |
| 45 | exit(1); |
| 46 | } |
| 47 | hdr = addr; |
| 48 | if (strncmp(hdr->e_ident, ELFMAG, SELFMAG)) { |
| 49 | fprintf(stderr, "\"%s\" is not an ELF object\n", filename); |
| 50 | exit(1); |
| 51 | } |
| 52 | for(i=0; i<hdr->e_shnum; i++) { |
| 53 | shdr = addr + hdr->e_shoff + i*hdr->e_shentsize; |
| 54 | if (shdr->sh_type == SHT_DYNSYM) { |
| 55 | if (!symtab) { |
| 56 | symtab = shdr->sh_addr; |
| 57 | symtab_len = shdr->sh_size; |
| 58 | } |
| 59 | } |
| 60 | if (shdr->sh_type == SHT_STRTAB) { |
| 61 | if (!strtab) { |
| 62 | strtab = shdr->sh_addr; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | fprintf(stderr, "symtab: 0x%08lx\n", symtab); |
| 67 | fprintf(stderr, "symtab_len: %lu\n", symtab_len); |
| 68 | fprintf(stderr, "strtab: 0x%08lx\n", strtab); |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int main(int argc, char **argv) |
| 73 | { |
| 74 | int pid; |
| 75 | int status; |
| 76 | struct rusage ru; |
| 77 | |
| 78 | if (argc<2) { |
| 79 | fprintf(stderr, "Usage: %s <program> [<arguments>]\n", argv[0]); |
| 80 | exit(1); |
| 81 | } |
| 82 | read_elf(argv[1]); |
| 83 | if (!symtab) { |
| 84 | fprintf(stderr, "%s: Not dynamically linked\n", argv[0]); |
| 85 | exit(1); |
| 86 | } |
| 87 | pid = fork(); |
| 88 | if (pid<0) { |
| 89 | perror("fork"); |
| 90 | exit(1); |
| 91 | } else if (!pid) { |
| 92 | if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) { |
| 93 | perror("PTRACE_TRACEME"); |
| 94 | exit(1); |
| 95 | } |
| 96 | execvp(argv[1], argv+1); |
| 97 | fprintf(stderr, "Can't execute \"%s\": %s\n", argv[1], sys_errlist[errno]); |
| 98 | exit(1); |
| 99 | } |
| 100 | fprintf(stderr, "pid %u attached\n", pid); |
| 101 | while(1) { |
| 102 | pid = wait4(-1, &status, 0, &ru); |
| 103 | if (pid==-1) { |
| 104 | if (errno == ECHILD) { |
| 105 | fprintf(stderr, "No more children\n"); |
| 106 | exit(0); |
| 107 | } |
| 108 | perror("wait4"); |
| 109 | exit(1); |
| 110 | } |
| 111 | if (WIFEXITED(status)) { |
| 112 | fprintf(stderr, "pid %u exited\n", pid); |
| 113 | continue; |
| 114 | } |
| 115 | fprintf(stderr,"EIP = 0x%08x\n", ptrace(PTRACE_PEEKUSR, pid, 4*EIP, 0)); |
| 116 | fprintf(stderr,"EBP = 0x%08x\n", ptrace(PTRACE_PEEKUSR, pid, 4*EBP, 0)); |
| 117 | #if 0 |
| 118 | ptrace(PTRACE_SINGLESTEP, pid, 0, 0); |
| 119 | continue; |
| 120 | #endif |
| 121 | if (WIFSTOPPED(status)) { |
| 122 | fprintf(stderr, "pid %u stopped; continuing it...\n", pid); |
| 123 | ptrace(PTRACE_CONT, pid, 1, 0); |
| 124 | } else { |
| 125 | fprintf(stderr, "pid %u ???\n", pid); |
| 126 | } |
| 127 | } |
| 128 | exit(0); |
| 129 | } |