2 Declaration for Linux kernel compatibility
10 #include <sys/ptrace.h>
15 char *victim_exe
= NULL
;
17 #define TRAP_BIT (0x80000000)
19 static struct nlist
*exe_nlist
;
20 static int exe_nlist_n
;
22 /* unsigned long __get_free_page(int type) { assert(0); } */
23 /* void *kmalloc(size_t size, int type) { assert(0); } */
24 void free_page(unsigned long page
) { assert(0); }
25 /* void kfree(void *mem) { assert(0); } */
26 void vfree(void *mem
) { assert(0); }
28 size_t strncpy_from_user(char *addr
, const char *user_name
, size_t size
)
29 { assert(0); return 0; }
31 /* void lock_kernel(void) { assert(0); } */
32 /* void unlock_kernel(void) { assert(0); } */
33 /* void __asm__(char *str) { assert(0); } */
35 extern void *__vmalloc(unsigned long size
, int gfp_mask
, pgprot_t prot
)
36 { assert(0); return NULL
; }
39 void kallsyms_sections(void *infop
,
40 int (*fp
)(void *token
, const char *modname
, const char *secname
,
41 ElfW(Addr
) secstart
, ElfW(Addr
) secend
, ElfW(Word
) secflags
))
45 unsigned long __generic_copy_to_user(void *x
, const void *y
, unsigned long z
)
46 { assert(0); return -1; }
47 unsigned long __generic_copy_from_user(void *x
, const void *y
, unsigned long z
)
48 { assert(0); return -1; }
50 /* void read_lock(struct lock *lock) { assert(0); } */
51 /* void read_unlock(struct lock *lock) { assert(0); } */
52 void udelay(unsigned long usecs
) { assert(0); }
53 int copy_to_user(void * result_record
, void *res
, size_t size
)
55 memcpy(result_record
, res
, size
);
59 void panic(char *str
) { assert(0); }
61 void printk(char *fmt
, ...)
66 vfprintf(stderr
, fmt
, ap
);
70 int kallsyms_address_to_symbol(db_expr_t off
,
71 const char * *mod_name
, unsigned long *mod_start
, unsigned long *mod_end
,
72 const char * *sec_name
, unsigned long *sec_start
, unsigned long *sec_end
,
73 const char * *sym_name
, unsigned long *sym_start
, unsigned long *sym_end
)
78 unsigned long btext
, etext
;
79 struct nlist
*below
, *above
;
82 load_nlist(victim_exe
, &btext
, &etext
);
84 for (i
= 0; i
<exe_nlist_n
; i
++)
86 if (exe_nlist
[i
].n_type
!= N_TEXT
)
88 if (exe_nlist
[i
].n_value
<= off
)
90 if (!below
|| exe_nlist
[i
].n_value
> below
->n_value
)
93 if (exe_nlist
[i
].n_value
> off
)
95 if (!above
|| exe_nlist
[i
].n_value
< above
->n_value
)
102 printf("found '%s' at 0x%x\n", below
->n_name
, below
->n_value
);
106 printf("found '%s' at 0x%x\n", above
->n_name
, above
->n_value
);
113 *mod_name
= victim_exe
;
120 assert(below
&& above
);
122 strncpy(name
, below
->n_name
, sizeof(name
)-1);
123 name
[sizeof(name
)-1]= '\0';
126 *sym_start
= below
->n_value
| TRAP_BIT
;
127 *sym_end
= above
->n_value
| TRAP_BIT
;
132 struct module
*module_list
;
133 struct task_struct
*task_list
;
134 struct lock tasklist_lock
;
136 unsigned long text_read_ul(void *addr
)
141 for (i
= 0; i
<sizeof(value
); i
++)
143 ((unsigned char *)&value
)[i
]= text_read_ub((char *)addr
+i
);
148 unsigned char text_read_ub(void *addr
)
153 vaddr
= (unsigned long)addr
;
155 v
= ptrace(T_READB_INS
, victim_pid
, vaddr
, 0);
159 "text_read_ub: trace T_READB_INS failed on pid %d, addr 0x%lx: %s\n",
160 victim_pid
, vaddr
, strerror(errno
));
166 void text_write_ul(void *addr
, unsigned long value
)
170 for (i
= 0; i
<sizeof(value
); i
++)
172 text_write_ub((char *)addr
+i
, ((unsigned char *)&value
)[i
]);
176 void text_write_ub(void *addr
, unsigned char value
)
181 vaddr
= (unsigned long)addr
;
183 v
= ptrace(T_WRITEB_INS
, victim_pid
, vaddr
, value
);
187 "text_read_ub: trace T_WRITEB_INS failed on pid %d, addr 0x%lx: %s\n",
188 victim_pid
, vaddr
, strerror(errno
));
193 void load_nlist(exe_name
, btextp
, etextp
)
195 unsigned long *btextp
;
196 unsigned long *etextp
;
199 unsigned long btext
, etext
;
203 exe_nlist_n
= read_nlist(exe_name
, &exe_nlist
);
204 if (exe_nlist_n
<= 0)
206 if (exe_nlist_n
== -1)
209 "error reading name list from '%s': %s\n",
210 exe_name
, strerror(errno
));
213 fprintf(stderr
, "no name list in '%s'\n",
219 if (!btextp
&& !etextp
)
223 btext
= (unsigned long)-1;
224 for (i
= 0; i
<exe_nlist_n
; i
++)
226 if (exe_nlist
[i
].n_type
!= N_TEXT
)
228 if (exe_nlist
[i
].n_value
< btext
)
229 btext
= exe_nlist
[i
].n_value
;
230 if (exe_nlist
[i
].n_value
> etext
)
231 etext
= exe_nlist
[i
].n_value
;
236 fprintf(stderr
, "Bad btext (0x%lx) or etext (0x%lx) in %s\n",
237 btext
, etext
, exe_name
);