Commit | Line | Data |
---|---|---|
f8f6f20b TJB |
1 | /* Python interface to stack frames |
2 | ||
28e7fd62 | 3 | Copyright (C) 2008-2013 Free Software Foundation, Inc. |
f8f6f20b TJB |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
21 | #include "charset.h" | |
22 | #include "block.h" | |
23 | #include "frame.h" | |
24 | #include "exceptions.h" | |
25 | #include "symtab.h" | |
26 | #include "stack.h" | |
27 | #include "value.h" | |
28 | #include "python-internal.h" | |
f3e9a817 PM |
29 | #include "symfile.h" |
30 | #include "objfiles.h" | |
f8f6f20b TJB |
31 | |
32 | typedef struct { | |
33 | PyObject_HEAD | |
34 | struct frame_id frame_id; | |
35 | struct gdbarch *gdbarch; | |
36 | ||
37 | /* Marks that the FRAME_ID member actually holds the ID of the frame next | |
38 | to this, and not this frames' ID itself. This is a hack to permit Python | |
39 | frame objects which represent invalid frames (i.e., the last frame_info | |
40 | in a corrupt stack). The problem arises from the fact that this code | |
41 | relies on FRAME_ID to uniquely identify a frame, which is not always true | |
42 | for the last "frame" in a corrupt stack (it can have a null ID, or the same | |
43 | ID as the previous frame). Whenever get_prev_frame returns NULL, we | |
44 | record the frame_id of the next frame and set FRAME_ID_IS_NEXT to 1. */ | |
45 | int frame_id_is_next; | |
46 | } frame_object; | |
47 | ||
48 | /* Require a valid frame. This must be called inside a TRY_CATCH, or | |
49 | another context in which a gdb exception is allowed. */ | |
50 | #define FRAPY_REQUIRE_VALID(frame_obj, frame) \ | |
51 | do { \ | |
52 | frame = frame_object_to_frame_info (frame_obj); \ | |
53 | if (frame == NULL) \ | |
044c0f87 | 54 | error (_("Frame is invalid.")); \ |
f8f6f20b TJB |
55 | } while (0) |
56 | ||
f8f6f20b TJB |
57 | /* Returns the frame_info object corresponding to the given Python Frame |
58 | object. If the frame doesn't exist anymore (the frame id doesn't | |
59 | correspond to any frame in the inferior), returns NULL. */ | |
60 | ||
cc72b2a2 KP |
61 | struct frame_info * |
62 | frame_object_to_frame_info (PyObject *obj) | |
f8f6f20b | 63 | { |
cc72b2a2 | 64 | frame_object *frame_obj = (frame_object *) obj; |
f8f6f20b TJB |
65 | struct frame_info *frame; |
66 | ||
67 | frame = frame_find_by_id (frame_obj->frame_id); | |
68 | if (frame == NULL) | |
69 | return NULL; | |
70 | ||
71 | if (frame_obj->frame_id_is_next) | |
72 | frame = get_prev_frame (frame); | |
73 | ||
74 | return frame; | |
75 | } | |
76 | ||
77 | /* Called by the Python interpreter to obtain string representation | |
78 | of the object. */ | |
79 | ||
80 | static PyObject * | |
81 | frapy_str (PyObject *self) | |
82 | { | |
83 | char *s; | |
f8f6f20b TJB |
84 | PyObject *result; |
85 | struct ui_file *strfile; | |
86 | ||
87 | strfile = mem_fileopen (); | |
88 | fprint_frame_id (strfile, ((frame_object *) self)->frame_id); | |
759ef836 | 89 | s = ui_file_xstrdup (strfile, NULL); |
f8f6f20b TJB |
90 | result = PyString_FromString (s); |
91 | xfree (s); | |
92 | ||
93 | return result; | |
94 | } | |
95 | ||
96 | /* Implementation of gdb.Frame.is_valid (self) -> Boolean. | |
97 | Returns True if the frame corresponding to the frame_id of this | |
98 | object still exists in the inferior. */ | |
99 | ||
100 | static PyObject * | |
101 | frapy_is_valid (PyObject *self, PyObject *args) | |
102 | { | |
76dce0be PM |
103 | struct frame_info *frame = NULL; |
104 | volatile struct gdb_exception except; | |
105 | ||
106 | TRY_CATCH (except, RETURN_MASK_ALL) | |
107 | { | |
cc72b2a2 | 108 | frame = frame_object_to_frame_info (self); |
76dce0be PM |
109 | } |
110 | GDB_PY_HANDLE_EXCEPTION (except); | |
f8f6f20b | 111 | |
f8f6f20b TJB |
112 | if (frame == NULL) |
113 | Py_RETURN_FALSE; | |
114 | ||
115 | Py_RETURN_TRUE; | |
116 | } | |
117 | ||
118 | /* Implementation of gdb.Frame.name (self) -> String. | |
119 | Returns the name of the function corresponding to this frame. */ | |
120 | ||
121 | static PyObject * | |
122 | frapy_name (PyObject *self, PyObject *args) | |
123 | { | |
124 | struct frame_info *frame; | |
0d5cff50 | 125 | const char *name; |
f8f6f20b TJB |
126 | enum language lang; |
127 | PyObject *result; | |
128 | volatile struct gdb_exception except; | |
129 | ||
130 | TRY_CATCH (except, RETURN_MASK_ALL) | |
131 | { | |
cc72b2a2 | 132 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b | 133 | |
e9e07ba6 | 134 | find_frame_funname (frame, &name, &lang, NULL); |
f8f6f20b TJB |
135 | } |
136 | GDB_PY_HANDLE_EXCEPTION (except); | |
137 | ||
138 | if (name) | |
139 | result = PyUnicode_Decode (name, strlen (name), host_charset (), NULL); | |
140 | else | |
141 | { | |
142 | result = Py_None; | |
143 | Py_INCREF (Py_None); | |
144 | } | |
145 | ||
146 | return result; | |
147 | } | |
148 | ||
149 | /* Implementation of gdb.Frame.type (self) -> Integer. | |
150 | Returns the frame type, namely one of the gdb.*_FRAME constants. */ | |
151 | ||
152 | static PyObject * | |
153 | frapy_type (PyObject *self, PyObject *args) | |
154 | { | |
155 | struct frame_info *frame; | |
156 | enum frame_type type = NORMAL_FRAME;/* Initialize to appease gcc warning. */ | |
157 | volatile struct gdb_exception except; | |
158 | ||
159 | TRY_CATCH (except, RETURN_MASK_ALL) | |
160 | { | |
cc72b2a2 | 161 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
162 | |
163 | type = get_frame_type (frame); | |
164 | } | |
165 | GDB_PY_HANDLE_EXCEPTION (except); | |
166 | ||
167 | return PyInt_FromLong (type); | |
168 | } | |
169 | ||
bea883fd SCR |
170 | /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture. |
171 | Returns the frame's architecture as a gdb.Architecture object. */ | |
172 | ||
173 | static PyObject * | |
174 | frapy_arch (PyObject *self, PyObject *args) | |
175 | { | |
176 | struct frame_info *frame = NULL; /* Initialize to appease gcc warning. */ | |
177 | frame_object *obj = (frame_object *) self; | |
178 | volatile struct gdb_exception except; | |
179 | ||
180 | TRY_CATCH (except, RETURN_MASK_ALL) | |
181 | { | |
182 | FRAPY_REQUIRE_VALID (self, frame); | |
183 | } | |
184 | GDB_PY_HANDLE_EXCEPTION (except); | |
185 | ||
186 | return gdbarch_to_arch_object (obj->gdbarch); | |
187 | } | |
188 | ||
f8f6f20b TJB |
189 | /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer. |
190 | Returns one of the gdb.FRAME_UNWIND_* constants. */ | |
191 | ||
192 | static PyObject * | |
193 | frapy_unwind_stop_reason (PyObject *self, PyObject *args) | |
194 | { | |
195 | struct frame_info *frame = NULL; /* Initialize to appease gcc warning. */ | |
196 | volatile struct gdb_exception except; | |
197 | enum unwind_stop_reason stop_reason; | |
198 | ||
199 | TRY_CATCH (except, RETURN_MASK_ALL) | |
200 | { | |
cc72b2a2 | 201 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
202 | } |
203 | GDB_PY_HANDLE_EXCEPTION (except); | |
204 | ||
205 | stop_reason = get_frame_unwind_stop_reason (frame); | |
206 | ||
207 | return PyInt_FromLong (stop_reason); | |
208 | } | |
209 | ||
210 | /* Implementation of gdb.Frame.pc (self) -> Long. | |
211 | Returns the frame's resume address. */ | |
212 | ||
213 | static PyObject * | |
214 | frapy_pc (PyObject *self, PyObject *args) | |
215 | { | |
216 | CORE_ADDR pc = 0; /* Initialize to appease gcc warning. */ | |
217 | struct frame_info *frame; | |
218 | volatile struct gdb_exception except; | |
219 | ||
220 | TRY_CATCH (except, RETURN_MASK_ALL) | |
221 | { | |
cc72b2a2 | 222 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
223 | |
224 | pc = get_frame_pc (frame); | |
225 | } | |
226 | GDB_PY_HANDLE_EXCEPTION (except); | |
227 | ||
74aedc46 | 228 | return gdb_py_long_from_ulongest (pc); |
f8f6f20b TJB |
229 | } |
230 | ||
f3e9a817 PM |
231 | /* Implementation of gdb.Frame.block (self) -> gdb.Block. |
232 | Returns the frame's code block. */ | |
233 | ||
234 | static PyObject * | |
235 | frapy_block (PyObject *self, PyObject *args) | |
236 | { | |
237 | struct frame_info *frame; | |
57126e4a | 238 | struct block *block = NULL, *fn_block; |
f3e9a817 | 239 | volatile struct gdb_exception except; |
f3e9a817 PM |
240 | |
241 | TRY_CATCH (except, RETURN_MASK_ALL) | |
242 | { | |
cc72b2a2 | 243 | FRAPY_REQUIRE_VALID (self, frame); |
57126e4a | 244 | block = get_frame_block (frame, NULL); |
f3e9a817 PM |
245 | } |
246 | GDB_PY_HANDLE_EXCEPTION (except); | |
247 | ||
57126e4a TT |
248 | for (fn_block = block; |
249 | fn_block != NULL && BLOCK_FUNCTION (fn_block) == NULL; | |
250 | fn_block = BLOCK_SUPERBLOCK (fn_block)) | |
251 | ; | |
252 | ||
253 | if (block == NULL || fn_block == NULL || BLOCK_FUNCTION (fn_block) == NULL) | |
f3e9a817 PM |
254 | { |
255 | PyErr_SetString (PyExc_RuntimeError, | |
044c0f87 | 256 | _("Cannot locate object file for block.")); |
f3e9a817 PM |
257 | return NULL; |
258 | } | |
259 | ||
260 | if (block) | |
57126e4a TT |
261 | { |
262 | struct symtab *symt; | |
263 | ||
264 | symt = SYMBOL_SYMTAB (BLOCK_FUNCTION (fn_block)); | |
265 | return block_to_block_object (block, symt->objfile); | |
266 | } | |
f3e9a817 PM |
267 | |
268 | Py_RETURN_NONE; | |
269 | } | |
270 | ||
271 | ||
272 | /* Implementation of gdb.Frame.function (self) -> gdb.Symbol. | |
273 | Returns the symbol for the function corresponding to this frame. */ | |
274 | ||
275 | static PyObject * | |
276 | frapy_function (PyObject *self, PyObject *args) | |
277 | { | |
278 | struct symbol *sym = NULL; | |
279 | struct frame_info *frame; | |
280 | volatile struct gdb_exception except; | |
281 | ||
282 | TRY_CATCH (except, RETURN_MASK_ALL) | |
283 | { | |
cc72b2a2 | 284 | FRAPY_REQUIRE_VALID (self, frame); |
f3e9a817 PM |
285 | |
286 | sym = find_pc_function (get_frame_address_in_block (frame)); | |
287 | } | |
288 | GDB_PY_HANDLE_EXCEPTION (except); | |
289 | ||
290 | if (sym) | |
291 | return symbol_to_symbol_object (sym); | |
292 | ||
293 | Py_RETURN_NONE; | |
294 | } | |
295 | ||
f8f6f20b TJB |
296 | /* Convert a frame_info struct to a Python Frame object. |
297 | Sets a Python exception and returns NULL on error. */ | |
298 | ||
595939de | 299 | PyObject * |
f8f6f20b TJB |
300 | frame_info_to_frame_object (struct frame_info *frame) |
301 | { | |
302 | frame_object *frame_obj; | |
76dce0be | 303 | volatile struct gdb_exception except; |
f8f6f20b TJB |
304 | |
305 | frame_obj = PyObject_New (frame_object, &frame_object_type); | |
306 | if (frame_obj == NULL) | |
6cbc7c3d | 307 | return NULL; |
f8f6f20b | 308 | |
76dce0be | 309 | TRY_CATCH (except, RETURN_MASK_ALL) |
f8f6f20b | 310 | { |
f8f6f20b | 311 | |
76dce0be PM |
312 | /* Try to get the previous frame, to determine if this is the last frame |
313 | in a corrupt stack. If so, we need to store the frame_id of the next | |
314 | frame and not of this one (which is possibly invalid). */ | |
315 | if (get_prev_frame (frame) == NULL | |
316 | && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON | |
317 | && get_next_frame (frame) != NULL) | |
318 | { | |
319 | frame_obj->frame_id = get_frame_id (get_next_frame (frame)); | |
320 | frame_obj->frame_id_is_next = 1; | |
321 | } | |
322 | else | |
323 | { | |
324 | frame_obj->frame_id = get_frame_id (frame); | |
325 | frame_obj->frame_id_is_next = 0; | |
326 | } | |
327 | frame_obj->gdbarch = get_frame_arch (frame); | |
328 | } | |
329 | GDB_PY_HANDLE_EXCEPTION (except); | |
f8f6f20b | 330 | |
595939de | 331 | return (PyObject *) frame_obj; |
f8f6f20b TJB |
332 | } |
333 | ||
334 | /* Implementation of gdb.Frame.older (self) -> gdb.Frame. | |
335 | Returns the frame immediately older (outer) to this frame, or None if | |
336 | there isn't one. */ | |
337 | ||
338 | static PyObject * | |
339 | frapy_older (PyObject *self, PyObject *args) | |
340 | { | |
341 | struct frame_info *frame, *prev; | |
342 | volatile struct gdb_exception except; | |
343 | PyObject *prev_obj = NULL; /* Initialize to appease gcc warning. */ | |
344 | ||
345 | TRY_CATCH (except, RETURN_MASK_ALL) | |
346 | { | |
cc72b2a2 | 347 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
348 | |
349 | prev = get_prev_frame (frame); | |
350 | if (prev) | |
351 | prev_obj = (PyObject *) frame_info_to_frame_object (prev); | |
352 | else | |
353 | { | |
354 | Py_INCREF (Py_None); | |
355 | prev_obj = Py_None; | |
356 | } | |
357 | } | |
358 | GDB_PY_HANDLE_EXCEPTION (except); | |
359 | ||
360 | return prev_obj; | |
361 | } | |
362 | ||
363 | /* Implementation of gdb.Frame.newer (self) -> gdb.Frame. | |
364 | Returns the frame immediately newer (inner) to this frame, or None if | |
365 | there isn't one. */ | |
366 | ||
367 | static PyObject * | |
368 | frapy_newer (PyObject *self, PyObject *args) | |
369 | { | |
370 | struct frame_info *frame, *next; | |
371 | volatile struct gdb_exception except; | |
372 | PyObject *next_obj = NULL; /* Initialize to appease gcc warning. */ | |
373 | ||
374 | TRY_CATCH (except, RETURN_MASK_ALL) | |
375 | { | |
cc72b2a2 | 376 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
377 | |
378 | next = get_next_frame (frame); | |
379 | if (next) | |
380 | next_obj = (PyObject *) frame_info_to_frame_object (next); | |
381 | else | |
382 | { | |
383 | Py_INCREF (Py_None); | |
384 | next_obj = Py_None; | |
385 | } | |
386 | } | |
387 | GDB_PY_HANDLE_EXCEPTION (except); | |
388 | ||
389 | return next_obj; | |
390 | } | |
391 | ||
f3e9a817 PM |
392 | /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line. |
393 | Returns the frame's symtab and line. */ | |
394 | ||
395 | static PyObject * | |
396 | frapy_find_sal (PyObject *self, PyObject *args) | |
397 | { | |
398 | struct frame_info *frame; | |
399 | struct symtab_and_line sal; | |
f3e9a817 PM |
400 | volatile struct gdb_exception except; |
401 | PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */ | |
402 | ||
403 | TRY_CATCH (except, RETURN_MASK_ALL) | |
404 | { | |
cc72b2a2 | 405 | FRAPY_REQUIRE_VALID (self, frame); |
f3e9a817 PM |
406 | |
407 | find_frame_sal (frame, &sal); | |
408 | sal_obj = symtab_and_line_to_sal_object (sal); | |
409 | } | |
410 | GDB_PY_HANDLE_EXCEPTION (except); | |
411 | ||
412 | return sal_obj; | |
413 | } | |
414 | ||
dc00d89f PM |
415 | /* Implementation of gdb.Frame.read_var_value (self, variable, |
416 | [block]) -> gdb.Value. If the optional block argument is provided | |
417 | start the search from that block, otherwise search from the frame's | |
418 | current block (determined by examining the resume address of the | |
419 | frame). The variable argument must be a string or an instance of a | |
8dc78533 JK |
420 | gdb.Symbol. The block argument must be an instance of gdb.Block. Returns |
421 | NULL on error, with a python exception set. */ | |
f8f6f20b TJB |
422 | static PyObject * |
423 | frapy_read_var (PyObject *self, PyObject *args) | |
424 | { | |
425 | struct frame_info *frame; | |
dc00d89f | 426 | PyObject *sym_obj, *block_obj = NULL; |
f8f6f20b TJB |
427 | struct symbol *var = NULL; /* gcc-4.3.2 false warning. */ |
428 | struct value *val = NULL; | |
429 | volatile struct gdb_exception except; | |
430 | ||
dc00d89f | 431 | if (!PyArg_ParseTuple (args, "O|O", &sym_obj, &block_obj)) |
f8f6f20b TJB |
432 | return NULL; |
433 | ||
f3e9a817 PM |
434 | if (PyObject_TypeCheck (sym_obj, &symbol_object_type)) |
435 | var = symbol_object_to_symbol (sym_obj); | |
436 | else if (gdbpy_is_string (sym_obj)) | |
f8f6f20b TJB |
437 | { |
438 | char *var_name; | |
9df2fbc4 | 439 | const struct block *block = NULL; |
f8f6f20b TJB |
440 | struct cleanup *cleanup; |
441 | volatile struct gdb_exception except; | |
442 | ||
443 | var_name = python_string_to_target_string (sym_obj); | |
444 | if (!var_name) | |
445 | return NULL; | |
446 | cleanup = make_cleanup (xfree, var_name); | |
447 | ||
dc00d89f PM |
448 | if (block_obj) |
449 | { | |
450 | block = block_object_to_block (block_obj); | |
451 | if (!block) | |
452 | { | |
453 | PyErr_SetString (PyExc_RuntimeError, | |
454 | _("Second argument must be block.")); | |
455 | return NULL; | |
456 | } | |
457 | } | |
458 | ||
f8f6f20b TJB |
459 | TRY_CATCH (except, RETURN_MASK_ALL) |
460 | { | |
cc72b2a2 | 461 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b | 462 | |
dc00d89f | 463 | if (!block) |
626e7282 | 464 | block = get_frame_block (frame, NULL); |
f8f6f20b TJB |
465 | var = lookup_symbol (var_name, block, VAR_DOMAIN, NULL); |
466 | } | |
467 | GDB_PY_HANDLE_EXCEPTION (except); | |
468 | ||
469 | if (!var) | |
470 | { | |
471 | PyErr_Format (PyExc_ValueError, | |
044c0f87 | 472 | _("Variable '%s' not found."), var_name); |
f8f6f20b TJB |
473 | do_cleanups (cleanup); |
474 | ||
475 | return NULL; | |
476 | } | |
477 | ||
478 | do_cleanups (cleanup); | |
479 | } | |
480 | else | |
481 | { | |
482 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 483 | _("Argument must be a symbol or string.")); |
f8f6f20b TJB |
484 | return NULL; |
485 | } | |
486 | ||
487 | TRY_CATCH (except, RETURN_MASK_ALL) | |
488 | { | |
cc72b2a2 | 489 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
490 | |
491 | val = read_var_value (var, frame); | |
492 | } | |
493 | GDB_PY_HANDLE_EXCEPTION (except); | |
494 | ||
dc00d89f | 495 | return value_to_value_object (val); |
f8f6f20b TJB |
496 | } |
497 | ||
f3e9a817 PM |
498 | /* Select this frame. */ |
499 | ||
500 | static PyObject * | |
501 | frapy_select (PyObject *self, PyObject *args) | |
502 | { | |
503 | struct frame_info *fi; | |
f3e9a817 PM |
504 | volatile struct gdb_exception except; |
505 | ||
506 | TRY_CATCH (except, RETURN_MASK_ALL) | |
507 | { | |
cc72b2a2 | 508 | FRAPY_REQUIRE_VALID (self, fi); |
f3e9a817 PM |
509 | |
510 | select_frame (fi); | |
511 | } | |
512 | GDB_PY_HANDLE_EXCEPTION (except); | |
513 | ||
514 | Py_RETURN_NONE; | |
515 | } | |
516 | ||
d8e22779 TT |
517 | /* Implementation of gdb.newest_frame () -> gdb.Frame. |
518 | Returns the newest frame object. */ | |
519 | ||
520 | PyObject * | |
521 | gdbpy_newest_frame (PyObject *self, PyObject *args) | |
522 | { | |
523 | struct frame_info *frame; | |
524 | PyObject *frame_obj = NULL; /* Initialize to appease gcc warning. */ | |
525 | volatile struct gdb_exception except; | |
526 | ||
527 | TRY_CATCH (except, RETURN_MASK_ALL) | |
528 | { | |
529 | frame = get_current_frame (); | |
530 | frame_obj = frame_info_to_frame_object (frame); | |
531 | } | |
532 | GDB_PY_HANDLE_EXCEPTION (except); | |
533 | ||
534 | return frame_obj; | |
535 | } | |
536 | ||
f8f6f20b TJB |
537 | /* Implementation of gdb.selected_frame () -> gdb.Frame. |
538 | Returns the selected frame object. */ | |
539 | ||
540 | PyObject * | |
541 | gdbpy_selected_frame (PyObject *self, PyObject *args) | |
542 | { | |
543 | struct frame_info *frame; | |
595939de | 544 | PyObject *frame_obj = NULL; /* Initialize to appease gcc warning. */ |
f8f6f20b TJB |
545 | volatile struct gdb_exception except; |
546 | ||
547 | TRY_CATCH (except, RETURN_MASK_ALL) | |
548 | { | |
549 | frame = get_selected_frame ("No frame is currently selected."); | |
550 | frame_obj = frame_info_to_frame_object (frame); | |
551 | } | |
552 | GDB_PY_HANDLE_EXCEPTION (except); | |
553 | ||
595939de | 554 | return frame_obj; |
f8f6f20b TJB |
555 | } |
556 | ||
557 | /* Implementation of gdb.stop_reason_string (Integer) -> String. | |
558 | Return a string explaining the unwind stop reason. */ | |
559 | ||
560 | PyObject * | |
561 | gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args) | |
562 | { | |
563 | int reason; | |
564 | const char *str; | |
565 | ||
566 | if (!PyArg_ParseTuple (args, "i", &reason)) | |
567 | return NULL; | |
568 | ||
2231f1fb | 569 | if (reason < UNWIND_FIRST || reason > UNWIND_LAST) |
f8f6f20b | 570 | { |
044c0f87 PM |
571 | PyErr_SetString (PyExc_ValueError, |
572 | _("Invalid frame stop reason.")); | |
f8f6f20b TJB |
573 | return NULL; |
574 | } | |
575 | ||
576 | str = frame_stop_reason_string (reason); | |
577 | return PyUnicode_Decode (str, strlen (str), host_charset (), NULL); | |
578 | } | |
579 | ||
580 | /* Implements the equality comparison for Frame objects. | |
581 | All other comparison operators will throw a TypeError Python exception, | |
582 | as they aren't valid for frames. */ | |
583 | ||
584 | static PyObject * | |
585 | frapy_richcompare (PyObject *self, PyObject *other, int op) | |
586 | { | |
18e8c3bc TT |
587 | int result; |
588 | ||
589 | if (!PyObject_TypeCheck (other, &frame_object_type) | |
590 | || (op != Py_EQ && op != Py_NE)) | |
f8f6f20b | 591 | { |
18e8c3bc TT |
592 | Py_INCREF (Py_NotImplemented); |
593 | return Py_NotImplemented; | |
f8f6f20b TJB |
594 | } |
595 | ||
596 | if (frame_id_eq (((frame_object *) self)->frame_id, | |
597 | ((frame_object *) other)->frame_id)) | |
18e8c3bc TT |
598 | result = Py_EQ; |
599 | else | |
600 | result = Py_NE; | |
f8f6f20b | 601 | |
18e8c3bc TT |
602 | if (op == result) |
603 | Py_RETURN_TRUE; | |
f8f6f20b TJB |
604 | Py_RETURN_FALSE; |
605 | } | |
606 | ||
607 | /* Sets up the Frame API in the gdb module. */ | |
608 | ||
609 | void | |
610 | gdbpy_initialize_frames (void) | |
611 | { | |
6a1b1664 | 612 | frame_object_type.tp_new = PyType_GenericNew; |
f8f6f20b TJB |
613 | if (PyType_Ready (&frame_object_type) < 0) |
614 | return; | |
615 | ||
9a2b4c1b MS |
616 | /* Note: These would probably be best exposed as class attributes of |
617 | Frame, but I don't know how to do it except by messing with the | |
618 | type's dictionary. That seems too messy. */ | |
f8f6f20b TJB |
619 | PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME); |
620 | PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME); | |
ccfc3d6e | 621 | PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME); |
111c6489 | 622 | PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME", TAILCALL_FRAME); |
f8f6f20b | 623 | PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME", SIGTRAMP_FRAME); |
ccfc3d6e | 624 | PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME); |
f8f6f20b | 625 | PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME", SENTINEL_FRAME); |
2231f1fb KP |
626 | |
627 | #define SET(name, description) \ | |
628 | PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name); | |
629 | #define FIRST_ERROR(name) \ | |
630 | PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name); | |
631 | #include "unwind_stop_reasons.def" | |
632 | #undef SET | |
f8f6f20b TJB |
633 | |
634 | Py_INCREF (&frame_object_type); | |
635 | PyModule_AddObject (gdb_module, "Frame", (PyObject *) &frame_object_type); | |
636 | } | |
637 | ||
638 | \f | |
639 | ||
640 | static PyMethodDef frame_object_methods[] = { | |
641 | { "is_valid", frapy_is_valid, METH_NOARGS, | |
642 | "is_valid () -> Boolean.\n\ | |
643 | Return true if this frame is valid, false if not." }, | |
644 | { "name", frapy_name, METH_NOARGS, | |
645 | "name () -> String.\n\ | |
646 | Return the function name of the frame, or None if it can't be determined." }, | |
647 | { "type", frapy_type, METH_NOARGS, | |
648 | "type () -> Integer.\n\ | |
649 | Return the type of the frame." }, | |
bea883fd SCR |
650 | { "architecture", frapy_arch, METH_NOARGS, |
651 | "architecture () -> gdb.Architecture.\n\ | |
652 | Return the architecture of the frame." }, | |
f8f6f20b TJB |
653 | { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS, |
654 | "unwind_stop_reason () -> Integer.\n\ | |
655 | Return the reason why it's not possible to find frames older than this." }, | |
656 | { "pc", frapy_pc, METH_NOARGS, | |
657 | "pc () -> Long.\n\ | |
658 | Return the frame's resume address." }, | |
f3e9a817 PM |
659 | { "block", frapy_block, METH_NOARGS, |
660 | "block () -> gdb.Block.\n\ | |
661 | Return the frame's code block." }, | |
662 | { "function", frapy_function, METH_NOARGS, | |
663 | "function () -> gdb.Symbol.\n\ | |
664 | Returns the symbol for the function corresponding to this frame." }, | |
f8f6f20b TJB |
665 | { "older", frapy_older, METH_NOARGS, |
666 | "older () -> gdb.Frame.\n\ | |
667 | Return the frame that called this frame." }, | |
668 | { "newer", frapy_newer, METH_NOARGS, | |
669 | "newer () -> gdb.Frame.\n\ | |
670 | Return the frame called by this frame." }, | |
f3e9a817 PM |
671 | { "find_sal", frapy_find_sal, METH_NOARGS, |
672 | "find_sal () -> gdb.Symtab_and_line.\n\ | |
673 | Return the frame's symtab and line." }, | |
f8f6f20b TJB |
674 | { "read_var", frapy_read_var, METH_VARARGS, |
675 | "read_var (variable) -> gdb.Value.\n\ | |
676 | Return the value of the variable in this frame." }, | |
f3e9a817 PM |
677 | { "select", frapy_select, METH_NOARGS, |
678 | "Select this frame as the user's current frame." }, | |
f8f6f20b TJB |
679 | {NULL} /* Sentinel */ |
680 | }; | |
681 | ||
f0823d2c | 682 | PyTypeObject frame_object_type = { |
9a27f2c6 | 683 | PyVarObject_HEAD_INIT (NULL, 0) |
f8f6f20b TJB |
684 | "gdb.Frame", /* tp_name */ |
685 | sizeof (frame_object), /* tp_basicsize */ | |
686 | 0, /* tp_itemsize */ | |
687 | 0, /* tp_dealloc */ | |
688 | 0, /* tp_print */ | |
689 | 0, /* tp_getattr */ | |
690 | 0, /* tp_setattr */ | |
691 | 0, /* tp_compare */ | |
692 | 0, /* tp_repr */ | |
693 | 0, /* tp_as_number */ | |
694 | 0, /* tp_as_sequence */ | |
695 | 0, /* tp_as_mapping */ | |
696 | 0, /* tp_hash */ | |
697 | 0, /* tp_call */ | |
698 | frapy_str, /* tp_str */ | |
699 | 0, /* tp_getattro */ | |
700 | 0, /* tp_setattro */ | |
701 | 0, /* tp_as_buffer */ | |
702 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
703 | "GDB frame object", /* tp_doc */ | |
704 | 0, /* tp_traverse */ | |
705 | 0, /* tp_clear */ | |
706 | frapy_richcompare, /* tp_richcompare */ | |
707 | 0, /* tp_weaklistoffset */ | |
708 | 0, /* tp_iter */ | |
709 | 0, /* tp_iternext */ | |
710 | frame_object_methods, /* tp_methods */ | |
711 | 0, /* tp_members */ | |
712 | 0, /* tp_getset */ | |
713 | 0, /* tp_base */ | |
714 | 0, /* tp_dict */ | |
715 | 0, /* tp_descr_get */ | |
716 | 0, /* tp_descr_set */ | |
717 | 0, /* tp_dictoffset */ | |
718 | 0, /* tp_init */ | |
719 | 0, /* tp_alloc */ | |
f8f6f20b | 720 | }; |