Exemplo n.º 1
0
void snapshot_destroy(struct snapshot *snap)
{
    if (snap == NULL)
        return;

    if (snap->map != NULL)
        mem_map_destroy(snap->map);

    free(snap->regs);
    free(snap->tids);
    free(snap);
}
Exemplo n.º 2
0
struct mem_map *create_maps(int pid)
{
    FILE *f;
    char *buf = NULL, *str = NULL;
    size_t total_read, capacity;

    size_t addr_start, addr_end, offset, len;
    char r, w, x, p;
    int dev_major, dev_minor, inode;
    char path[PATH_MAX];

    struct mem_map *map = NULL;
    struct mem_region *region;

    capacity = 0x100000;
    buf = calloc(1, capacity);

    sprintf(buf, "/proc/%d/maps", pid);
    if ((f = fopen(buf, "r")) == NULL) {
        fprintf(stderr, "cannot open %s: %s\n", buf, strerror(errno));
        return NULL;
    }

    map = malloc(sizeof(struct mem_map));
    mem_map_init(map);

    memset(buf, 0, capacity);
    total_read = 0;
    while (!feof(f)) {
        fread(&buf[total_read], capacity - total_read - 1, 1, f);
        if (errno) {
            perror("maps");
            mem_map_destroy(map);
            map = NULL;
            goto create_maps_end;
        }

        total_read = strlen(buf);
        if ((total_read + 1) == capacity) {
            capacity *= 2;
            buf = realloc(buf, capacity);
            memset(&buf[total_read], 0, capacity - total_read);
        } else {
            buf[total_read] = '\0';
        }
    }

    str = &buf[0];
    while (*str) {
        int scan;
        char *next;

        next = strchr(str, '\n');
        if (next != NULL)
            *next = '\0';

        scan = sscanf(str, "%zx-%zx %c%c%c%c %zx %x:%x %d %[^\t\n]",
                &addr_start, &addr_end,
                &r, &w, &x, &p,
                &offset,
                &dev_major, &dev_minor,
                &inode,
                path);

        if (scan < 10) {
            fprintf(stderr, "warning: unable to parse maps "
                    "entry '%s' (read %d)\n", str, scan);
            break;
        }

        region = malloc(sizeof(struct mem_region));
        mem_region_init(region);

        region->start = (void *)addr_start;
        region->length = addr_end - addr_start;
        region->offset = offset;
        if (scan > 10 && path[0] != '\0') {
            if (!strcmp(path, "[vdso]")) {
                region->type = MEM_REGION_TYPE_VDSO;
            } else if (!strcmp(path, "[vsyscall]")) {
                region->type = MEM_REGION_TYPE_VSYSCALL;
            } else if ((len = strlen(path)) > 10 &&
                    !strcmp(path + len - 10, " (deleted)")) {
                *(path + len - 10) = '\0';
                region->path = strdup(path);
                region->type = MEM_REGION_TYPE_DELETED;
            } else {
                region->path = strdup(path);
                region->type = MEM_REGION_TYPE_MMAP;
            }
        }

        if (mem_map_add_region(map, region) != 0) {
            mem_map_destroy(map);
            map = NULL;
            break;
        }

        if (next != NULL)
            str = next + 1;
    }

    if (map != NULL)
        mem_map_create_region_index(map);

create_maps_end:
    fclose(f);
    free(buf);
    return map;
}