Accessing another process virtual memory in Linux (debugging)

前端 未结 1 1998
故里飘歌
故里飘歌 2020-12-21 16:22

How does gdb access another process virtual memory on Linux? Is it all done via /proc?

相关标签:
1条回答
  • 2020-12-21 17:02

    How does gdb access another process virtual memory on Linux? Is it all done via /proc?

    On Linux for reading memory:

    1) If the number of bytes to read is fewer than 3 * sizeof (long) or the filesystem /proc is unavailable or reading from /proc/PID/mem is unsuccessful then ptrace is used with PTRACE_PEEKTEXT to read data.

    These are these conditions in the function linux_proc_xfer_partial():

      /* Don't bother for one word.  */
      if (len < 3 * sizeof (long))
        return 0;
    
      /* We could keep this file open and cache it - possibly one per
         thread.  That requires some juggling, but is even faster.  */
      xsnprintf (filename, sizeof filename, "/proc/%d/mem",
             ptid_get_pid (inferior_ptid));
      fd = gdb_open_cloexec (filename, O_RDONLY | O_LARGEFILE, 0);
      if (fd == -1)
        return 0;
    

    2) If the number of bytes to read is greater or equal to 3 * sizeof (long) and /proc is available then pread64 or (lseek() and read() are used:

    static LONGEST
    linux_proc_xfer_partial (struct target_ops *ops, enum target_object object,
                 const char *annex, gdb_byte *readbuf,
                 const gdb_byte *writebuf,
                 ULONGEST offset, LONGEST len)
    {
      .....
    
      /* If pread64 is available, use it.  It's faster if the kernel
         supports it (only one syscall), and it's 64-bit safe even on
         32-bit platforms (for instance, SPARC debugging a SPARC64
         application).  */
    #ifdef HAVE_PREAD64
      if (pread64 (fd, readbuf, len, offset) != len)
    #else
      if (lseek (fd, offset, SEEK_SET) == -1 || read (fd, readbuf, len) != len)
    #endif
        ret = 0;
      else
        ret = len;
    
      close (fd);
      return ret;
    }
    

    On Linux for writing memory:

    1) ptrace with PTRACE_POKETEXT or PTRACE_POKEDATA is used.


    As for your second question:

    where can I find information about ... setting hardware watchpoints

    gdb, Internals Watchpoint:s http://sourceware.org/gdb/wiki/Internals%20Watchpoints

    Reference:

    • http://linux.die.net/man/2/ptrace
    • http://www.alexonlinux.com/how-debugger-works
    0 讨论(0)
提交回复
热议问题