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