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