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