Commit | Line | Data |
---|---|---|
f8f6f20b TJB |
1 | /* Python interface to stack frames |
2 | ||
ecd75fc8 | 3 | Copyright (C) 2008-2014 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" | |
f8f6f20b TJB |
24 | #include "symtab.h" |
25 | #include "stack.h" | |
26 | #include "value.h" | |
27 | #include "python-internal.h" | |
f3e9a817 PM |
28 | #include "symfile.h" |
29 | #include "objfiles.h" | |
5f3b99cf | 30 | #include "user-regs.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 | { |
256458bc | 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; | |
55b87a52 | 125 | char *name = NULL; |
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 | 135 | } |
55b87a52 KS |
136 | |
137 | if (except.reason < 0) | |
138 | xfree (name); | |
139 | ||
f8f6f20b TJB |
140 | GDB_PY_HANDLE_EXCEPTION (except); |
141 | ||
142 | if (name) | |
55b87a52 KS |
143 | { |
144 | result = PyUnicode_Decode (name, strlen (name), host_charset (), NULL); | |
145 | xfree (name); | |
146 | } | |
f8f6f20b TJB |
147 | else |
148 | { | |
149 | result = Py_None; | |
150 | Py_INCREF (Py_None); | |
151 | } | |
152 | ||
153 | return result; | |
154 | } | |
155 | ||
156 | /* Implementation of gdb.Frame.type (self) -> Integer. | |
157 | Returns the frame type, namely one of the gdb.*_FRAME constants. */ | |
158 | ||
159 | static PyObject * | |
160 | frapy_type (PyObject *self, PyObject *args) | |
161 | { | |
162 | struct frame_info *frame; | |
163 | enum frame_type type = NORMAL_FRAME;/* Initialize to appease gcc warning. */ | |
164 | volatile struct gdb_exception except; | |
165 | ||
166 | TRY_CATCH (except, RETURN_MASK_ALL) | |
167 | { | |
cc72b2a2 | 168 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
169 | |
170 | type = get_frame_type (frame); | |
171 | } | |
172 | GDB_PY_HANDLE_EXCEPTION (except); | |
173 | ||
174 | return PyInt_FromLong (type); | |
175 | } | |
176 | ||
bea883fd SCR |
177 | /* Implementation of gdb.Frame.architecture (self) -> gdb.Architecture. |
178 | Returns the frame's architecture as a gdb.Architecture object. */ | |
179 | ||
180 | static PyObject * | |
181 | frapy_arch (PyObject *self, PyObject *args) | |
182 | { | |
183 | struct frame_info *frame = NULL; /* Initialize to appease gcc warning. */ | |
184 | frame_object *obj = (frame_object *) self; | |
185 | volatile struct gdb_exception except; | |
186 | ||
187 | TRY_CATCH (except, RETURN_MASK_ALL) | |
188 | { | |
189 | FRAPY_REQUIRE_VALID (self, frame); | |
190 | } | |
191 | GDB_PY_HANDLE_EXCEPTION (except); | |
192 | ||
193 | return gdbarch_to_arch_object (obj->gdbarch); | |
194 | } | |
195 | ||
f8f6f20b TJB |
196 | /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer. |
197 | Returns one of the gdb.FRAME_UNWIND_* constants. */ | |
198 | ||
199 | static PyObject * | |
200 | frapy_unwind_stop_reason (PyObject *self, PyObject *args) | |
201 | { | |
202 | struct frame_info *frame = NULL; /* Initialize to appease gcc warning. */ | |
203 | volatile struct gdb_exception except; | |
204 | enum unwind_stop_reason stop_reason; | |
205 | ||
206 | TRY_CATCH (except, RETURN_MASK_ALL) | |
207 | { | |
cc72b2a2 | 208 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
209 | } |
210 | GDB_PY_HANDLE_EXCEPTION (except); | |
211 | ||
212 | stop_reason = get_frame_unwind_stop_reason (frame); | |
213 | ||
214 | return PyInt_FromLong (stop_reason); | |
215 | } | |
216 | ||
217 | /* Implementation of gdb.Frame.pc (self) -> Long. | |
218 | Returns the frame's resume address. */ | |
219 | ||
220 | static PyObject * | |
221 | frapy_pc (PyObject *self, PyObject *args) | |
222 | { | |
223 | CORE_ADDR pc = 0; /* Initialize to appease gcc warning. */ | |
224 | struct frame_info *frame; | |
225 | volatile struct gdb_exception except; | |
226 | ||
227 | TRY_CATCH (except, RETURN_MASK_ALL) | |
228 | { | |
cc72b2a2 | 229 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
230 | |
231 | pc = get_frame_pc (frame); | |
232 | } | |
233 | GDB_PY_HANDLE_EXCEPTION (except); | |
234 | ||
74aedc46 | 235 | return gdb_py_long_from_ulongest (pc); |
f8f6f20b TJB |
236 | } |
237 | ||
5f3b99cf SS |
238 | /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value. |
239 | Returns the value of a register in this frame. */ | |
240 | ||
241 | static PyObject * | |
242 | frapy_read_register (PyObject *self, PyObject *args) | |
243 | { | |
244 | volatile struct gdb_exception except; | |
245 | const char *regnum_str; | |
246 | struct value *val = NULL; | |
247 | ||
248 | if (!PyArg_ParseTuple (args, "s", ®num_str)) | |
249 | return NULL; | |
250 | ||
251 | TRY_CATCH (except, RETURN_MASK_ALL) | |
252 | { | |
253 | struct frame_info *frame; | |
254 | int regnum; | |
255 | ||
256 | FRAPY_REQUIRE_VALID (self, frame); | |
257 | ||
258 | regnum = user_reg_map_name_to_regnum (get_frame_arch (frame), | |
259 | regnum_str, | |
260 | strlen (regnum_str)); | |
261 | if (regnum >= 0) | |
262 | val = value_of_register (regnum, frame); | |
263 | ||
264 | if (val == NULL) | |
265 | PyErr_SetString (PyExc_ValueError, _("Unknown register.")); | |
266 | } | |
267 | GDB_PY_HANDLE_EXCEPTION (except); | |
268 | ||
269 | return val == NULL ? NULL : value_to_value_object (val); | |
270 | } | |
271 | ||
f3e9a817 PM |
272 | /* Implementation of gdb.Frame.block (self) -> gdb.Block. |
273 | Returns the frame's code block. */ | |
274 | ||
275 | static PyObject * | |
276 | frapy_block (PyObject *self, PyObject *args) | |
277 | { | |
278 | struct frame_info *frame; | |
3977b71f | 279 | const struct block *block = NULL, *fn_block; |
f3e9a817 | 280 | volatile struct gdb_exception except; |
f3e9a817 PM |
281 | |
282 | TRY_CATCH (except, RETURN_MASK_ALL) | |
283 | { | |
cc72b2a2 | 284 | FRAPY_REQUIRE_VALID (self, frame); |
57126e4a | 285 | block = get_frame_block (frame, NULL); |
f3e9a817 PM |
286 | } |
287 | GDB_PY_HANDLE_EXCEPTION (except); | |
288 | ||
57126e4a TT |
289 | for (fn_block = block; |
290 | fn_block != NULL && BLOCK_FUNCTION (fn_block) == NULL; | |
291 | fn_block = BLOCK_SUPERBLOCK (fn_block)) | |
292 | ; | |
293 | ||
294 | if (block == NULL || fn_block == NULL || BLOCK_FUNCTION (fn_block) == NULL) | |
f3e9a817 PM |
295 | { |
296 | PyErr_SetString (PyExc_RuntimeError, | |
1e1d6920 | 297 | _("Cannot locate block for frame.")); |
f3e9a817 PM |
298 | return NULL; |
299 | } | |
300 | ||
301 | if (block) | |
57126e4a TT |
302 | { |
303 | struct symtab *symt; | |
304 | ||
305 | symt = SYMBOL_SYMTAB (BLOCK_FUNCTION (fn_block)); | |
eb822aa6 | 306 | return block_to_block_object (block, SYMTAB_OBJFILE (symt)); |
57126e4a | 307 | } |
f3e9a817 PM |
308 | |
309 | Py_RETURN_NONE; | |
310 | } | |
311 | ||
312 | ||
313 | /* Implementation of gdb.Frame.function (self) -> gdb.Symbol. | |
314 | Returns the symbol for the function corresponding to this frame. */ | |
315 | ||
316 | static PyObject * | |
317 | frapy_function (PyObject *self, PyObject *args) | |
318 | { | |
319 | struct symbol *sym = NULL; | |
320 | struct frame_info *frame; | |
321 | volatile struct gdb_exception except; | |
322 | ||
323 | TRY_CATCH (except, RETURN_MASK_ALL) | |
324 | { | |
cc72b2a2 | 325 | FRAPY_REQUIRE_VALID (self, frame); |
f3e9a817 PM |
326 | |
327 | sym = find_pc_function (get_frame_address_in_block (frame)); | |
328 | } | |
329 | GDB_PY_HANDLE_EXCEPTION (except); | |
330 | ||
331 | if (sym) | |
332 | return symbol_to_symbol_object (sym); | |
333 | ||
334 | Py_RETURN_NONE; | |
335 | } | |
336 | ||
f8f6f20b TJB |
337 | /* Convert a frame_info struct to a Python Frame object. |
338 | Sets a Python exception and returns NULL on error. */ | |
339 | ||
595939de | 340 | PyObject * |
f8f6f20b TJB |
341 | frame_info_to_frame_object (struct frame_info *frame) |
342 | { | |
343 | frame_object *frame_obj; | |
76dce0be | 344 | volatile struct gdb_exception except; |
f8f6f20b TJB |
345 | |
346 | frame_obj = PyObject_New (frame_object, &frame_object_type); | |
347 | if (frame_obj == NULL) | |
6cbc7c3d | 348 | return NULL; |
f8f6f20b | 349 | |
76dce0be | 350 | TRY_CATCH (except, RETURN_MASK_ALL) |
f8f6f20b | 351 | { |
f8f6f20b | 352 | |
76dce0be PM |
353 | /* Try to get the previous frame, to determine if this is the last frame |
354 | in a corrupt stack. If so, we need to store the frame_id of the next | |
355 | frame and not of this one (which is possibly invalid). */ | |
356 | if (get_prev_frame (frame) == NULL | |
357 | && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON | |
358 | && get_next_frame (frame) != NULL) | |
359 | { | |
360 | frame_obj->frame_id = get_frame_id (get_next_frame (frame)); | |
361 | frame_obj->frame_id_is_next = 1; | |
362 | } | |
363 | else | |
364 | { | |
365 | frame_obj->frame_id = get_frame_id (frame); | |
366 | frame_obj->frame_id_is_next = 0; | |
367 | } | |
368 | frame_obj->gdbarch = get_frame_arch (frame); | |
369 | } | |
1efd7661 PM |
370 | if (except.reason < 0) |
371 | { | |
372 | Py_DECREF (frame_obj); | |
373 | gdbpy_convert_exception (except); | |
374 | return NULL; | |
375 | } | |
595939de | 376 | return (PyObject *) frame_obj; |
f8f6f20b TJB |
377 | } |
378 | ||
379 | /* Implementation of gdb.Frame.older (self) -> gdb.Frame. | |
380 | Returns the frame immediately older (outer) to this frame, or None if | |
381 | there isn't one. */ | |
382 | ||
383 | static PyObject * | |
384 | frapy_older (PyObject *self, PyObject *args) | |
385 | { | |
dcf87832 | 386 | struct frame_info *frame, *prev = NULL; |
f8f6f20b TJB |
387 | volatile struct gdb_exception except; |
388 | PyObject *prev_obj = NULL; /* Initialize to appease gcc warning. */ | |
389 | ||
390 | TRY_CATCH (except, RETURN_MASK_ALL) | |
391 | { | |
cc72b2a2 | 392 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
393 | |
394 | prev = get_prev_frame (frame); | |
f8f6f20b TJB |
395 | } |
396 | GDB_PY_HANDLE_EXCEPTION (except); | |
397 | ||
dcf87832 TT |
398 | if (prev) |
399 | prev_obj = (PyObject *) frame_info_to_frame_object (prev); | |
400 | else | |
401 | { | |
402 | Py_INCREF (Py_None); | |
403 | prev_obj = Py_None; | |
404 | } | |
405 | ||
f8f6f20b TJB |
406 | return prev_obj; |
407 | } | |
408 | ||
409 | /* Implementation of gdb.Frame.newer (self) -> gdb.Frame. | |
410 | Returns the frame immediately newer (inner) to this frame, or None if | |
411 | there isn't one. */ | |
412 | ||
413 | static PyObject * | |
414 | frapy_newer (PyObject *self, PyObject *args) | |
415 | { | |
dcf87832 | 416 | struct frame_info *frame, *next = NULL; |
f8f6f20b TJB |
417 | volatile struct gdb_exception except; |
418 | PyObject *next_obj = NULL; /* Initialize to appease gcc warning. */ | |
419 | ||
420 | TRY_CATCH (except, RETURN_MASK_ALL) | |
421 | { | |
cc72b2a2 | 422 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
423 | |
424 | next = get_next_frame (frame); | |
f8f6f20b TJB |
425 | } |
426 | GDB_PY_HANDLE_EXCEPTION (except); | |
427 | ||
dcf87832 TT |
428 | if (next) |
429 | next_obj = (PyObject *) frame_info_to_frame_object (next); | |
430 | else | |
431 | { | |
432 | Py_INCREF (Py_None); | |
433 | next_obj = Py_None; | |
434 | } | |
435 | ||
f8f6f20b TJB |
436 | return next_obj; |
437 | } | |
438 | ||
f3e9a817 PM |
439 | /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line. |
440 | Returns the frame's symtab and line. */ | |
441 | ||
442 | static PyObject * | |
443 | frapy_find_sal (PyObject *self, PyObject *args) | |
444 | { | |
445 | struct frame_info *frame; | |
446 | struct symtab_and_line sal; | |
f3e9a817 PM |
447 | volatile struct gdb_exception except; |
448 | PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */ | |
449 | ||
450 | TRY_CATCH (except, RETURN_MASK_ALL) | |
451 | { | |
cc72b2a2 | 452 | FRAPY_REQUIRE_VALID (self, frame); |
f3e9a817 PM |
453 | |
454 | find_frame_sal (frame, &sal); | |
455 | sal_obj = symtab_and_line_to_sal_object (sal); | |
456 | } | |
457 | GDB_PY_HANDLE_EXCEPTION (except); | |
458 | ||
459 | return sal_obj; | |
460 | } | |
461 | ||
dc00d89f PM |
462 | /* Implementation of gdb.Frame.read_var_value (self, variable, |
463 | [block]) -> gdb.Value. If the optional block argument is provided | |
464 | start the search from that block, otherwise search from the frame's | |
465 | current block (determined by examining the resume address of the | |
466 | frame). The variable argument must be a string or an instance of a | |
8dc78533 JK |
467 | gdb.Symbol. The block argument must be an instance of gdb.Block. Returns |
468 | NULL on error, with a python exception set. */ | |
f8f6f20b TJB |
469 | static PyObject * |
470 | frapy_read_var (PyObject *self, PyObject *args) | |
471 | { | |
472 | struct frame_info *frame; | |
dc00d89f | 473 | PyObject *sym_obj, *block_obj = NULL; |
f8f6f20b TJB |
474 | struct symbol *var = NULL; /* gcc-4.3.2 false warning. */ |
475 | struct value *val = NULL; | |
476 | volatile struct gdb_exception except; | |
477 | ||
dc00d89f | 478 | if (!PyArg_ParseTuple (args, "O|O", &sym_obj, &block_obj)) |
f8f6f20b TJB |
479 | return NULL; |
480 | ||
f3e9a817 PM |
481 | if (PyObject_TypeCheck (sym_obj, &symbol_object_type)) |
482 | var = symbol_object_to_symbol (sym_obj); | |
483 | else if (gdbpy_is_string (sym_obj)) | |
f8f6f20b TJB |
484 | { |
485 | char *var_name; | |
9df2fbc4 | 486 | const struct block *block = NULL; |
f8f6f20b TJB |
487 | struct cleanup *cleanup; |
488 | volatile struct gdb_exception except; | |
489 | ||
490 | var_name = python_string_to_target_string (sym_obj); | |
491 | if (!var_name) | |
492 | return NULL; | |
493 | cleanup = make_cleanup (xfree, var_name); | |
494 | ||
dc00d89f PM |
495 | if (block_obj) |
496 | { | |
497 | block = block_object_to_block (block_obj); | |
498 | if (!block) | |
499 | { | |
500 | PyErr_SetString (PyExc_RuntimeError, | |
501 | _("Second argument must be block.")); | |
af1c6971 | 502 | do_cleanups (cleanup); |
dc00d89f PM |
503 | return NULL; |
504 | } | |
505 | } | |
506 | ||
f8f6f20b TJB |
507 | TRY_CATCH (except, RETURN_MASK_ALL) |
508 | { | |
cc72b2a2 | 509 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b | 510 | |
dc00d89f | 511 | if (!block) |
626e7282 | 512 | block = get_frame_block (frame, NULL); |
f8f6f20b TJB |
513 | var = lookup_symbol (var_name, block, VAR_DOMAIN, NULL); |
514 | } | |
af1c6971 TT |
515 | if (except.reason < 0) |
516 | { | |
517 | do_cleanups (cleanup); | |
f3300387 TT |
518 | gdbpy_convert_exception (except); |
519 | return NULL; | |
af1c6971 | 520 | } |
f8f6f20b TJB |
521 | |
522 | if (!var) | |
523 | { | |
524 | PyErr_Format (PyExc_ValueError, | |
044c0f87 | 525 | _("Variable '%s' not found."), var_name); |
f8f6f20b TJB |
526 | do_cleanups (cleanup); |
527 | ||
528 | return NULL; | |
529 | } | |
530 | ||
531 | do_cleanups (cleanup); | |
532 | } | |
533 | else | |
534 | { | |
535 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 536 | _("Argument must be a symbol or string.")); |
f8f6f20b TJB |
537 | return NULL; |
538 | } | |
539 | ||
540 | TRY_CATCH (except, RETURN_MASK_ALL) | |
541 | { | |
cc72b2a2 | 542 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
543 | |
544 | val = read_var_value (var, frame); | |
545 | } | |
546 | GDB_PY_HANDLE_EXCEPTION (except); | |
547 | ||
dc00d89f | 548 | return value_to_value_object (val); |
f8f6f20b TJB |
549 | } |
550 | ||
f3e9a817 PM |
551 | /* Select this frame. */ |
552 | ||
553 | static PyObject * | |
554 | frapy_select (PyObject *self, PyObject *args) | |
555 | { | |
556 | struct frame_info *fi; | |
f3e9a817 PM |
557 | volatile struct gdb_exception except; |
558 | ||
559 | TRY_CATCH (except, RETURN_MASK_ALL) | |
560 | { | |
cc72b2a2 | 561 | FRAPY_REQUIRE_VALID (self, fi); |
f3e9a817 PM |
562 | |
563 | select_frame (fi); | |
564 | } | |
565 | GDB_PY_HANDLE_EXCEPTION (except); | |
566 | ||
567 | Py_RETURN_NONE; | |
568 | } | |
569 | ||
d8e22779 TT |
570 | /* Implementation of gdb.newest_frame () -> gdb.Frame. |
571 | Returns the newest frame object. */ | |
572 | ||
573 | PyObject * | |
574 | gdbpy_newest_frame (PyObject *self, PyObject *args) | |
575 | { | |
dcf87832 | 576 | struct frame_info *frame = NULL; |
d8e22779 TT |
577 | volatile struct gdb_exception except; |
578 | ||
579 | TRY_CATCH (except, RETURN_MASK_ALL) | |
580 | { | |
581 | frame = get_current_frame (); | |
d8e22779 TT |
582 | } |
583 | GDB_PY_HANDLE_EXCEPTION (except); | |
584 | ||
dcf87832 | 585 | return frame_info_to_frame_object (frame); |
d8e22779 TT |
586 | } |
587 | ||
f8f6f20b TJB |
588 | /* Implementation of gdb.selected_frame () -> gdb.Frame. |
589 | Returns the selected frame object. */ | |
590 | ||
591 | PyObject * | |
592 | gdbpy_selected_frame (PyObject *self, PyObject *args) | |
593 | { | |
dcf87832 | 594 | struct frame_info *frame = NULL; |
f8f6f20b TJB |
595 | volatile struct gdb_exception except; |
596 | ||
597 | TRY_CATCH (except, RETURN_MASK_ALL) | |
598 | { | |
599 | frame = get_selected_frame ("No frame is currently selected."); | |
f8f6f20b TJB |
600 | } |
601 | GDB_PY_HANDLE_EXCEPTION (except); | |
602 | ||
dcf87832 | 603 | return frame_info_to_frame_object (frame); |
f8f6f20b TJB |
604 | } |
605 | ||
606 | /* Implementation of gdb.stop_reason_string (Integer) -> String. | |
607 | Return a string explaining the unwind stop reason. */ | |
608 | ||
609 | PyObject * | |
610 | gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args) | |
611 | { | |
612 | int reason; | |
613 | const char *str; | |
614 | ||
615 | if (!PyArg_ParseTuple (args, "i", &reason)) | |
616 | return NULL; | |
617 | ||
2231f1fb | 618 | if (reason < UNWIND_FIRST || reason > UNWIND_LAST) |
f8f6f20b | 619 | { |
256458bc | 620 | PyErr_SetString (PyExc_ValueError, |
044c0f87 | 621 | _("Invalid frame stop reason.")); |
f8f6f20b TJB |
622 | return NULL; |
623 | } | |
624 | ||
70e38b8e | 625 | str = unwind_stop_reason_to_string (reason); |
f8f6f20b TJB |
626 | return PyUnicode_Decode (str, strlen (str), host_charset (), NULL); |
627 | } | |
628 | ||
629 | /* Implements the equality comparison for Frame objects. | |
630 | All other comparison operators will throw a TypeError Python exception, | |
631 | as they aren't valid for frames. */ | |
632 | ||
633 | static PyObject * | |
634 | frapy_richcompare (PyObject *self, PyObject *other, int op) | |
635 | { | |
18e8c3bc TT |
636 | int result; |
637 | ||
638 | if (!PyObject_TypeCheck (other, &frame_object_type) | |
639 | || (op != Py_EQ && op != Py_NE)) | |
f8f6f20b | 640 | { |
18e8c3bc TT |
641 | Py_INCREF (Py_NotImplemented); |
642 | return Py_NotImplemented; | |
f8f6f20b TJB |
643 | } |
644 | ||
645 | if (frame_id_eq (((frame_object *) self)->frame_id, | |
646 | ((frame_object *) other)->frame_id)) | |
18e8c3bc TT |
647 | result = Py_EQ; |
648 | else | |
649 | result = Py_NE; | |
f8f6f20b | 650 | |
18e8c3bc TT |
651 | if (op == result) |
652 | Py_RETURN_TRUE; | |
f8f6f20b TJB |
653 | Py_RETURN_FALSE; |
654 | } | |
655 | ||
656 | /* Sets up the Frame API in the gdb module. */ | |
657 | ||
999633ed | 658 | int |
f8f6f20b TJB |
659 | gdbpy_initialize_frames (void) |
660 | { | |
6a1b1664 | 661 | frame_object_type.tp_new = PyType_GenericNew; |
f8f6f20b | 662 | if (PyType_Ready (&frame_object_type) < 0) |
999633ed | 663 | return -1; |
f8f6f20b | 664 | |
9a2b4c1b MS |
665 | /* Note: These would probably be best exposed as class attributes of |
666 | Frame, but I don't know how to do it except by messing with the | |
667 | type's dictionary. That seems too messy. */ | |
999633ed TT |
668 | if (PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME) < 0 |
669 | || PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME) < 0 | |
670 | || PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME) < 0 | |
671 | || PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME", | |
672 | TAILCALL_FRAME) < 0 | |
673 | || PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME", | |
674 | SIGTRAMP_FRAME) < 0 | |
675 | || PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME) < 0 | |
676 | || PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME", | |
677 | SENTINEL_FRAME) < 0) | |
678 | return -1; | |
2231f1fb KP |
679 | |
680 | #define SET(name, description) \ | |
999633ed TT |
681 | if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \ |
682 | return -1; | |
2231f1fb KP |
683 | #include "unwind_stop_reasons.def" |
684 | #undef SET | |
f8f6f20b | 685 | |
aa36459a TT |
686 | return gdb_pymodule_addobject (gdb_module, "Frame", |
687 | (PyObject *) &frame_object_type); | |
f8f6f20b TJB |
688 | } |
689 | ||
690 | \f | |
691 | ||
692 | static PyMethodDef frame_object_methods[] = { | |
693 | { "is_valid", frapy_is_valid, METH_NOARGS, | |
694 | "is_valid () -> Boolean.\n\ | |
695 | Return true if this frame is valid, false if not." }, | |
696 | { "name", frapy_name, METH_NOARGS, | |
697 | "name () -> String.\n\ | |
698 | Return the function name of the frame, or None if it can't be determined." }, | |
699 | { "type", frapy_type, METH_NOARGS, | |
700 | "type () -> Integer.\n\ | |
701 | Return the type of the frame." }, | |
bea883fd SCR |
702 | { "architecture", frapy_arch, METH_NOARGS, |
703 | "architecture () -> gdb.Architecture.\n\ | |
704 | Return the architecture of the frame." }, | |
f8f6f20b TJB |
705 | { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS, |
706 | "unwind_stop_reason () -> Integer.\n\ | |
707 | Return the reason why it's not possible to find frames older than this." }, | |
708 | { "pc", frapy_pc, METH_NOARGS, | |
709 | "pc () -> Long.\n\ | |
710 | Return the frame's resume address." }, | |
5f3b99cf SS |
711 | { "read_register", frapy_read_register, METH_VARARGS, |
712 | "read_register (register_name) -> gdb.Value\n\ | |
713 | Return the value of the register in the frame." }, | |
f3e9a817 PM |
714 | { "block", frapy_block, METH_NOARGS, |
715 | "block () -> gdb.Block.\n\ | |
716 | Return the frame's code block." }, | |
717 | { "function", frapy_function, METH_NOARGS, | |
718 | "function () -> gdb.Symbol.\n\ | |
719 | Returns the symbol for the function corresponding to this frame." }, | |
f8f6f20b TJB |
720 | { "older", frapy_older, METH_NOARGS, |
721 | "older () -> gdb.Frame.\n\ | |
722 | Return the frame that called this frame." }, | |
723 | { "newer", frapy_newer, METH_NOARGS, | |
724 | "newer () -> gdb.Frame.\n\ | |
725 | Return the frame called by this frame." }, | |
f3e9a817 PM |
726 | { "find_sal", frapy_find_sal, METH_NOARGS, |
727 | "find_sal () -> gdb.Symtab_and_line.\n\ | |
728 | Return the frame's symtab and line." }, | |
f8f6f20b TJB |
729 | { "read_var", frapy_read_var, METH_VARARGS, |
730 | "read_var (variable) -> gdb.Value.\n\ | |
731 | Return the value of the variable in this frame." }, | |
f3e9a817 PM |
732 | { "select", frapy_select, METH_NOARGS, |
733 | "Select this frame as the user's current frame." }, | |
f8f6f20b TJB |
734 | {NULL} /* Sentinel */ |
735 | }; | |
736 | ||
f0823d2c | 737 | PyTypeObject frame_object_type = { |
9a27f2c6 | 738 | PyVarObject_HEAD_INIT (NULL, 0) |
f8f6f20b TJB |
739 | "gdb.Frame", /* tp_name */ |
740 | sizeof (frame_object), /* tp_basicsize */ | |
741 | 0, /* tp_itemsize */ | |
742 | 0, /* tp_dealloc */ | |
743 | 0, /* tp_print */ | |
744 | 0, /* tp_getattr */ | |
745 | 0, /* tp_setattr */ | |
746 | 0, /* tp_compare */ | |
747 | 0, /* tp_repr */ | |
748 | 0, /* tp_as_number */ | |
749 | 0, /* tp_as_sequence */ | |
750 | 0, /* tp_as_mapping */ | |
751 | 0, /* tp_hash */ | |
752 | 0, /* tp_call */ | |
753 | frapy_str, /* tp_str */ | |
754 | 0, /* tp_getattro */ | |
755 | 0, /* tp_setattro */ | |
756 | 0, /* tp_as_buffer */ | |
757 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
758 | "GDB frame object", /* tp_doc */ | |
759 | 0, /* tp_traverse */ | |
760 | 0, /* tp_clear */ | |
761 | frapy_richcompare, /* tp_richcompare */ | |
762 | 0, /* tp_weaklistoffset */ | |
763 | 0, /* tp_iter */ | |
764 | 0, /* tp_iternext */ | |
765 | frame_object_methods, /* tp_methods */ | |
766 | 0, /* tp_members */ | |
767 | 0, /* tp_getset */ | |
768 | 0, /* tp_base */ | |
769 | 0, /* tp_dict */ | |
770 | 0, /* tp_descr_get */ | |
771 | 0, /* tp_descr_set */ | |
772 | 0, /* tp_dictoffset */ | |
773 | 0, /* tp_init */ | |
774 | 0, /* tp_alloc */ | |
f8f6f20b | 775 | }; |