2020-04-03 22:11:46 +03:00
|
|
|
/*
|
|
|
|
* Utility function to get QEMU's own process map
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Linaro Ltd
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/cutils.h"
|
|
|
|
#include "qemu/selfmap.h"
|
|
|
|
|
2023-08-06 20:10:44 +03:00
|
|
|
IntervalTreeRoot *read_self_maps(void)
|
2020-04-03 22:11:46 +03:00
|
|
|
{
|
2023-08-06 20:10:44 +03:00
|
|
|
IntervalTreeRoot *root;
|
|
|
|
gchar *maps, **lines;
|
|
|
|
guint i, nlines;
|
2020-04-03 22:11:46 +03:00
|
|
|
|
2023-08-06 20:10:44 +03:00
|
|
|
if (!g_file_get_contents("/proc/self/maps", &maps, NULL, NULL)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
root = g_new0(IntervalTreeRoot, 1);
|
|
|
|
lines = g_strsplit(maps, "\n", 0);
|
|
|
|
nlines = g_strv_length(lines);
|
|
|
|
|
|
|
|
for (i = 0; i < nlines; i++) {
|
|
|
|
gchar **fields = g_strsplit(lines[i], " ", 6);
|
|
|
|
guint nfields = g_strv_length(fields);
|
|
|
|
|
|
|
|
if (nfields > 4) {
|
|
|
|
uint64_t start, end, offset, inode;
|
2023-08-09 05:43:51 +03:00
|
|
|
unsigned dev_maj, dev_min;
|
2023-08-06 20:10:44 +03:00
|
|
|
int errors = 0;
|
|
|
|
const char *p;
|
2020-04-03 22:11:46 +03:00
|
|
|
|
2023-08-06 20:10:44 +03:00
|
|
|
errors |= qemu_strtou64(fields[0], &p, 16, &start);
|
|
|
|
errors |= qemu_strtou64(p + 1, NULL, 16, &end);
|
|
|
|
errors |= qemu_strtou64(fields[2], NULL, 16, &offset);
|
2023-08-09 05:43:51 +03:00
|
|
|
errors |= qemu_strtoui(fields[3], &p, 16, &dev_maj);
|
|
|
|
errors |= qemu_strtoui(p + 1, NULL, 16, &dev_min);
|
2023-08-06 20:10:44 +03:00
|
|
|
errors |= qemu_strtou64(fields[4], NULL, 10, &inode);
|
|
|
|
|
|
|
|
if (!errors) {
|
2023-08-09 05:43:51 +03:00
|
|
|
size_t path_len;
|
2023-08-06 20:10:44 +03:00
|
|
|
MapInfo *e;
|
|
|
|
|
|
|
|
if (nfields == 6) {
|
|
|
|
p = fields[5];
|
|
|
|
p += strspn(p, " ");
|
|
|
|
path_len = strlen(p) + 1;
|
|
|
|
} else {
|
|
|
|
p = NULL;
|
|
|
|
path_len = 0;
|
|
|
|
}
|
2020-04-03 22:11:46 +03:00
|
|
|
|
2023-08-09 05:43:51 +03:00
|
|
|
e = g_malloc0(sizeof(*e) + path_len);
|
2023-08-06 20:10:44 +03:00
|
|
|
|
|
|
|
e->itree.start = start;
|
|
|
|
e->itree.last = end - 1;
|
|
|
|
e->offset = offset;
|
2023-08-09 05:43:51 +03:00
|
|
|
e->dev = makedev(dev_maj, dev_min);
|
2023-08-06 20:10:44 +03:00
|
|
|
e->inode = inode;
|
2020-04-03 22:11:46 +03:00
|
|
|
|
|
|
|
e->is_read = fields[1][0] == 'r';
|
|
|
|
e->is_write = fields[1][1] == 'w';
|
|
|
|
e->is_exec = fields[1][2] == 'x';
|
|
|
|
e->is_priv = fields[1][3] == 'p';
|
|
|
|
|
2023-08-06 20:10:44 +03:00
|
|
|
if (path_len) {
|
2023-08-09 05:43:51 +03:00
|
|
|
e->path = memcpy(e + 1, p, path_len);
|
2020-04-03 22:11:46 +03:00
|
|
|
}
|
|
|
|
|
2023-08-06 20:10:44 +03:00
|
|
|
interval_tree_insert(&e->itree, root);
|
|
|
|
}
|
2020-04-03 22:11:46 +03:00
|
|
|
}
|
2023-08-06 20:10:44 +03:00
|
|
|
g_strfreev(fields);
|
2020-04-03 22:11:46 +03:00
|
|
|
}
|
2023-08-06 20:10:44 +03:00
|
|
|
g_strfreev(lines);
|
|
|
|
g_free(maps);
|
2020-04-03 22:11:46 +03:00
|
|
|
|
2023-08-06 20:10:44 +03:00
|
|
|
return root;
|
2020-04-03 22:11:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* free_self_maps:
|
2023-08-06 20:10:44 +03:00
|
|
|
* @root: an interval tree
|
2020-04-03 22:11:46 +03:00
|
|
|
*
|
2023-08-06 20:10:44 +03:00
|
|
|
* Free a tree of MapInfo structures.
|
|
|
|
* Since we allocated each MapInfo in one chunk, we need not consider the
|
|
|
|
* contents and can simply free each RBNode.
|
2020-04-03 22:11:46 +03:00
|
|
|
*/
|
2023-08-06 20:10:44 +03:00
|
|
|
|
|
|
|
static void free_rbnode(RBNode *n)
|
2020-04-03 22:11:46 +03:00
|
|
|
{
|
2023-08-06 20:10:44 +03:00
|
|
|
if (n) {
|
|
|
|
free_rbnode(n->rb_left);
|
|
|
|
free_rbnode(n->rb_right);
|
|
|
|
g_free(n);
|
|
|
|
}
|
2020-04-03 22:11:46 +03:00
|
|
|
}
|
|
|
|
|
2023-08-06 20:10:44 +03:00
|
|
|
void free_self_maps(IntervalTreeRoot *root)
|
2020-04-03 22:11:46 +03:00
|
|
|
{
|
2023-08-06 20:10:44 +03:00
|
|
|
if (root) {
|
|
|
|
free_rbnode(root->rb_root.rb_node);
|
|
|
|
g_free(root);
|
|
|
|
}
|
2020-04-03 22:11:46 +03:00
|
|
|
}
|