Find the answer to your Linux question:
Results 1 to 8 of 8
How can I write something to the child process space?? ptrace(PTRACE_POKETEXT,.....)seems could not do that The Linux sources(2.4.7-10) about sysptrace() is: ...... case PTRACE_POKETEXT: /* write the word at location ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2003
    Location
    china
    Posts
    8

    a problem about ptrace()


    How can I write something to the child process space??
    ptrace(PTRACE_POKETEXT,.....)seems could not do that

    The Linux sources(2.4.7-10) about sysptrace() is:

    ......
    case PTRACE_POKETEXT: /* write the word at location addr. */
    case PTRACE_POKEDATA:
    ret = 0;
    if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
    break;
    ret = -EIO;
    break;
    ......

    it seems don't write something to the child process space!!
    what can I do?


    sorry,my English is too weak :->

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Why do you get that idea? The access_process_vm function writes the data into the child's VM. I'd recommend using PTRACE_POKEDATA instead, though. It doesn't _really_ make a difference for the i386 arch, but technically POKETEXT is for poking in a process' text, and other archs might have the text and the data in seperate address spaces.
    What are you doing? Can you post the surrounding code, so that maybe I can see why it fails. Do you get EIO returned by ptrace(PTRACE_POKETEXT) or does it return success?

  3. #3
    Just Joined!
    Join Date
    Jul 2003
    Location
    china
    Posts
    8
    I know the access_process_vm function could write the data to child process but....
    a simple test:

    /* testwrite.c */

    /*The ADDR is the start of the data segment,could be writed*/
    #define ADDR 0x80495fc

    int main(int argc,char *argv[])
    {
    pid_t pid;
    long word=2;

    if(argc != 2){
    printf("Usage:testwrite pid\n");
    exit(0);
    }
    pid=atoi(argv[1]);
    ptrace(PTRACE_ATTACH,pid,NULL,NULL);

    waitpid(pid,NULL,WUNTRACED);
    ptrace(PTRACE_POKEDATA,pid,(void *)ADDR,&word);
    perror("testwrite");
    ptrace(PTRACE_DETACH,pid,NULL,NULL);
    exit(0);
    }

    /* testread.c */
    #define ADDR 0x80495fc

    int main(int argc,char *argv[])
    {
    pid_t pid;
    long word;

    if(argc != 2){
    printf("Usage:testread pid\n");
    exit(0);
    }
    pid=atoi(argv[1]);
    ptrace(PTRACE_ATTACH,pid,NULL,NULL);

    waitpid(pid,NULL,WUNTRACED);
    ptrace(PTRACE_PEEKDATA,pid,(void *)ADDR,&word);
    printf("There is :%#x\n",word);

    ptrace(PTRACE_DETACH,pid,NULL,NULL);
    exit(0);
    }

    then try it:
    ./testread 1242
    There is :0x4003d0f4
    ./testwrite 1242
    testwrite:success
    ./testread 1242
    There is :0x4003d0f4

    why??

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Did you check the return value from ptrace(PTRACE_POKEDATA)? What process is 1242?

  6. #5
    Just Joined!
    Join Date
    Jul 2003
    Location
    china
    Posts
    8
    return value is 0(success)
    The tested process is very simple.

    /* hi .c */
    int main(void)
    {
    while(1){
    printf("hi\n");
    }
    }

    ./hi > /dev/null &
    It's pid is 1242

    (RH7.2,kernel version 2.4.7-10)

  7. #6
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Hmmm... now that is very strange, admittedly. I don't really see why it should make a difference, but can't you try reading/writing a user-allocated variable in the test process instead? Like this:

    test.c:
    Code:
    int main(void)
    {
        int test;
        
        test = 0;
        printf("%p\n", &test);
        while(1);
            printf("%i\n", test);
    }
    write.c:
    Code:
    int main(int argc, char **argv)
    {
        pid_t pid;
        void *addr;
        
        pid = atoi(argv[1]);
        addr = atoi(argv[2]);
        ptrace(PTRACE_ATTACH, pid, NULL, NULL);
        waitpid(pid, NULL, WUNTRACED);
        ptrace(PTRACE_POKEDATA, pid, addr, 2); /* Yes, you're supposed to pass the data as a pointer, not a pointer to the data */
        perror("write");
        ptrace(PTRACE_DETACH, pid, NULL, NULL);
        return(0);
    }

  8. #7
    Just Joined!
    Join Date
    Jul 2003
    Location
    china
    Posts
    8
    Sorry,I make a very very ( ) mistake......
    it is ptrace(PEEKDATA,pid,(void *ADDR),&word) this &word
    is error,it should be word=ptrace(PEEKDATA,pid,(void *)ADDR,NULL)


    Very very sorry..............
    Thank you!


    sorry,my English is too weak :->

  9. #8
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Oh, of course... I guess I should have seen that, too... =)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •