xref: /openbmc/qemu/util/selfmap.c (revision f7160f32)
1 /*
2  * Utility function to get QEMU's own process map
3  *
4  * Copyright (c) 2020 Linaro Ltd
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/cutils.h"
11 #include "qemu/selfmap.h"
12 
13 GSList *read_self_maps(void)
14 {
15     gchar *maps;
16     GSList *map_info = NULL;
17 
18     if (g_file_get_contents("/proc/self/maps", &maps, NULL, NULL)) {
19         gchar **lines = g_strsplit(maps, "\n", 0);
20         int i, entries = g_strv_length(lines);
21 
22         for (i = 0; i < entries; i++) {
23             gchar **fields = g_strsplit(lines[i], " ", 6);
24             if (g_strv_length(fields) > 4) {
25                 MapInfo *e = g_new0(MapInfo, 1);
26                 int errors;
27                 const char *end;
28 
29                 errors  = qemu_strtoul(fields[0], &end, 16, &e->start);
30                 errors += qemu_strtoul(end + 1, NULL, 16, &e->end);
31 
32                 e->is_read  = fields[1][0] == 'r';
33                 e->is_write = fields[1][1] == 'w';
34                 e->is_exec  = fields[1][2] == 'x';
35                 e->is_priv  = fields[1][3] == 'p';
36 
37                 errors += qemu_strtoul(fields[2], NULL, 16, &e->offset);
38                 e->dev = g_strdup(fields[3]);
39                 errors += qemu_strtou64(fields[4], NULL, 10, &e->inode);
40 
41                 /*
42                  * The last field may have leading spaces which we
43                  * need to strip.
44                  */
45                 if (g_strv_length(fields) == 6) {
46                     e->path = g_strdup(g_strchug(fields[5]));
47                 }
48                 map_info = g_slist_prepend(map_info, e);
49             }
50 
51             g_strfreev(fields);
52         }
53         g_strfreev(lines);
54         g_free(maps);
55     }
56 
57     /* ensure the map data is in the same order we collected it */
58     return g_slist_reverse(map_info);
59 }
60 
61 /**
62  * free_self_maps:
63  * @info: a GSlist
64  *
65  * Free a list of MapInfo structures.
66  */
67 static void free_info(gpointer data)
68 {
69     MapInfo *e = (MapInfo *) data;
70     g_free(e->dev);
71     g_free(e->path);
72     g_free(e);
73 }
74 
75 void free_self_maps(GSList *info)
76 {
77     g_slist_free_full(info, &free_info);
78 }
79