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" |
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; | |
55b87a52 | 121 | char *name = NULL; |
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 | |
e9e07ba6 | 129 | find_frame_funname (frame, &name, &lang, NULL); |
f8f6f20b | 130 | } |
492d29ea PA |
131 | CATCH (except, RETURN_MASK_ALL) |
132 | { | |
133 | xfree (name); | |
134 | GDB_PY_HANDLE_EXCEPTION (except); | |
135 | } | |
136 | END_CATCH | |
f8f6f20b TJB |
137 | |
138 | if (name) | |
55b87a52 KS |
139 | { |
140 | result = PyUnicode_Decode (name, strlen (name), host_charset (), NULL); | |
141 | xfree (name); | |
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 | char *funname; |
337 | enum language funlang; | |
338 | ||
cc72b2a2 | 339 | FRAPY_REQUIRE_VALID (self, frame); |
f3e9a817 | 340 | |
282a0691 TT |
341 | find_frame_funname (frame, &funname, &funlang, &sym); |
342 | xfree (funname); | |
f3e9a817 | 343 | } |
492d29ea PA |
344 | CATCH (except, RETURN_MASK_ALL) |
345 | { | |
346 | GDB_PY_HANDLE_EXCEPTION (except); | |
347 | } | |
348 | END_CATCH | |
f3e9a817 PM |
349 | |
350 | if (sym) | |
351 | return symbol_to_symbol_object (sym); | |
352 | ||
353 | Py_RETURN_NONE; | |
354 | } | |
355 | ||
f8f6f20b TJB |
356 | /* Convert a frame_info struct to a Python Frame object. |
357 | Sets a Python exception and returns NULL on error. */ | |
358 | ||
595939de | 359 | PyObject * |
f8f6f20b TJB |
360 | frame_info_to_frame_object (struct frame_info *frame) |
361 | { | |
362 | frame_object *frame_obj; | |
363 | ||
364 | frame_obj = PyObject_New (frame_object, &frame_object_type); | |
365 | if (frame_obj == NULL) | |
6cbc7c3d | 366 | return NULL; |
f8f6f20b | 367 | |
492d29ea | 368 | TRY |
f8f6f20b | 369 | { |
f8f6f20b | 370 | |
76dce0be PM |
371 | /* Try to get the previous frame, to determine if this is the last frame |
372 | in a corrupt stack. If so, we need to store the frame_id of the next | |
373 | frame and not of this one (which is possibly invalid). */ | |
374 | if (get_prev_frame (frame) == NULL | |
375 | && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON | |
376 | && get_next_frame (frame) != NULL) | |
377 | { | |
378 | frame_obj->frame_id = get_frame_id (get_next_frame (frame)); | |
379 | frame_obj->frame_id_is_next = 1; | |
380 | } | |
381 | else | |
382 | { | |
383 | frame_obj->frame_id = get_frame_id (frame); | |
384 | frame_obj->frame_id_is_next = 0; | |
385 | } | |
386 | frame_obj->gdbarch = get_frame_arch (frame); | |
387 | } | |
492d29ea | 388 | CATCH (except, RETURN_MASK_ALL) |
1efd7661 PM |
389 | { |
390 | Py_DECREF (frame_obj); | |
391 | gdbpy_convert_exception (except); | |
392 | return NULL; | |
393 | } | |
492d29ea PA |
394 | END_CATCH |
395 | ||
595939de | 396 | return (PyObject *) frame_obj; |
f8f6f20b TJB |
397 | } |
398 | ||
399 | /* Implementation of gdb.Frame.older (self) -> gdb.Frame. | |
400 | Returns the frame immediately older (outer) to this frame, or None if | |
401 | there isn't one. */ | |
402 | ||
403 | static PyObject * | |
404 | frapy_older (PyObject *self, PyObject *args) | |
405 | { | |
dcf87832 | 406 | struct frame_info *frame, *prev = NULL; |
f8f6f20b TJB |
407 | PyObject *prev_obj = NULL; /* Initialize to appease gcc warning. */ |
408 | ||
492d29ea | 409 | TRY |
f8f6f20b | 410 | { |
cc72b2a2 | 411 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
412 | |
413 | prev = get_prev_frame (frame); | |
f8f6f20b | 414 | } |
492d29ea PA |
415 | CATCH (except, RETURN_MASK_ALL) |
416 | { | |
417 | GDB_PY_HANDLE_EXCEPTION (except); | |
418 | } | |
419 | END_CATCH | |
f8f6f20b | 420 | |
dcf87832 TT |
421 | if (prev) |
422 | prev_obj = (PyObject *) frame_info_to_frame_object (prev); | |
423 | else | |
424 | { | |
425 | Py_INCREF (Py_None); | |
426 | prev_obj = Py_None; | |
427 | } | |
428 | ||
f8f6f20b TJB |
429 | return prev_obj; |
430 | } | |
431 | ||
432 | /* Implementation of gdb.Frame.newer (self) -> gdb.Frame. | |
433 | Returns the frame immediately newer (inner) to this frame, or None if | |
434 | there isn't one. */ | |
435 | ||
436 | static PyObject * | |
437 | frapy_newer (PyObject *self, PyObject *args) | |
438 | { | |
dcf87832 | 439 | struct frame_info *frame, *next = NULL; |
f8f6f20b TJB |
440 | PyObject *next_obj = NULL; /* Initialize to appease gcc warning. */ |
441 | ||
492d29ea | 442 | TRY |
f8f6f20b | 443 | { |
cc72b2a2 | 444 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b TJB |
445 | |
446 | next = get_next_frame (frame); | |
f8f6f20b | 447 | } |
492d29ea PA |
448 | CATCH (except, RETURN_MASK_ALL) |
449 | { | |
450 | GDB_PY_HANDLE_EXCEPTION (except); | |
451 | } | |
452 | END_CATCH | |
f8f6f20b | 453 | |
dcf87832 TT |
454 | if (next) |
455 | next_obj = (PyObject *) frame_info_to_frame_object (next); | |
456 | else | |
457 | { | |
458 | Py_INCREF (Py_None); | |
459 | next_obj = Py_None; | |
460 | } | |
461 | ||
f8f6f20b TJB |
462 | return next_obj; |
463 | } | |
464 | ||
f3e9a817 PM |
465 | /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line. |
466 | Returns the frame's symtab and line. */ | |
467 | ||
468 | static PyObject * | |
469 | frapy_find_sal (PyObject *self, PyObject *args) | |
470 | { | |
471 | struct frame_info *frame; | |
472 | struct symtab_and_line sal; | |
f3e9a817 PM |
473 | PyObject *sal_obj = NULL; /* Initialize to appease gcc warning. */ |
474 | ||
492d29ea | 475 | TRY |
f3e9a817 | 476 | { |
cc72b2a2 | 477 | FRAPY_REQUIRE_VALID (self, frame); |
f3e9a817 PM |
478 | |
479 | find_frame_sal (frame, &sal); | |
480 | sal_obj = symtab_and_line_to_sal_object (sal); | |
481 | } | |
492d29ea PA |
482 | CATCH (except, RETURN_MASK_ALL) |
483 | { | |
484 | GDB_PY_HANDLE_EXCEPTION (except); | |
485 | } | |
486 | END_CATCH | |
f3e9a817 PM |
487 | |
488 | return sal_obj; | |
489 | } | |
490 | ||
dc00d89f PM |
491 | /* Implementation of gdb.Frame.read_var_value (self, variable, |
492 | [block]) -> gdb.Value. If the optional block argument is provided | |
493 | start the search from that block, otherwise search from the frame's | |
494 | current block (determined by examining the resume address of the | |
495 | frame). The variable argument must be a string or an instance of a | |
8dc78533 JK |
496 | gdb.Symbol. The block argument must be an instance of gdb.Block. Returns |
497 | NULL on error, with a python exception set. */ | |
f8f6f20b TJB |
498 | static PyObject * |
499 | frapy_read_var (PyObject *self, PyObject *args) | |
500 | { | |
501 | struct frame_info *frame; | |
dc00d89f | 502 | PyObject *sym_obj, *block_obj = NULL; |
f8f6f20b | 503 | struct symbol *var = NULL; /* gcc-4.3.2 false warning. */ |
63e43d3a | 504 | const struct block *block = NULL; |
f8f6f20b | 505 | struct value *val = NULL; |
f8f6f20b | 506 | |
dc00d89f | 507 | if (!PyArg_ParseTuple (args, "O|O", &sym_obj, &block_obj)) |
f8f6f20b TJB |
508 | return NULL; |
509 | ||
f3e9a817 PM |
510 | if (PyObject_TypeCheck (sym_obj, &symbol_object_type)) |
511 | var = symbol_object_to_symbol (sym_obj); | |
512 | else if (gdbpy_is_string (sym_obj)) | |
f8f6f20b | 513 | { |
9b972014 TT |
514 | gdb::unique_xmalloc_ptr<char> |
515 | var_name (python_string_to_target_string (sym_obj)); | |
f8f6f20b | 516 | |
f8f6f20b TJB |
517 | if (!var_name) |
518 | return NULL; | |
f8f6f20b | 519 | |
dc00d89f PM |
520 | if (block_obj) |
521 | { | |
522 | block = block_object_to_block (block_obj); | |
523 | if (!block) | |
524 | { | |
525 | PyErr_SetString (PyExc_RuntimeError, | |
526 | _("Second argument must be block.")); | |
527 | return NULL; | |
528 | } | |
529 | } | |
530 | ||
492d29ea | 531 | TRY |
f8f6f20b | 532 | { |
63e43d3a | 533 | struct block_symbol lookup_sym; |
cc72b2a2 | 534 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b | 535 | |
dc00d89f | 536 | if (!block) |
626e7282 | 537 | block = get_frame_block (frame, NULL); |
9b972014 | 538 | lookup_sym = lookup_symbol (var_name.get (), block, VAR_DOMAIN, NULL); |
63e43d3a PMR |
539 | var = lookup_sym.symbol; |
540 | block = lookup_sym.block; | |
f8f6f20b | 541 | } |
492d29ea | 542 | CATCH (except, RETURN_MASK_ALL) |
af1c6971 | 543 | { |
f3300387 TT |
544 | gdbpy_convert_exception (except); |
545 | return NULL; | |
af1c6971 | 546 | } |
492d29ea | 547 | END_CATCH |
f8f6f20b TJB |
548 | |
549 | if (!var) | |
550 | { | |
551 | PyErr_Format (PyExc_ValueError, | |
9b972014 | 552 | _("Variable '%s' not found."), var_name.get ()); |
f8f6f20b TJB |
553 | |
554 | return NULL; | |
555 | } | |
f8f6f20b TJB |
556 | } |
557 | else | |
558 | { | |
559 | PyErr_SetString (PyExc_TypeError, | |
044c0f87 | 560 | _("Argument must be a symbol or string.")); |
f8f6f20b TJB |
561 | return NULL; |
562 | } | |
563 | ||
492d29ea | 564 | TRY |
f8f6f20b | 565 | { |
cc72b2a2 | 566 | FRAPY_REQUIRE_VALID (self, frame); |
f8f6f20b | 567 | |
63e43d3a | 568 | val = read_var_value (var, block, frame); |
f8f6f20b | 569 | } |
492d29ea PA |
570 | CATCH (except, RETURN_MASK_ALL) |
571 | { | |
572 | GDB_PY_HANDLE_EXCEPTION (except); | |
573 | } | |
574 | END_CATCH | |
f8f6f20b | 575 | |
dc00d89f | 576 | return value_to_value_object (val); |
f8f6f20b TJB |
577 | } |
578 | ||
f3e9a817 PM |
579 | /* Select this frame. */ |
580 | ||
581 | static PyObject * | |
582 | frapy_select (PyObject *self, PyObject *args) | |
583 | { | |
584 | struct frame_info *fi; | |
f3e9a817 | 585 | |
492d29ea | 586 | TRY |
f3e9a817 | 587 | { |
cc72b2a2 | 588 | FRAPY_REQUIRE_VALID (self, fi); |
f3e9a817 PM |
589 | |
590 | select_frame (fi); | |
591 | } | |
492d29ea PA |
592 | CATCH (except, RETURN_MASK_ALL) |
593 | { | |
594 | GDB_PY_HANDLE_EXCEPTION (except); | |
595 | } | |
596 | END_CATCH | |
f3e9a817 PM |
597 | |
598 | Py_RETURN_NONE; | |
599 | } | |
600 | ||
d8e22779 TT |
601 | /* Implementation of gdb.newest_frame () -> gdb.Frame. |
602 | Returns the newest frame object. */ | |
603 | ||
604 | PyObject * | |
605 | gdbpy_newest_frame (PyObject *self, PyObject *args) | |
606 | { | |
dcf87832 | 607 | struct frame_info *frame = NULL; |
d8e22779 | 608 | |
492d29ea | 609 | TRY |
d8e22779 TT |
610 | { |
611 | frame = get_current_frame (); | |
d8e22779 | 612 | } |
492d29ea PA |
613 | CATCH (except, RETURN_MASK_ALL) |
614 | { | |
615 | GDB_PY_HANDLE_EXCEPTION (except); | |
616 | } | |
617 | END_CATCH | |
d8e22779 | 618 | |
dcf87832 | 619 | return frame_info_to_frame_object (frame); |
d8e22779 TT |
620 | } |
621 | ||
f8f6f20b TJB |
622 | /* Implementation of gdb.selected_frame () -> gdb.Frame. |
623 | Returns the selected frame object. */ | |
624 | ||
625 | PyObject * | |
626 | gdbpy_selected_frame (PyObject *self, PyObject *args) | |
627 | { | |
dcf87832 | 628 | struct frame_info *frame = NULL; |
f8f6f20b | 629 | |
492d29ea | 630 | TRY |
f8f6f20b TJB |
631 | { |
632 | frame = get_selected_frame ("No frame is currently selected."); | |
f8f6f20b | 633 | } |
492d29ea PA |
634 | CATCH (except, RETURN_MASK_ALL) |
635 | { | |
636 | GDB_PY_HANDLE_EXCEPTION (except); | |
637 | } | |
638 | END_CATCH | |
f8f6f20b | 639 | |
dcf87832 | 640 | return frame_info_to_frame_object (frame); |
f8f6f20b TJB |
641 | } |
642 | ||
643 | /* Implementation of gdb.stop_reason_string (Integer) -> String. | |
644 | Return a string explaining the unwind stop reason. */ | |
645 | ||
646 | PyObject * | |
647 | gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args) | |
648 | { | |
649 | int reason; | |
650 | const char *str; | |
651 | ||
652 | if (!PyArg_ParseTuple (args, "i", &reason)) | |
653 | return NULL; | |
654 | ||
2231f1fb | 655 | if (reason < UNWIND_FIRST || reason > UNWIND_LAST) |
f8f6f20b | 656 | { |
256458bc | 657 | PyErr_SetString (PyExc_ValueError, |
044c0f87 | 658 | _("Invalid frame stop reason.")); |
f8f6f20b TJB |
659 | return NULL; |
660 | } | |
661 | ||
aead7601 | 662 | str = unwind_stop_reason_to_string ((enum unwind_stop_reason) reason); |
f8f6f20b TJB |
663 | return PyUnicode_Decode (str, strlen (str), host_charset (), NULL); |
664 | } | |
665 | ||
666 | /* Implements the equality comparison for Frame objects. | |
667 | All other comparison operators will throw a TypeError Python exception, | |
668 | as they aren't valid for frames. */ | |
669 | ||
670 | static PyObject * | |
671 | frapy_richcompare (PyObject *self, PyObject *other, int op) | |
672 | { | |
18e8c3bc TT |
673 | int result; |
674 | ||
675 | if (!PyObject_TypeCheck (other, &frame_object_type) | |
676 | || (op != Py_EQ && op != Py_NE)) | |
f8f6f20b | 677 | { |
18e8c3bc TT |
678 | Py_INCREF (Py_NotImplemented); |
679 | return Py_NotImplemented; | |
f8f6f20b TJB |
680 | } |
681 | ||
682 | if (frame_id_eq (((frame_object *) self)->frame_id, | |
683 | ((frame_object *) other)->frame_id)) | |
18e8c3bc TT |
684 | result = Py_EQ; |
685 | else | |
686 | result = Py_NE; | |
f8f6f20b | 687 | |
18e8c3bc TT |
688 | if (op == result) |
689 | Py_RETURN_TRUE; | |
f8f6f20b TJB |
690 | Py_RETURN_FALSE; |
691 | } | |
692 | ||
693 | /* Sets up the Frame API in the gdb module. */ | |
694 | ||
999633ed | 695 | int |
f8f6f20b TJB |
696 | gdbpy_initialize_frames (void) |
697 | { | |
6a1b1664 | 698 | frame_object_type.tp_new = PyType_GenericNew; |
f8f6f20b | 699 | if (PyType_Ready (&frame_object_type) < 0) |
999633ed | 700 | return -1; |
f8f6f20b | 701 | |
9a2b4c1b MS |
702 | /* Note: These would probably be best exposed as class attributes of |
703 | Frame, but I don't know how to do it except by messing with the | |
704 | type's dictionary. That seems too messy. */ | |
999633ed TT |
705 | if (PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME) < 0 |
706 | || PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME) < 0 | |
707 | || PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME) < 0 | |
708 | || PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME", | |
709 | TAILCALL_FRAME) < 0 | |
710 | || PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME", | |
711 | SIGTRAMP_FRAME) < 0 | |
712 | || PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME) < 0 | |
713 | || PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME", | |
714 | SENTINEL_FRAME) < 0) | |
715 | return -1; | |
2231f1fb KP |
716 | |
717 | #define SET(name, description) \ | |
999633ed TT |
718 | if (PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name) < 0) \ |
719 | return -1; | |
2231f1fb KP |
720 | #include "unwind_stop_reasons.def" |
721 | #undef SET | |
f8f6f20b | 722 | |
aa36459a TT |
723 | return gdb_pymodule_addobject (gdb_module, "Frame", |
724 | (PyObject *) &frame_object_type); | |
f8f6f20b TJB |
725 | } |
726 | ||
727 | \f | |
728 | ||
729 | static PyMethodDef frame_object_methods[] = { | |
730 | { "is_valid", frapy_is_valid, METH_NOARGS, | |
731 | "is_valid () -> Boolean.\n\ | |
732 | Return true if this frame is valid, false if not." }, | |
733 | { "name", frapy_name, METH_NOARGS, | |
734 | "name () -> String.\n\ | |
735 | Return the function name of the frame, or None if it can't be determined." }, | |
736 | { "type", frapy_type, METH_NOARGS, | |
737 | "type () -> Integer.\n\ | |
738 | Return the type of the frame." }, | |
bea883fd SCR |
739 | { "architecture", frapy_arch, METH_NOARGS, |
740 | "architecture () -> gdb.Architecture.\n\ | |
741 | Return the architecture of the frame." }, | |
f8f6f20b TJB |
742 | { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS, |
743 | "unwind_stop_reason () -> Integer.\n\ | |
744 | Return the reason why it's not possible to find frames older than this." }, | |
745 | { "pc", frapy_pc, METH_NOARGS, | |
746 | "pc () -> Long.\n\ | |
747 | Return the frame's resume address." }, | |
5f3b99cf SS |
748 | { "read_register", frapy_read_register, METH_VARARGS, |
749 | "read_register (register_name) -> gdb.Value\n\ | |
750 | Return the value of the register in the frame." }, | |
f3e9a817 PM |
751 | { "block", frapy_block, METH_NOARGS, |
752 | "block () -> gdb.Block.\n\ | |
753 | Return the frame's code block." }, | |
754 | { "function", frapy_function, METH_NOARGS, | |
755 | "function () -> gdb.Symbol.\n\ | |
756 | Returns the symbol for the function corresponding to this frame." }, | |
f8f6f20b TJB |
757 | { "older", frapy_older, METH_NOARGS, |
758 | "older () -> gdb.Frame.\n\ | |
759 | Return the frame that called this frame." }, | |
760 | { "newer", frapy_newer, METH_NOARGS, | |
761 | "newer () -> gdb.Frame.\n\ | |
762 | Return the frame called by this frame." }, | |
f3e9a817 PM |
763 | { "find_sal", frapy_find_sal, METH_NOARGS, |
764 | "find_sal () -> gdb.Symtab_and_line.\n\ | |
765 | Return the frame's symtab and line." }, | |
f8f6f20b TJB |
766 | { "read_var", frapy_read_var, METH_VARARGS, |
767 | "read_var (variable) -> gdb.Value.\n\ | |
768 | Return the value of the variable in this frame." }, | |
f3e9a817 PM |
769 | { "select", frapy_select, METH_NOARGS, |
770 | "Select this frame as the user's current frame." }, | |
f8f6f20b TJB |
771 | {NULL} /* Sentinel */ |
772 | }; | |
773 | ||
f0823d2c | 774 | PyTypeObject frame_object_type = { |
9a27f2c6 | 775 | PyVarObject_HEAD_INIT (NULL, 0) |
f8f6f20b TJB |
776 | "gdb.Frame", /* tp_name */ |
777 | sizeof (frame_object), /* tp_basicsize */ | |
778 | 0, /* tp_itemsize */ | |
779 | 0, /* tp_dealloc */ | |
780 | 0, /* tp_print */ | |
781 | 0, /* tp_getattr */ | |
782 | 0, /* tp_setattr */ | |
783 | 0, /* tp_compare */ | |
784 | 0, /* tp_repr */ | |
785 | 0, /* tp_as_number */ | |
786 | 0, /* tp_as_sequence */ | |
787 | 0, /* tp_as_mapping */ | |
788 | 0, /* tp_hash */ | |
789 | 0, /* tp_call */ | |
790 | frapy_str, /* tp_str */ | |
791 | 0, /* tp_getattro */ | |
792 | 0, /* tp_setattro */ | |
793 | 0, /* tp_as_buffer */ | |
794 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
795 | "GDB frame object", /* tp_doc */ | |
796 | 0, /* tp_traverse */ | |
797 | 0, /* tp_clear */ | |
798 | frapy_richcompare, /* tp_richcompare */ | |
799 | 0, /* tp_weaklistoffset */ | |
800 | 0, /* tp_iter */ | |
801 | 0, /* tp_iternext */ | |
802 | frame_object_methods, /* tp_methods */ | |
803 | 0, /* tp_members */ | |
804 | 0, /* tp_getset */ | |
805 | 0, /* tp_base */ | |
806 | 0, /* tp_dict */ | |
807 | 0, /* tp_descr_get */ | |
808 | 0, /* tp_descr_set */ | |
809 | 0, /* tp_dictoffset */ | |
810 | 0, /* tp_init */ | |
811 | 0, /* tp_alloc */ | |
f8f6f20b | 812 | }; |