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