2011-09-14 8 views
6

Estoy siguiendo el tutorial here, y modificado un poco para x86-64 (básicamente reemplazar a eax rax, etc) para que compila:¿Cómo se juega con ptrace en x86-64?

#include <sys/ptrace.h> 
#include <sys/types.h> 
#include <sys/wait.h> 
#include <unistd.h> 
#include <sys/user.h> 
#include <sys/reg.h> 
#include <unistd.h> 


int main() 
{ pid_t child; 
    long orig_eax; 
    child = fork(); 
    if(child == 0) { 
     ptrace(PTRACE_TRACEME, 0, NULL, NULL); 
     execl("/bin/ls", "ls", NULL); 
    } 
    else { 
     wait(NULL); 
     orig_eax = ptrace(PTRACE_PEEKUSER, 
          child, 4 * ORIG_RAX, 
          NULL); 
     printf("The child made a " 
       "system call %ld\n", orig_eax); 
     ptrace(PTRACE_CONT, child, NULL, NULL); 
    } 
    return 0; 
} 

Pero en realidad no funciona como se esperaba, siempre se dice :

The child made a system call -1 

¿Qué hay de malo en el código?

Respuesta

5

ptrace devuelve -1 con ERRNO EIO porque lo que está tratando de leer no está alineado correctamente. Tomado de página de manual ptrace:

PTRACE_PEEKUSER 
      Reads a word at offset addr in the child's USER area, which 
      holds the registers and other information about the process (see 
      <sys/user.h>). The word is returned as the result of the 
      ptrace() call. Typically the offset must be word-aligned, 
      though this might vary by architecture. See NOTES. (data is 
      ignored.) 

En mi sistema de 64 bits, en 4 * ORIG_RAX no está alineado-8-byte. Pruebe con valores como 0 u 8 y debería funcionar.

+0

que no entiendo lo que quieres decir ... –

5

En 64 bits = 8 * ORIG_RAX

8 = sizeof (largos)

Cuestiones relacionadas