- Timestamp:
- Jul 1, 2008, 4:14:54 PM (13 years ago)
- Location:
- neercs/trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/mytrace.c
r2509 r2511 38 38 char* dest, long src, size_t n); 39 39 static int memcpy_into_target(struct mytrace *t, 40 long dest, char *src, size_t n);40 long dest, char const *src, size_t n); 41 41 static long remote_syscall(pid_t pid, long call, 42 42 long arg1, long arg2, long arg3); 43 # if 043 # if defined DEBUG 44 44 static void print_registers(pid_t pid); 45 #endif 45 # else 46 # define print_registers(x) do {} while(0) 47 # endif 46 48 #endif 47 49 48 50 #define X(x) #x 49 51 #define STRINGIFY(x) X(x) 52 53 #define SYSCALL_X86 0x80cd /* CD 80 = int $0x80 */ 54 #define SYSCALL_AMD64 0x050fL /* 0F 05 = syscall */ 55 56 #define MYCALL_OPEN 0 57 #define MYCALL_CLOSE 1 58 #define MYCALL_DUP2 2 59 #define MYCALL_SETPGID 3 60 #define MYCALL_SETSID 4 50 61 51 62 #if defined __x86_64__ … … 58 69 # define RDI rdi 59 70 # define RSI rsi 60 # define SYSCALL 0x050fL /* 0F 05 = syscall */61 71 # define FMT "%016lx" 72 int syscalls64[] = { SYS_open, SYS_close, SYS_dup2, SYS_setpgid, SYS_setsid }; 73 int syscalls32[] = { 5, 6, 63, 57, 66 }; 62 74 #else 63 75 # define RAX eax … … 69 81 # define RDI edi 70 82 # define RSI esi 71 # define SYSCALL 0x80cd /* CD 80 = int $0x80 */72 83 # define FMT "%08lx" 84 int syscalls32[] = { SYS_open, SYS_close, SYS_dup2, SYS_setpgid, SYS_setsid }; 73 85 #endif 74 86 … … 136 148 memcpy_into_target(t, regs.RSP, path, size); 137 149 138 ret = remote_syscall(t->pid, SYS_open, regs.RSP, O_RDWR, 0755);139 140 /* Restore the data s*/150 ret = remote_syscall(t->pid, MYCALL_OPEN, regs.RSP, O_RDWR, 0755); 151 152 /* Restore the data */ 141 153 memcpy_into_target(t, regs.RSP, backup_data, size); 142 154 … … 157 169 { 158 170 #if defined USE_GRAB 159 return remote_syscall(t->pid, SYS_close, fd, 0, 0);171 return remote_syscall(t->pid, MYCALL_CLOSE, fd, 0, 0); 160 172 #else 161 173 errno = ENOSYS; … … 167 179 { 168 180 #if defined USE_GRAB 169 return remote_syscall(t->pid, SYS_dup2, oldfd, newfd, 0);181 return remote_syscall(t->pid, MYCALL_DUP2, oldfd, newfd, 0); 170 182 #else 171 183 errno = ENOSYS; … … 177 189 { 178 190 #if defined USE_GRAB 179 return remote_syscall(t->pid, SYS_setpgid, pid, pgid, 0);191 return remote_syscall(t->pid, MYCALL_SETPGID, pid, pgid, 0); 180 192 #else 181 193 errno = ENOSYS; … … 187 199 { 188 200 #if defined USE_GRAB 189 return remote_syscall(t->pid, SYS_setsid, 0, 0, 0);201 return remote_syscall(t->pid, MYCALL_SETSID, 0, 0, 0); 190 202 #else 191 203 errno = ENOSYS; … … 198 210 */ 199 211 212 #if defined USE_GRAB 200 213 static int memcpy_from_target(struct mytrace *t, 201 214 char* dest, long src, size_t n) 202 215 { 216 static int const align = sizeof(long) - 1; 217 203 218 while(n) 204 219 { 205 220 long data; 206 int align = sizeof(long) - 1;207 221 size_t todo = sizeof(long) - (src & align); 208 222 … … 227 241 228 242 static int memcpy_into_target(struct mytrace *t, 229 long dest, char *src, size_t n) 230 { 243 long dest, char const *src, size_t n) 244 { 245 static int const align = sizeof(long) - 1; 246 231 247 while(n) 232 248 { 233 249 long data; 234 int align = sizeof(long) - 1;235 250 size_t todo = sizeof(long) - (dest & align); 236 251 … … 271 286 * - rewind eip/rip to point on the syscall instruction 272 287 * - single step: execute syscall instruction 288 * - retrieve resulting registers 273 289 * - restore registers */ 274 290 struct user_regs_struct regs, oldregs; 275 291 long oinst; 292 int bits; 293 294 print_registers(pid); 295 #if defined __x86_64__ 296 bits = 64; 297 #else 298 bits = 32; 299 #endif 276 300 277 301 for(;;) … … 285 309 oinst = ptrace(PTRACE_PEEKTEXT, pid, oldregs.RIP - 2, 0) & 0xffff; 286 310 287 if(oinst == SYSCALL) 311 #if defined __x86_64__ 312 if(oinst == SYSCALL_AMD64) 288 313 break; 314 if(oinst == SYSCALL_X86) 315 { 316 bits = 32; 317 break; 318 } 319 #else 320 if(oinst == SYSCALL_X86) 321 break; 322 #endif 289 323 290 324 if(ptrace(PTRACE_SYSCALL, pid, NULL, 0) < 0) … … 303 337 } 304 338 339 print_registers(pid); 340 305 341 regs = oldregs; 306 342 regs.RIP = regs.RIP - 2; 307 regs.RAX = call;308 343 #if defined __x86_64__ 309 regs.RDI = arg1; 310 regs.RSI = arg2; 311 regs.RDX = arg3; 312 #else 313 regs.RBX = arg1; 314 regs.RCX = arg2; 315 regs.RDX = arg3; 316 #endif 344 if(bits == 64) 345 { 346 regs.RAX = syscalls64[call]; 347 regs.RDI = arg1; 348 regs.RSI = arg2; 349 regs.RDX = arg3; 350 } 351 else 352 #endif 353 { 354 regs.RAX = syscalls32[call]; 355 regs.RBX = arg1; 356 regs.RCX = arg2; 357 regs.RDX = arg3; 358 } 317 359 318 360 if(ptrace(PTRACE_SETREGS, pid, NULL, ®s) < 0) … … 322 364 } 323 365 366 print_registers(pid); 367 324 368 if(ptrace(PTRACE_SINGLESTEP, pid, NULL, NULL) < 0) 325 369 { … … 329 373 waitpid(pid, NULL, 0); 330 374 375 print_registers(pid); 376 331 377 if(ptrace(PTRACE_GETREGS, pid, NULL, ®s) < 0) 332 378 { … … 340 386 return -1; 341 387 } 388 print_registers(pid); 342 389 343 390 debug("syscall %ld returned %ld", call, regs.RAX); … … 354 401 355 402 /* For debugging purposes only. Prints register and stack information. */ 356 #if 0403 #if defined DEBUG 357 404 static void print_registers(pid_t pid) 358 405 { … … 400 447 fprintf(stderr, "...\n"); 401 448 } 402 #endif 403 449 #endif /* DEBUG */ 450 451 #endif /* USE_GRAB */ 452 -
neercs/trunk/src/neercs.h
r2510 r2511 212 212 static inline void debug(const char *format, ...) 213 213 { 214 int saved_errno = geterrno();215 214 va_list args; 216 215 va_start(args, format); … … 219 218 fprintf(stderr, "\n"); 220 219 va_end(args); 221 seterrno(saved_errno);222 220 } 223 221 #else
Note: See TracChangeset
for help on using the changeset viewer.