Upgrade GDB from 7.3 to 7.4.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / python / py-frame.c
1 /* Python interface to stack frames
2
3    Copyright (C) 2008-2012 Free Software Foundation, Inc.
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"
24 #include "exceptions.h"
25 #include "symtab.h"
26 #include "stack.h"
27 #include "value.h"
28 #include "python-internal.h"
29 #include "symfile.h"
30 #include "objfiles.h"
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)                                \
54         error (_("Frame is invalid."));                 \
55     } while (0)
56
57 static PyTypeObject frame_object_type;
58
59 /* Returns the frame_info object corresponding to the given Python Frame
60    object.  If the frame doesn't exist anymore (the frame id doesn't
61    correspond to any frame in the inferior), returns NULL.  */
62
63 struct frame_info *
64 frame_object_to_frame_info (PyObject *obj)
65 {
66   frame_object *frame_obj = (frame_object *) obj;  
67   struct frame_info *frame;
68
69   frame = frame_find_by_id (frame_obj->frame_id);
70   if (frame == NULL)
71     return NULL;
72
73   if (frame_obj->frame_id_is_next)
74     frame = get_prev_frame (frame);
75
76   return frame;
77 }
78
79 /* Called by the Python interpreter to obtain string representation
80    of the object.  */
81
82 static PyObject *
83 frapy_str (PyObject *self)
84 {
85   char *s;
86   PyObject *result;
87   struct ui_file *strfile;
88
89   strfile = mem_fileopen ();
90   fprint_frame_id (strfile, ((frame_object *) self)->frame_id);
91   s = ui_file_xstrdup (strfile, NULL);
92   result = PyString_FromString (s);
93   xfree (s);
94
95   return result;
96 }
97
98 /* Implementation of gdb.Frame.is_valid (self) -> Boolean.
99    Returns True if the frame corresponding to the frame_id of this
100    object still exists in the inferior.  */
101
102 static PyObject *
103 frapy_is_valid (PyObject *self, PyObject *args)
104 {
105   struct frame_info *frame = NULL;
106   volatile struct gdb_exception except;
107
108   TRY_CATCH (except, RETURN_MASK_ALL)
109     {
110       frame = frame_object_to_frame_info (self);
111     }
112   GDB_PY_HANDLE_EXCEPTION (except);
113
114   if (frame == NULL)
115     Py_RETURN_FALSE;
116
117   Py_RETURN_TRUE;
118 }
119
120 /* Implementation of gdb.Frame.name (self) -> String.
121    Returns the name of the function corresponding to this frame.  */
122
123 static PyObject *
124 frapy_name (PyObject *self, PyObject *args)
125 {
126   struct frame_info *frame;
127   char *name;
128   enum language lang;
129   PyObject *result;
130   volatile struct gdb_exception except;
131
132   TRY_CATCH (except, RETURN_MASK_ALL)
133     {
134       FRAPY_REQUIRE_VALID (self, frame);
135
136       find_frame_funname (frame, &name, &lang, NULL);
137     }
138   GDB_PY_HANDLE_EXCEPTION (except);
139
140   if (name)
141     result = PyUnicode_Decode (name, strlen (name), host_charset (), NULL);
142   else
143     {
144       result = Py_None;
145       Py_INCREF (Py_None);
146     }
147
148   return result;
149 }
150
151 /* Implementation of gdb.Frame.type (self) -> Integer.
152    Returns the frame type, namely one of the gdb.*_FRAME constants.  */
153
154 static PyObject *
155 frapy_type (PyObject *self, PyObject *args)
156 {
157   struct frame_info *frame;
158   enum frame_type type = NORMAL_FRAME;/* Initialize to appease gcc warning.  */
159   volatile struct gdb_exception except;
160
161   TRY_CATCH (except, RETURN_MASK_ALL)
162     {
163       FRAPY_REQUIRE_VALID (self, frame);
164
165       type = get_frame_type (frame);
166     }
167   GDB_PY_HANDLE_EXCEPTION (except);
168
169   return PyInt_FromLong (type);
170 }
171
172 /* Implementation of gdb.Frame.unwind_stop_reason (self) -> Integer.
173    Returns one of the gdb.FRAME_UNWIND_* constants.  */
174
175 static PyObject *
176 frapy_unwind_stop_reason (PyObject *self, PyObject *args)
177 {
178   struct frame_info *frame = NULL;    /* Initialize to appease gcc warning.  */
179   volatile struct gdb_exception except;
180   enum unwind_stop_reason stop_reason;
181
182   TRY_CATCH (except, RETURN_MASK_ALL)
183     {
184       FRAPY_REQUIRE_VALID (self, frame);
185     }
186   GDB_PY_HANDLE_EXCEPTION (except);
187
188   stop_reason = get_frame_unwind_stop_reason (frame);
189
190   return PyInt_FromLong (stop_reason);
191 }
192
193 /* Implementation of gdb.Frame.pc (self) -> Long.
194    Returns the frame's resume address.  */
195
196 static PyObject *
197 frapy_pc (PyObject *self, PyObject *args)
198 {
199   CORE_ADDR pc = 0;           /* Initialize to appease gcc warning.  */
200   struct frame_info *frame;
201   volatile struct gdb_exception except;
202
203   TRY_CATCH (except, RETURN_MASK_ALL)
204     {
205       FRAPY_REQUIRE_VALID (self, frame);
206
207       pc = get_frame_pc (frame);
208     }
209   GDB_PY_HANDLE_EXCEPTION (except);
210
211   return gdb_py_long_from_ulongest (pc);
212 }
213
214 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
215    Returns the frame's code block.  */
216
217 static PyObject *
218 frapy_block (PyObject *self, PyObject *args)
219 {
220   struct frame_info *frame;
221   struct block *block = NULL, *fn_block;
222   volatile struct gdb_exception except;
223
224   TRY_CATCH (except, RETURN_MASK_ALL)
225     {
226       FRAPY_REQUIRE_VALID (self, frame);
227       block = get_frame_block (frame, NULL);
228     }
229   GDB_PY_HANDLE_EXCEPTION (except);
230
231   for (fn_block = block;
232        fn_block != NULL && BLOCK_FUNCTION (fn_block) == NULL;
233        fn_block = BLOCK_SUPERBLOCK (fn_block))
234     ;
235
236   if (block == NULL || fn_block == NULL || BLOCK_FUNCTION (fn_block) == NULL)
237     {
238       PyErr_SetString (PyExc_RuntimeError,
239                        _("Cannot locate object file for block."));
240       return NULL;
241     }
242
243   if (block)
244     {
245       struct symtab *symt;
246
247       symt = SYMBOL_SYMTAB (BLOCK_FUNCTION (fn_block));
248       return block_to_block_object (block, symt->objfile);
249     }
250
251   Py_RETURN_NONE;
252 }
253
254
255 /* Implementation of gdb.Frame.function (self) -> gdb.Symbol.
256    Returns the symbol for the function corresponding to this frame.  */
257
258 static PyObject *
259 frapy_function (PyObject *self, PyObject *args)
260 {
261   struct symbol *sym = NULL;
262   struct frame_info *frame;
263   volatile struct gdb_exception except;
264
265   TRY_CATCH (except, RETURN_MASK_ALL)
266     {
267       FRAPY_REQUIRE_VALID (self, frame);
268
269       sym = find_pc_function (get_frame_address_in_block (frame));
270     }
271   GDB_PY_HANDLE_EXCEPTION (except);
272
273   if (sym)
274     return symbol_to_symbol_object (sym);
275
276   Py_RETURN_NONE;
277 }
278
279 /* Convert a frame_info struct to a Python Frame object.
280    Sets a Python exception and returns NULL on error.  */
281
282 PyObject *
283 frame_info_to_frame_object (struct frame_info *frame)
284 {
285   frame_object *frame_obj;
286   volatile struct gdb_exception except;
287
288   frame_obj = PyObject_New (frame_object, &frame_object_type);
289   if (frame_obj == NULL)
290     {
291       PyErr_SetString (PyExc_MemoryError, 
292                        _("Could not allocate frame object."));
293       return NULL;
294     }
295
296   TRY_CATCH (except, RETURN_MASK_ALL)
297     {
298
299       /* Try to get the previous frame, to determine if this is the last frame
300          in a corrupt stack.  If so, we need to store the frame_id of the next
301          frame and not of this one (which is possibly invalid).  */
302       if (get_prev_frame (frame) == NULL
303           && get_frame_unwind_stop_reason (frame) != UNWIND_NO_REASON
304           && get_next_frame (frame) != NULL)
305         {
306           frame_obj->frame_id = get_frame_id (get_next_frame (frame));
307           frame_obj->frame_id_is_next = 1;
308         }
309       else
310         {
311           frame_obj->frame_id = get_frame_id (frame);
312           frame_obj->frame_id_is_next = 0;
313         }
314       frame_obj->gdbarch = get_frame_arch (frame);
315     }
316   GDB_PY_HANDLE_EXCEPTION (except);
317
318   return (PyObject *) frame_obj;
319 }
320
321 /* Implementation of gdb.Frame.older (self) -> gdb.Frame.
322    Returns the frame immediately older (outer) to this frame, or None if
323    there isn't one.  */
324
325 static PyObject *
326 frapy_older (PyObject *self, PyObject *args)
327 {
328   struct frame_info *frame, *prev;
329   volatile struct gdb_exception except;
330   PyObject *prev_obj = NULL;   /* Initialize to appease gcc warning.  */
331
332   TRY_CATCH (except, RETURN_MASK_ALL)
333     {
334       FRAPY_REQUIRE_VALID (self, frame);
335
336       prev = get_prev_frame (frame);
337       if (prev)
338         prev_obj = (PyObject *) frame_info_to_frame_object (prev);
339       else
340         {
341           Py_INCREF (Py_None);
342           prev_obj = Py_None;
343         }
344     }
345   GDB_PY_HANDLE_EXCEPTION (except);
346
347   return prev_obj;
348 }
349
350 /* Implementation of gdb.Frame.newer (self) -> gdb.Frame.
351    Returns the frame immediately newer (inner) to this frame, or None if
352    there isn't one.  */
353
354 static PyObject *
355 frapy_newer (PyObject *self, PyObject *args)
356 {
357   struct frame_info *frame, *next;
358   volatile struct gdb_exception except;
359   PyObject *next_obj = NULL;   /* Initialize to appease gcc warning.  */
360
361   TRY_CATCH (except, RETURN_MASK_ALL)
362     {
363       FRAPY_REQUIRE_VALID (self, frame);
364
365       next = get_next_frame (frame);
366       if (next)
367         next_obj = (PyObject *) frame_info_to_frame_object (next);
368       else
369         {
370           Py_INCREF (Py_None);
371           next_obj = Py_None;
372         }
373     }
374   GDB_PY_HANDLE_EXCEPTION (except);
375
376   return next_obj;
377 }
378
379 /* Implementation of gdb.Frame.find_sal (self) -> gdb.Symtab_and_line.
380    Returns the frame's symtab and line.  */
381
382 static PyObject *
383 frapy_find_sal (PyObject *self, PyObject *args)
384 {
385   struct frame_info *frame;
386   struct symtab_and_line sal;
387   volatile struct gdb_exception except;
388   PyObject *sal_obj = NULL;   /* Initialize to appease gcc warning.  */
389
390   TRY_CATCH (except, RETURN_MASK_ALL)
391     {
392       FRAPY_REQUIRE_VALID (self, frame);
393
394       find_frame_sal (frame, &sal);
395       sal_obj = symtab_and_line_to_sal_object (sal);
396     }
397   GDB_PY_HANDLE_EXCEPTION (except);
398
399   return sal_obj;
400 }
401
402 /* Implementation of gdb.Frame.read_var_value (self, variable,
403    [block]) -> gdb.Value.  If the optional block argument is provided
404    start the search from that block, otherwise search from the frame's
405    current block (determined by examining the resume address of the
406    frame).  The variable argument must be a string or an instance of a
407    gdb.Symbol.  The block argument must be an instance of gdb.Block.  Returns
408    NULL on error, with a python exception set.  */
409 static PyObject *
410 frapy_read_var (PyObject *self, PyObject *args)
411 {
412   struct frame_info *frame;
413   PyObject *sym_obj, *block_obj = NULL;
414   struct symbol *var = NULL;    /* gcc-4.3.2 false warning.  */
415   struct value *val = NULL;
416   volatile struct gdb_exception except;
417
418   if (!PyArg_ParseTuple (args, "O|O", &sym_obj, &block_obj))
419     return NULL;
420
421   if (PyObject_TypeCheck (sym_obj, &symbol_object_type))
422     var = symbol_object_to_symbol (sym_obj);
423   else if (gdbpy_is_string (sym_obj))
424     {
425       char *var_name;
426       const struct block *block = NULL;
427       struct cleanup *cleanup;
428       volatile struct gdb_exception except;
429
430       var_name = python_string_to_target_string (sym_obj);
431       if (!var_name)
432         return NULL;
433       cleanup = make_cleanup (xfree, var_name);
434
435       if (block_obj)
436         {
437           block = block_object_to_block (block_obj);
438           if (!block)
439             {
440               PyErr_SetString (PyExc_RuntimeError,
441                                _("Second argument must be block."));
442               return NULL;
443             }
444         }
445
446       TRY_CATCH (except, RETURN_MASK_ALL)
447         {
448           FRAPY_REQUIRE_VALID (self, frame);
449
450           if (!block)
451             block = get_frame_block (frame, NULL);
452           var = lookup_symbol (var_name, block, VAR_DOMAIN, NULL);
453         }
454       GDB_PY_HANDLE_EXCEPTION (except);
455
456       if (!var)
457         {
458           PyErr_Format (PyExc_ValueError,
459                         _("Variable '%s' not found."), var_name);
460           do_cleanups (cleanup);
461
462           return NULL;
463         }
464
465       do_cleanups (cleanup);
466     }
467   else
468     {
469       PyErr_SetString (PyExc_TypeError,
470                        _("Argument must be a symbol or string."));
471       return NULL;
472     }
473
474   TRY_CATCH (except, RETURN_MASK_ALL)
475     {
476       FRAPY_REQUIRE_VALID (self, frame);
477
478       val = read_var_value (var, frame);
479     }
480   GDB_PY_HANDLE_EXCEPTION (except);
481
482   return value_to_value_object (val);
483 }
484
485 /* Select this frame.  */
486
487 static PyObject *
488 frapy_select (PyObject *self, PyObject *args)
489 {
490   struct frame_info *fi;
491   volatile struct gdb_exception except;
492
493   TRY_CATCH (except, RETURN_MASK_ALL)
494     {
495       FRAPY_REQUIRE_VALID (self, fi);
496
497       select_frame (fi);
498     }
499   GDB_PY_HANDLE_EXCEPTION (except);
500
501   Py_RETURN_NONE;
502 }
503
504 /* Implementation of gdb.newest_frame () -> gdb.Frame.
505    Returns the newest frame object.  */
506
507 PyObject *
508 gdbpy_newest_frame (PyObject *self, PyObject *args)
509 {
510   struct frame_info *frame;
511   PyObject *frame_obj = NULL;   /* Initialize to appease gcc warning.  */
512   volatile struct gdb_exception except;
513
514   TRY_CATCH (except, RETURN_MASK_ALL)
515     {
516       frame = get_current_frame ();
517       frame_obj = frame_info_to_frame_object (frame);
518     }
519   GDB_PY_HANDLE_EXCEPTION (except);
520
521   return frame_obj;
522 }
523
524 /* Implementation of gdb.selected_frame () -> gdb.Frame.
525    Returns the selected frame object.  */
526
527 PyObject *
528 gdbpy_selected_frame (PyObject *self, PyObject *args)
529 {
530   struct frame_info *frame;
531   PyObject *frame_obj = NULL;   /* Initialize to appease gcc warning.  */
532   volatile struct gdb_exception except;
533
534   TRY_CATCH (except, RETURN_MASK_ALL)
535     {
536       frame = get_selected_frame ("No frame is currently selected.");
537       frame_obj = frame_info_to_frame_object (frame);
538     }
539   GDB_PY_HANDLE_EXCEPTION (except);
540
541   return frame_obj;
542 }
543
544 /* Implementation of gdb.stop_reason_string (Integer) -> String.
545    Return a string explaining the unwind stop reason.  */
546
547 PyObject *
548 gdbpy_frame_stop_reason_string (PyObject *self, PyObject *args)
549 {
550   int reason;
551   const char *str;
552
553   if (!PyArg_ParseTuple (args, "i", &reason))
554     return NULL;
555
556   if (reason < UNWIND_FIRST || reason > UNWIND_LAST)
557     {
558       PyErr_SetString (PyExc_ValueError, 
559                        _("Invalid frame stop reason."));
560       return NULL;
561     }
562
563   str = frame_stop_reason_string (reason);
564   return PyUnicode_Decode (str, strlen (str), host_charset (), NULL);
565 }
566
567 /* Implements the equality comparison for Frame objects.
568    All other comparison operators will throw a TypeError Python exception,
569    as they aren't valid for frames.  */
570
571 static PyObject *
572 frapy_richcompare (PyObject *self, PyObject *other, int op)
573 {
574   int result;
575
576   if (!PyObject_TypeCheck (other, &frame_object_type)
577       || (op != Py_EQ && op != Py_NE))
578     {
579       Py_INCREF (Py_NotImplemented);
580       return Py_NotImplemented;
581     }
582
583   if (frame_id_eq (((frame_object *) self)->frame_id,
584                    ((frame_object *) other)->frame_id))
585     result = Py_EQ;
586   else
587     result = Py_NE;
588
589   if (op == result)
590     Py_RETURN_TRUE;
591   Py_RETURN_FALSE;
592 }
593
594 /* Sets up the Frame API in the gdb module.  */
595
596 void
597 gdbpy_initialize_frames (void)
598 {
599   frame_object_type.tp_new = PyType_GenericNew;
600   if (PyType_Ready (&frame_object_type) < 0)
601     return;
602
603   /* Note: These would probably be best exposed as class attributes of
604      Frame, but I don't know how to do it except by messing with the
605      type's dictionary.  That seems too messy.  */
606   PyModule_AddIntConstant (gdb_module, "NORMAL_FRAME", NORMAL_FRAME);
607   PyModule_AddIntConstant (gdb_module, "DUMMY_FRAME", DUMMY_FRAME);
608   PyModule_AddIntConstant (gdb_module, "INLINE_FRAME", INLINE_FRAME);
609   PyModule_AddIntConstant (gdb_module, "TAILCALL_FRAME", TAILCALL_FRAME);
610   PyModule_AddIntConstant (gdb_module, "SIGTRAMP_FRAME", SIGTRAMP_FRAME);
611   PyModule_AddIntConstant (gdb_module, "ARCH_FRAME", ARCH_FRAME);
612   PyModule_AddIntConstant (gdb_module, "SENTINEL_FRAME", SENTINEL_FRAME);
613
614 #define SET(name, description) \
615   PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name);
616 #define FIRST_ERROR(name) \
617   PyModule_AddIntConstant (gdb_module, "FRAME_"#name, name);
618 #include "unwind_stop_reasons.def"
619 #undef SET
620
621   Py_INCREF (&frame_object_type);
622   PyModule_AddObject (gdb_module, "Frame", (PyObject *) &frame_object_type);
623 }
624
625 \f
626
627 static PyMethodDef frame_object_methods[] = {
628   { "is_valid", frapy_is_valid, METH_NOARGS,
629     "is_valid () -> Boolean.\n\
630 Return true if this frame is valid, false if not." },
631   { "name", frapy_name, METH_NOARGS,
632     "name () -> String.\n\
633 Return the function name of the frame, or None if it can't be determined." },
634   { "type", frapy_type, METH_NOARGS,
635     "type () -> Integer.\n\
636 Return the type of the frame." },
637   { "unwind_stop_reason", frapy_unwind_stop_reason, METH_NOARGS,
638     "unwind_stop_reason () -> Integer.\n\
639 Return the reason why it's not possible to find frames older than this." },
640   { "pc", frapy_pc, METH_NOARGS,
641     "pc () -> Long.\n\
642 Return the frame's resume address." },
643   { "block", frapy_block, METH_NOARGS,
644     "block () -> gdb.Block.\n\
645 Return the frame's code block." },
646   { "function", frapy_function, METH_NOARGS,
647     "function () -> gdb.Symbol.\n\
648 Returns the symbol for the function corresponding to this frame." },
649   { "older", frapy_older, METH_NOARGS,
650     "older () -> gdb.Frame.\n\
651 Return the frame that called this frame." },
652   { "newer", frapy_newer, METH_NOARGS,
653     "newer () -> gdb.Frame.\n\
654 Return the frame called by this frame." },
655   { "find_sal", frapy_find_sal, METH_NOARGS,
656     "find_sal () -> gdb.Symtab_and_line.\n\
657 Return the frame's symtab and line." },
658   { "read_var", frapy_read_var, METH_VARARGS,
659     "read_var (variable) -> gdb.Value.\n\
660 Return the value of the variable in this frame." },
661   { "select", frapy_select, METH_NOARGS,
662     "Select this frame as the user's current frame." },
663   {NULL}  /* Sentinel */
664 };
665
666 static PyTypeObject frame_object_type = {
667   PyObject_HEAD_INIT (NULL)
668   0,                              /* ob_size */
669   "gdb.Frame",                    /* tp_name */
670   sizeof (frame_object),          /* tp_basicsize */
671   0,                              /* tp_itemsize */
672   0,                              /* tp_dealloc */
673   0,                              /* tp_print */
674   0,                              /* tp_getattr */
675   0,                              /* tp_setattr */
676   0,                              /* tp_compare */
677   0,                              /* tp_repr */
678   0,                              /* tp_as_number */
679   0,                              /* tp_as_sequence */
680   0,                              /* tp_as_mapping */
681   0,                              /* tp_hash  */
682   0,                              /* tp_call */
683   frapy_str,                      /* tp_str */
684   0,                              /* tp_getattro */
685   0,                              /* tp_setattro */
686   0,                              /* tp_as_buffer */
687   Py_TPFLAGS_DEFAULT,             /* tp_flags */
688   "GDB frame object",             /* tp_doc */
689   0,                              /* tp_traverse */
690   0,                              /* tp_clear */
691   frapy_richcompare,              /* tp_richcompare */
692   0,                              /* tp_weaklistoffset */
693   0,                              /* tp_iter */
694   0,                              /* tp_iternext */
695   frame_object_methods,           /* tp_methods */
696   0,                              /* tp_members */
697   0,                              /* tp_getset */
698   0,                              /* tp_base */
699   0,                              /* tp_dict */
700   0,                              /* tp_descr_get */
701   0,                              /* tp_descr_set */
702   0,                              /* tp_dictoffset */
703   0,                              /* tp_init */
704   0,                              /* tp_alloc */
705 };