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