File: sp-elf-symbol-resolver.c

package info (click to toggle)
sysprof 3.30.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,616 kB
  • sloc: ansic: 20,056; xml: 96; cpp: 23; sh: 18; makefile: 9
file content (310 lines) | stat: -rw-r--r-- 9,811 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* sp-elf-symbol-resolver.c
 *
 * Copyright (C) 2016 Christian Hergert <chergert@redhat.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>

#include "symbols/sp-elf-symbol-resolver.h"
#include "util/binfile.h"
#include "util/elfparser.h"
#include "util/sp-map-lookaside.h"

struct _SpElfSymbolResolver
{
  GObject     parent_instance;

  GHashTable *lookasides;
  GHashTable *bin_files;
  GHashTable *tag_cache;
};

static void symbol_resolver_iface_init (SpSymbolResolverInterface *iface);

G_DEFINE_TYPE_EXTENDED (SpElfSymbolResolver,
                        sp_elf_symbol_resolver,
                        G_TYPE_OBJECT,
                        0,
                        G_IMPLEMENT_INTERFACE (SP_TYPE_SYMBOL_RESOLVER,
                                               symbol_resolver_iface_init))

static void
sp_elf_symbol_resolver_finalize (GObject *object)
{
  SpElfSymbolResolver *self = (SpElfSymbolResolver *)object;

  g_clear_pointer (&self->bin_files, g_hash_table_unref);
  g_clear_pointer (&self->lookasides, g_hash_table_unref);
  g_clear_pointer (&self->tag_cache, g_hash_table_unref);

  G_OBJECT_CLASS (sp_elf_symbol_resolver_parent_class)->finalize (object);
}

static void
sp_elf_symbol_resolver_class_init (SpElfSymbolResolverClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = sp_elf_symbol_resolver_finalize;
}

static void
sp_elf_symbol_resolver_init (SpElfSymbolResolver *self)
{
  self->lookasides = g_hash_table_new_full (NULL,
                                            NULL,
                                            NULL,
                                            (GDestroyNotify)sp_map_lookaside_free);

  self->bin_files = g_hash_table_new_full (g_str_hash,
                                           g_str_equal,
                                           g_free,
                                           (GDestroyNotify)bin_file_free);

  self->tag_cache = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}

static void
sp_elf_symbol_resolver_load (SpSymbolResolver *resolver,
                             SpCaptureReader  *reader)
{
  SpElfSymbolResolver *self = (SpElfSymbolResolver *)resolver;
  SpCaptureFrameType type;

  g_assert (SP_IS_SYMBOL_RESOLVER (resolver));
  g_assert (reader != NULL);

  sp_capture_reader_reset (reader);

  while (sp_capture_reader_peek_type (reader, &type))
    {
      const SpCaptureMap *ev;
      SpMapLookaside *lookaside;
      SpMap map;

      if (type != SP_CAPTURE_FRAME_MAP)
        {
          if (!sp_capture_reader_skip (reader))
            return;
          continue;
        }

      ev = sp_capture_reader_read_map (reader);

      map.start = ev->start;
      map.end = ev->end;
      map.offset = ev->offset;
      map.inode = ev->inode;
      map.filename = ev->filename;

      lookaside = g_hash_table_lookup (self->lookasides, GINT_TO_POINTER (ev->frame.pid));

      if (lookaside == NULL)
        {
          lookaside = sp_map_lookaside_new ();
          g_hash_table_insert (self->lookasides, GINT_TO_POINTER (ev->frame.pid), lookaside);
        }

      sp_map_lookaside_insert (lookaside, &map);
    }
}

static bin_file_t *
sp_elf_symbol_resolver_get_bin_file (SpElfSymbolResolver *self,
                                     const gchar         *filename)
{
  bin_file_t *bin_file;

  g_assert (SP_IS_ELF_SYMBOL_RESOLVER (self));

  bin_file = g_hash_table_lookup (self->bin_files, filename);

  if (bin_file == NULL)
    {
      const gchar *alternate = filename;

      /*
       * If we are in a new mount namespace, then rely on the sp_symbol_dirs
       * to find us a locate to resolve the file where the CRC will match.
       *
       * TODO: We need to translate the path here so that we can locate the
       *       binary behind it (which then has links to the debug file in
       *       the section header).
       */
      if (g_str_has_prefix (filename, "/newroot/"))
        alternate += strlen ("/newroot");

      bin_file = bin_file_new (alternate);
      g_hash_table_insert (self->bin_files, g_strdup (filename), bin_file);
    }

  return bin_file;
}

static GQuark
guess_tag (SpElfSymbolResolver *self,
           const SpMap         *map)
{
  g_assert (map != NULL);
  g_assert (map->filename != NULL);

  if (!g_hash_table_contains (self->tag_cache, map->filename))
    {
      GQuark tag = 0;

      if (strstr (map->filename, "/libgobject-2.0."))
        tag = g_quark_from_static_string ("GObject");

      else if (strstr (map->filename, "/libglib-2.0."))
        tag = g_quark_from_static_string ("GLib");

      else if (strstr (map->filename, "/libgio-2.0."))
        tag = g_quark_from_static_string ("Gio");

      else if (strstr (map->filename, "/libgirepository-1.0."))
        tag = g_quark_from_static_string ("Introspection");

      else if (strstr (map->filename, "/libgtk-3."))
        tag = g_quark_from_static_string ("Gtk+");

      else if (strstr (map->filename, "/libgdk-3."))
        tag = g_quark_from_static_string ("Gdk");

      else if (strstr (map->filename, "/libgtksourceview-3.0"))
        tag = g_quark_from_static_string ("GtkSourceView");

      else if (strstr (map->filename, "/libpixman-1"))
        tag = g_quark_from_static_string ("Pixman");

      else if (strstr (map->filename, "/libcairo."))
        tag = g_quark_from_static_string ("cairo");

      else if (strstr (map->filename, "/libgstreamer-1."))
        tag = g_quark_from_static_string ("GStreamer");

      else if (strstr (map->filename, "/libX11."))
        tag = g_quark_from_static_string ("X11");

      else if (strstr (map->filename, "/libpango-1.0."))
        tag = g_quark_from_static_string ("Pango");

      else if (strstr (map->filename, "/libpangocairo-1.0."))
        tag = g_quark_from_static_string ("Pango");

      else if (strstr (map->filename, "/libpangomm-1.4."))
        tag = g_quark_from_static_string ("Pango");

      else if (strstr (map->filename, "/libpangoft2-1.0"))
        tag = g_quark_from_static_string ("Pango");

      else if (strstr (map->filename, "/libpangoxft-1.0."))
        tag = g_quark_from_static_string ("Pango");

      else if (strstr (map->filename, "/libclutter-"))
        tag = g_quark_from_static_string ("Clutter");

      else if (strstr (map->filename, "/libcogl.") ||
               strstr (map->filename, "/libcogl-"))
        tag = g_quark_from_static_string ("Cogl");

      else if (strstr (map->filename, "/libffi."))
        tag = g_quark_from_static_string ("libffi");

      else if (strstr (map->filename, "/libwayland-"))
        tag = g_quark_from_static_string ("Wayland");

      else if (strstr (map->filename, "/libinput."))
        tag = g_quark_from_static_string ("libinput");

      else if (strstr (map->filename, "/libgjs."))
        tag = g_quark_from_static_string ("Gjs");

      else if (strstr (map->filename, "/libmozjs-"))
        tag = g_quark_from_static_string ("MozJS");

      else if (strstr (map->filename, "/libGL."))
        tag = g_quark_from_static_string ("GL");

      else if (strstr (map->filename, "/libEGL."))
        tag = g_quark_from_static_string ("EGL");

      g_hash_table_insert (self->tag_cache,
                           g_strdup (map->filename),
                           GSIZE_TO_POINTER (tag));
    }

  return GPOINTER_TO_SIZE (g_hash_table_lookup (self->tag_cache, map->filename));
}

static gchar *
sp_elf_symbol_resolver_resolve_with_context (SpSymbolResolver *resolver,
                                             guint64           time,
                                             GPid              pid,
                                             SpAddressContext  context,
                                             SpCaptureAddress  address,
                                             GQuark           *tag)
{
  SpElfSymbolResolver *self = (SpElfSymbolResolver *)resolver;
  const bin_symbol_t *bin_sym;
  SpMapLookaside *lookaside;
  const gchar *bin_sym_name;
  const SpMap *map;
  bin_file_t *bin_file;

  g_assert (SP_IS_ELF_SYMBOL_RESOLVER (self));

  if (context != SP_ADDRESS_CONTEXT_USER)
    return NULL;

  lookaside = g_hash_table_lookup (self->lookasides, GINT_TO_POINTER (pid));
  if (lookaside == NULL)
    return NULL;

  map = sp_map_lookaside_lookup (lookaside, address);
  if (map == NULL)
    return NULL;

  address -= map->start;
  address += map->offset;

  bin_file = sp_elf_symbol_resolver_get_bin_file (self, map->filename);

  g_assert (bin_file != NULL);

  if (map->inode && !bin_file_check_inode (bin_file, map->inode))
    return g_strdup_printf ("%s: inode mismatch", map->filename);

  bin_sym = bin_file_lookup_symbol (bin_file, address);
  bin_sym_name = bin_symbol_get_name (bin_file, bin_sym);

  if (map->filename)
    *tag = guess_tag (self, map);

  return elf_demangle (bin_sym_name);
}

static void
symbol_resolver_iface_init (SpSymbolResolverInterface *iface)
{
  iface->load = sp_elf_symbol_resolver_load;
  iface->resolve_with_context = sp_elf_symbol_resolver_resolve_with_context;
}

SpSymbolResolver *
sp_elf_symbol_resolver_new (void)
{
  return g_object_new (SP_TYPE_ELF_SYMBOL_RESOLVER, NULL);
}