Examining mmaped addresses using GDB

后端 未结 7 1957
[愿得一人]
[愿得一人] 2020-12-13 21:19

I\'m using the driver I posted at Direct Memory Access in Linux to mmap some physical ram into a userspace address. However, I can\'t use GDB to look at any of the address;

7条回答
  •  隐瞒了意图╮
    2020-12-13 21:49

    I believe Linux does not make I/O memory accessible via ptrace(). You could write a function that simply reads the mmap'ed address and have gdb invoke it. Here's a slightly modified version of your foo-user.c program along with the output from a gdb session.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    char *mptr;
    
    char peek(int offset)
    {
        return mptr[offset];
    }
    
    int main(void)
    {
        int fd;
        fd = open("/dev/foo", O_RDWR | O_SYNC);
        if (fd == -1) {
            printf("open error...\n");
            return 1;
        }
        mptr = mmap(0, 1 * 1024 * 1024, PROT_READ | PROT_WRITE,
                 MAP_FILE | MAP_SHARED, fd, 4096);
        printf("On start, mptr points to 0x%lX.\n", (unsigned long) mptr);
        printf("mptr points to 0x%lX. *mptr = 0x%X\n", (unsigned long) mptr,
               *mptr);
        mptr[0] = 'a';
        mptr[1] = 'b';
        printf("mptr points to 0x%lX. *mptr = 0x%X\n", (unsigned long) mptr,
               *mptr);
        close(fd);
        return 0;
    }
    
    
    
    $ make foo-user CFLAGS=-g
    $ gdb -q foo-user
    (gdb) break 27
    Breakpoint 1 at 0x804855f: file foo-user.c, line 27.
    (gdb) run
    Starting program: /home/me/foo/foo-user 
    On start, mptr points to 0xB7E1E000.
    mptr points to 0xB7E1E000. *mptr = 0x61
    
    Breakpoint 1, main () at foo-user.c:27
    27          mptr[0] = 'a';
    (gdb) n
    28          mptr[1] = 'b';
    (gdb) print peek(0)
    $1 = 97 'a'
    (gdb) print peek(1)
    $2 = 98 'b'
    

提交回复
热议问题