-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpy-minsymbol.c
More file actions
509 lines (424 loc) · 13.2 KB
/
py-minsymbol.c
File metadata and controls
509 lines (424 loc) · 13.2 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/* Python interface to minsymbols.
Copyright (C) 2008-2013 Free Software Foundation, Inc.
This file is part of GDB.
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 "defs.h"
#include "block.h"
#include "exceptions.h"
#include "frame.h"
#include "symtab.h"
#include "python-internal.h"
#include "objfiles.h"
#include "value.h"
#include "py-ref.h"
typedef struct msympy_symbol_object {
PyObject_HEAD
/* The GDB bound_minimal_symbol structure this object is wrapping. */
struct bound_minimal_symbol bound;
/* A minsym object is associated with an objfile, so keep track with
doubly-linked list, rooted in the objfile. This lets us
invalidate the underlying struct minimal_symbol when the objfile is
deleted. */
struct msympy_symbol_object *prev;
struct msympy_symbol_object *next;
} minsym_object;
/* Return the symbol that is wrapped by this symbol object. */
static struct minimal_symbol *
minsym_object_to_minsym (PyObject *obj)
{
if (! PyObject_TypeCheck (obj, &minsym_object_type))
return NULL;
return ((minsym_object *) obj)->bound.minsym;
}
static struct objfile *
minsym_object_to_objfile (PyObject *obj)
{
if (! PyObject_TypeCheck (obj, &minsym_object_type))
return NULL;
return ((minsym_object *) obj)->bound.objfile;
}
/* Require a valid symbol. All access to minsym_object->symbol should be
gated by this call. */
#define MSYMPY_REQUIRE_VALID(minsym_obj, minsym) \
do { \
minsym = minsym_object_to_minsym (minsym_obj); \
if (minsym == NULL) \
{ \
PyErr_SetString (PyExc_RuntimeError, \
_("MinSymbol is invalid.")); \
return NULL; \
} \
} while (0)
#define MSYMPY_REQUIRE_VALID_BOUND(minsym_obj, minsym, objfile) \
do { \
minsym = minsym_object_to_minsym (minsym_obj); \
objfile = minsym_object_to_objfile (minsym_obj); \
if (minsym == NULL || objfile == NULL) \
{ \
PyErr_SetString (PyExc_RuntimeError, \
_("MinSymbol is invalid.")); \
return NULL; \
} \
} while (0)
static const struct objfile_data *msympy_objfile_data_key;
static PyObject *
msympy_str (PyObject *self)
{
struct minimal_symbol *minsym = NULL;
MSYMPY_REQUIRE_VALID (self, minsym);
return PyString_FromString (MSYMBOL_PRINT_NAME (minsym));
}
static PyObject *
msympy_get_name (PyObject *self, void *closure)
{
struct minimal_symbol *minsym = NULL;
MSYMPY_REQUIRE_VALID (self, minsym);
return PyString_FromString (MSYMBOL_NATURAL_NAME (minsym));
}
static PyObject *
msympy_get_file_name (PyObject *self, void *closure)
{
struct minimal_symbol *minsym = NULL;
MSYMPY_REQUIRE_VALID (self, minsym);
return PyString_FromString (minsym->filename);
}
static PyObject *
msympy_get_linkage_name (PyObject *self, void *closure)
{
struct minimal_symbol *minsym = NULL;
MSYMPY_REQUIRE_VALID (self, minsym);
return PyString_FromString (MSYMBOL_LINKAGE_NAME (minsym));
}
static PyObject *
msympy_get_print_name (PyObject *self, void *closure)
{
struct minimal_symbol *minsym = NULL;
MSYMPY_REQUIRE_VALID (self, minsym);
return msympy_str (self);
}
static PyObject *
msympy_get_section (PyObject *self, void *closure)
{
struct minimal_symbol *minsym = NULL;
struct objfile *objfile = NULL;
struct obj_section *section;
const char *name;
MSYMPY_REQUIRE_VALID_BOUND (self, minsym, objfile);
section = MSYMBOL_OBJ_SECTION (objfile, minsym);
if (section) {
name = bfd_section_name (objfile->obfd, section->the_bfd_section);
if (name)
return PyString_FromString (name);
}
Py_RETURN_NONE;
}
static PyObject *
msympy_get_type (PyObject *self, void *closure)
{
struct minimal_symbol *minsym = NULL;
MSYMPY_REQUIRE_VALID (self, minsym);
return PyInt_FromLong(MSYMBOL_TYPE(minsym));
}
static PyObject *
msympy_is_valid (PyObject *self, PyObject *args)
{
struct minimal_symbol *minsym = NULL;
minsym = minsym_object_to_minsym (self);
if (minsym == NULL)
Py_RETURN_FALSE;
Py_RETURN_TRUE;
}
static struct gdbarch *
minsym_gdbarch(PyObject *minsym_obj)
{
return get_objfile_arch (minsym_object_to_objfile(minsym_obj));
}
static struct type *
minsym_type(PyObject *minsym_obj)
{
struct type *type;
switch (minsym_object_to_minsym(minsym_obj)->type) {
case mst_text:
case mst_solib_trampoline:
case mst_file_text:
case mst_text_gnu_ifunc:
case mst_slot_got_plt:
type = builtin_type (minsym_gdbarch (minsym_obj))->builtin_func_ptr;
break;
case mst_data:
case mst_abs:
case mst_bss:
case mst_file_data:
case mst_file_bss:
type = builtin_type (minsym_gdbarch (minsym_obj))->builtin_data_ptr;
break;
case mst_unknown:
default:
type = builtin_type (minsym_gdbarch (minsym_obj))->builtin_void;
break;
}
return type;
}
static PyObject *
msympy_is_code (PyObject *self, PyObject *args)
{
struct minimal_symbol *minsym = NULL;
struct type *type;
MSYMPY_REQUIRE_VALID (self, minsym);
type = builtin_type (minsym_gdbarch (self))->builtin_func_ptr;
if (minsym_type(self) == type)
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
static PyObject *
msympy_is_data (PyObject *self, PyObject *args)
{
struct minimal_symbol *minsym = NULL;
struct type *type;
MSYMPY_REQUIRE_VALID (self, minsym);
type = builtin_type (minsym_gdbarch (self))->builtin_data_ptr;
if (minsym_type(self) == type)
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
/* Implementation of gdb.MinSymbol.value (self) -> gdb.Value. Returns
the value of the symbol, or an error in various circumstances. */
static PyObject *
msympy_value (PyObject *self, PyObject *args)
{
minsym_object *minsym_obj = (minsym_object *)self;
struct minimal_symbol *minsym = NULL;
struct value *value = NULL;
if (!PyArg_ParseTuple (args, ""))
return NULL;
MSYMPY_REQUIRE_VALID (self, minsym);
TRY
{
value = value_at_lazy (minsym_type (self),
BMSYMBOL_VALUE_ADDRESS(minsym_obj->bound));
}
CATCH (except, RETURN_MASK_ALL)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
END_CATCH
return value_to_value_object (value);
}
static void
set_symbol (minsym_object *obj, struct bound_minimal_symbol *bound)
{
obj->bound = *bound;
obj->prev = NULL;
if (bound->objfile)
{
obj->next = (minsym_object *) objfile_data (bound->objfile,
msympy_objfile_data_key);
if (obj->next)
obj->next->prev = obj;
set_objfile_data (bound->objfile, msympy_objfile_data_key, obj);
}
else
obj->next = NULL;
}
static PyObject *
bound_minsym_to_minsym_object (struct bound_minimal_symbol *bound)
{
minsym_object *msym_obj;
msym_obj = PyObject_New (minsym_object, &minsym_object_type);
if (msym_obj)
set_symbol (msym_obj, bound);
return (PyObject *) msym_obj;
}
static void
msympy_dealloc (PyObject *obj)
{
minsym_object *msym_obj = (minsym_object *) obj;
if (msym_obj->prev)
msym_obj->prev->next = msym_obj->next;
else if (msym_obj->bound.objfile)
set_objfile_data (msym_obj->bound.objfile,
msympy_objfile_data_key, msym_obj->next);
if (msym_obj->next)
msym_obj->next->prev = msym_obj->prev;
msym_obj->bound.minsym = NULL;
msym_obj->bound.objfile = NULL;
}
char *
gdb_PyUnicode_AsUTF8(PyObject *obj)
{
#ifdef IS_PY3K
return PyUnicode_AsUTF8(obj);
#else
gdbpy_ref<> ucode(PyUnicode_AsUTF8String(obj));
return PyBytes_AsString(ucode.get());
#endif
}
/* Implementation of
gdb.lookup_minimal_symbol (name, [sfile, [objfile]]) -> symbol or None. */
PyObject *
gdbpy_lookup_minimal_symbol (PyObject *self, PyObject *args, PyObject *kw)
{
const char *name, *sfile = NULL;
struct objfile *objfile = NULL;
static const char *keywords[] = { "name", "sfile", "objfile", NULL };
struct bound_minimal_symbol bound_minsym = {};
PyObject *msym_obj = NULL, *sfile_obj = NULL, *objfile_obj = NULL;
if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|OO", keywords, &name,
&sfile_obj, &objfile_obj))
return NULL;
if (sfile_obj && sfile_obj != Py_None)
{
if (PyUnicode_Check(sfile_obj))
{
sfile = gdb_PyUnicode_AsUTF8(sfile_obj);
}
else if (PyBytes_Check(sfile_obj))
{
sfile = PyBytes_AS_STRING(sfile_obj); // Borrowed pointer
}
else
{
/* wtf is this garbage */
}
if (!sfile)
return NULL;
}
if (objfile_obj && objfile_obj != Py_None)
{
objfile = objfpy_object_to_objfile (objfile_obj);
if (!objfile)
return NULL;
}
TRY
{
bound_minsym = lookup_minimal_symbol (name, sfile, objfile);
}
CATCH (except, RETURN_MASK_ALL)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
END_CATCH
if (bound_minsym.minsym)
msym_obj = bound_minsym_to_minsym_object (&bound_minsym);
if (msym_obj)
return msym_obj;
Py_RETURN_NONE;
}
static void
del_objfile_msymbols (struct objfile *objfile, void *datum)
{
minsym_object *obj = (minsym_object *) datum;
while (obj)
{
minsym_object *next = obj->next;
obj->bound.minsym = NULL;
obj->bound.objfile = NULL;
obj->next = NULL;
obj->prev = NULL;
obj = next;
}
}
int
gdbpy_initialize_minsymbols (void)
{
if (PyType_Ready (&minsym_object_type) < 0)
return -1;
msympy_objfile_data_key
= register_objfile_data_with_cleanup (NULL, del_objfile_msymbols);
if (PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_UNKNOWN",
mst_unknown) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_TEXT", mst_text) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_TEXT_GNU_IFUNC",
mst_text_gnu_ifunc) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_SLOT_GOT_PLT",
mst_slot_got_plt) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_DATA", mst_data) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_BSS", mst_bss) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_ABS", mst_abs) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_SOLIB_TRAMPOLINE",
mst_solib_trampoline) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_FILE_TEXT",
mst_file_text) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_FILE_DATA",
mst_file_data) < 0
|| PyModule_AddIntConstant (gdb_module, "MINSYMBOL_TYPE_FILE_BSS",
mst_file_bss) < 0)
return -1;
return gdb_pymodule_addobject (gdb_module, "MinSymbol",
(PyObject *) &minsym_object_type);
}
static gdb_PyGetSetDef minsym_object_getset[] = {
{ "name", msympy_get_name, NULL,
"Name of the minimal symbol, as it appears in the source code.", NULL },
{ "linkage_name", msympy_get_linkage_name, NULL,
"Name of the minimal symbol, as used by the linker (i.e., may be mangled).",
NULL },
{ "filename", msympy_get_file_name, NULL,
"Name of source file that contains this minimal symbol. Only applies for mst_file_*.",
NULL },
{ "print_name", msympy_get_print_name, NULL,
"Name of the minimal symbol in a form suitable for output.\n\
This is either name or linkage_name, depending on whether the user asked GDB\n\
to display demangled or mangled names.", NULL },
{ "section", msympy_get_section, NULL,
"Section that contains this minimal symbol, if any", NULL, },
{ "type", msympy_get_type, NULL,
"Type that this minimal symbol represents." },
{ NULL } /* Sentinel */
};
static PyMethodDef minsym_object_methods[] = {
{ "is_valid", msympy_is_valid, METH_NOARGS,
"is_valid () -> Boolean.\n\
Return true if this minimal symbol is valid, false if not." },
{ "is_code", msympy_is_code, METH_NOARGS,
"is_code () -> Boolean.\n\
Return true if this minimal symbol represents code." },
{ "is_data", msympy_is_data, METH_NOARGS,
"is_data () -> Boolean.\n\
Return true if this minimal symbol represents data." },
{ "value", msympy_value, METH_VARARGS,
"value ([frame]) -> gdb.Value\n\
Return the value of the minimal symbol." },
{NULL} /* Sentinel */
};
PyTypeObject minsym_object_type = {
PyVarObject_HEAD_INIT (NULL, 0)
"gdb.MinSymbol", /*tp_name*/
sizeof (minsym_object), /*tp_basicsize*/
0, /*tp_itemsize*/
msympy_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
msympy_str, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"GDB minimal symbol object", /*tp_doc */
0, /*tp_traverse */
0, /*tp_clear */
0, /*tp_richcompare */
0, /*tp_weaklistoffset */
0, /*tp_iter */
0, /*tp_iternext */
minsym_object_methods, /*tp_methods */
0, /*tp_members */
minsym_object_getset /*tp_getset */
};