Debian Codegraph @

src/libsysprof/sysprof-maps-parser.c

raw
1 /* sysprof-maps-parser.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include "sysprof-maps-parser-private.h"
24
25 static GRegex *address_range_regex;
26
27 void
28 sysprof_maps_parser_init (SysprofMapsParser *self,
29 const char *str,
30 gssize len)
31 {
32 line_reader_init (&self->reader, (char *)str, len);
33
34 if (address_range_regex == NULL)
35 address_range_regex = g_regex_new ("^([0-9a-f]+)-([0-9a-f]+) [r\\-][w\\-][x\\-][ps\\-] ([0-9a-f]+) [0-9a-f]+:[0-9a-f]+ ([0-9]+) +(.*)$",
36 G_REGEX_OPTIMIZE,
37 G_REGEX_MATCH_DEFAULT,
38 NULL);
39 }
40
41 gboolean
42 sysprof_maps_parser_next (SysprofMapsParser *self,
43 guint64 *out_begin_addr,
44 guint64 *out_end_addr,
45 guint64 *out_offset,
46 guint64 *out_inode,
47 char **out_filename)
48 {
49 const char *line;
50 gsize len;
51
52 while ((line = line_reader_next (&self->reader, &len)))
53 {
54 g_autoptr(GMatchInfo) match_info = NULL;
55
56 if (g_regex_match_full (address_range_regex, line, len, 0, 0, &match_info, NULL))
57 {
58 g_autofree char *file = NULL;
59 guint64 begin_addr;
60 guint64 end_addr;
61 guint64 inode;
62 guint64 offset;
63 gboolean is_vdso;
64 int begin_addr_begin;
65 int begin_addr_end;
66 int end_addr_begin;
67 int end_addr_end;
68 int offset_begin;
69 int offset_end;
70 int inode_begin;
71 int inode_end;
72 int path_begin;
73 int path_end;
74
75
76 if (!g_match_info_fetch_pos (match_info, 1, &begin_addr_begin, &begin_addr_end) ||
77 !g_match_info_fetch_pos (match_info, 2, &end_addr_begin, &end_addr_end) ||
78 !g_match_info_fetch_pos (match_info, 3, &offset_begin, &offset_end) ||
79 !g_match_info_fetch_pos (match_info, 4, &inode_begin, &inode_end) ||
80 !g_match_info_fetch_pos (match_info, 5, &path_begin, &path_end))
81 continue;
82
83 begin_addr = g_ascii_strtoull (&line[begin_addr_begin], NULL, 16);
84 end_addr = g_ascii_strtoull (&line[end_addr_begin], NULL, 16);
85 offset = g_ascii_strtoull (&line[offset_begin], NULL, 16);
86 inode = g_ascii_strtoull (&line[inode_begin], NULL, 10);
87
88 if (memcmp (" (deleted",
89 &line[path_end] - strlen (" (deleted"),
90 strlen (" (deleted")) == 0)
91 path_end -= strlen (" (deleted)");
92
93 file = g_strndup (&line[path_begin], path_end-path_begin);
94
95 is_vdso = strcmp ("[vdso]", file) == 0;
96
97 if (is_vdso)
98 inode = 0;
99
100 if (is_vdso)
101 offset = 0;
102
103 *out_begin_addr = begin_addr;
104 *out_end_addr = end_addr;
105 *out_offset = offset;
106 *out_inode = inode;
107 *out_filename = g_steal_pointer (&file);
108
109 return TRUE;
110 }
111 }
112
113 return FALSE;
114 }