TIL: pmap

The other day I was working on a Java application deployment, experimenting with some claimed performance improvements by installing native image libraries.

I wanted to see if the native library was actually being loaded, and my first thought was "this is a job for strace -e trace=open"! (reference: the always-excellent Julia Evans. You should go read those even if you don't read any further here).

Something made me search first though, and what I actually discovered was the pmap command, which can tell you exactly what file(s) are mapped (the -p is for "path", not "pid"):

pmap -p <pid>

It turns out that this is also exactly the contents of the ls -l /proc/<pid>/map_files, or cat /proc/<pid>maps.

(Note that it is not the same as my original strace plan, which was to look for the library being opened; this shows files that are mmap-ed into the process)

To test it out, I grabbed the mmap example from the man page, added a getchar() call to keep the process running, and ran it:

#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define handle_error(msg)                                       \
        do { perror(msg); exit(EXIT_FAILURE); } while (0)

int main(int argc, char *argv[]) {
        char *addr;
        int fd;
        struct stat sb;
        off_t offset, pa_offset;
        size_t length;
        ssize_t s;

        if (argc < 3 || argc > 4) {
                fprintf(stderr, "%s file offset [length]\n", argv[0]);
                exit(EXIT_FAILURE);
        }

        fd = open(argv[1], O_RDONLY);
        if (fd == -1)
                handle_error("open");

        if (fstat(fd, &sb) == -1)           /* To obtain file size */
                handle_error("fstat");

        offset = atoi(argv[2]);
        pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);
        /* offset for mmap() must be page aligned */

        if (offset >= sb.st_size) {
                fprintf(stderr, "offset is past end of file\n");
                exit(EXIT_FAILURE);
        }

        if (argc == 4) {
                length = atoi(argv[3]);
                if (offset + length > sb.st_size)
                        length = sb.st_size - offset;
                /* Can't display bytes past end of file */

        } else {    /* No length arg ==> display to end of file */
                length = sb.st_size - offset;
        }

        addr = mmap(NULL, length + offset - pa_offset, PROT_READ,
                    MAP_PRIVATE, fd, pa_offset);
        if (addr == MAP_FAILED)
                handle_error("mmap");

        s = write(STDOUT_FILENO, addr + offset - pa_offset, length);
        if (s != length) {
                if (s == -1)
                        handle_error("write");

                fprintf(stderr, "partial write");
                exit(EXIT_FAILURE);
        }

        while(getchar()!='\n'); /* ADDED: Wait to exit */

        munmap(addr, length + offset - pa_offset);
        close(fd);

        exit(EXIT_SUCCESS); }

Running:

$ gcc maptest.c
$ ./a.out maptest.c 5

while this is running, in another terminal:

PID=$(pgrep a.out)
pmap -p $PID

and sure enough, there's our file mapped into the process' memory.


TILLinux

416 Words

2023-07-09 00:00 +0000

comments powered by Disqus