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