]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/python/python.c
Enable readline in Python in a GDB-specific way and block the
[thirdparty/binutils-gdb.git] / gdb / python / python.c
1 /* General python/gdb code
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 "arch-utils.h"
22 #include "command.h"
23 #include "ui-out.h"
24 #include "cli/cli-script.h"
25 #include "gdbcmd.h"
26 #include "progspace.h"
27 #include "objfiles.h"
28 #include "value.h"
29 #include "language.h"
30 #include "exceptions.h"
31 #include "event-loop.h"
32 #include "serial.h"
33 #include "readline/tilde.h"
34 #include "python.h"
35
36 #include <ctype.h>
37
38 /* Declared constants and enum for python stack printing. */
39 static const char python_excp_none[] = "none";
40 static const char python_excp_full[] = "full";
41 static const char python_excp_message[] = "message";
42
43 /* "set python print-stack" choices. */
44 static const char *const python_excp_enums[] =
45 {
46 python_excp_none,
47 python_excp_full,
48 python_excp_message,
49 NULL
50 };
51
52 /* The exception printing variable. 'full' if we want to print the
53 error message and stack, 'none' if we want to print nothing, and
54 'message' if we only want to print the error message. 'message' is
55 the default. */
56 static const char *gdbpy_should_print_stack = python_excp_message;
57
58 #ifdef HAVE_PYTHON
59
60 #include "libiberty.h"
61 #include "cli/cli-decode.h"
62 #include "charset.h"
63 #include "top.h"
64 #include "solib.h"
65 #include "python-internal.h"
66 #include "linespec.h"
67 #include "source.h"
68 #include "version.h"
69 #include "target.h"
70 #include "gdbthread.h"
71 #include "observer.h"
72 #include "interps.h"
73
74 static PyMethodDef GdbMethods[];
75
76 PyObject *gdb_module;
77
78 /* Some string constants we may wish to use. */
79 PyObject *gdbpy_to_string_cst;
80 PyObject *gdbpy_children_cst;
81 PyObject *gdbpy_display_hint_cst;
82 PyObject *gdbpy_doc_cst;
83 PyObject *gdbpy_enabled_cst;
84 PyObject *gdbpy_value_cst;
85
86 /* The GdbError exception. */
87 PyObject *gdbpy_gdberror_exc;
88
89 /* The `gdb.error' base class. */
90 PyObject *gdbpy_gdb_error;
91
92 /* The `gdb.MemoryError' exception. */
93 PyObject *gdbpy_gdb_memory_error;
94
95 /* Architecture and language to be used in callbacks from
96 the Python interpreter. */
97 struct gdbarch *python_gdbarch;
98 const struct language_defn *python_language;
99
100 /* Restore global language and architecture and Python GIL state
101 when leaving the Python interpreter. */
102
103 struct python_env
104 {
105 PyGILState_STATE state;
106 struct gdbarch *gdbarch;
107 const struct language_defn *language;
108 PyObject *error_type, *error_value, *error_traceback;
109 };
110
111 static void
112 restore_python_env (void *p)
113 {
114 struct python_env *env = (struct python_env *)p;
115
116 /* Leftover Python error is forbidden by Python Exception Handling. */
117 if (PyErr_Occurred ())
118 {
119 /* This order is similar to the one calling error afterwards. */
120 gdbpy_print_stack ();
121 warning (_("internal error: Unhandled Python exception"));
122 }
123
124 PyErr_Restore (env->error_type, env->error_value, env->error_traceback);
125
126 PyGILState_Release (env->state);
127 python_gdbarch = env->gdbarch;
128 python_language = env->language;
129 xfree (env);
130 }
131
132 /* Called before entering the Python interpreter to install the
133 current language and architecture to be used for Python values. */
134
135 struct cleanup *
136 ensure_python_env (struct gdbarch *gdbarch,
137 const struct language_defn *language)
138 {
139 struct python_env *env = xmalloc (sizeof *env);
140
141 env->state = PyGILState_Ensure ();
142 env->gdbarch = python_gdbarch;
143 env->language = python_language;
144
145 python_gdbarch = gdbarch;
146 python_language = language;
147
148 /* Save it and ensure ! PyErr_Occurred () afterwards. */
149 PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback);
150
151 return make_cleanup (restore_python_env, env);
152 }
153
154 /* Clear the quit flag. */
155
156 void
157 clear_quit_flag (void)
158 {
159 /* This clears the flag as a side effect. */
160 PyOS_InterruptOccurred ();
161 }
162
163 /* Set the quit flag. */
164
165 void
166 set_quit_flag (void)
167 {
168 PyErr_SetInterrupt ();
169 }
170
171 /* Return true if the quit flag has been set, false otherwise. */
172
173 int
174 check_quit_flag (void)
175 {
176 return PyOS_InterruptOccurred ();
177 }
178
179 /* Evaluate a Python command like PyRun_SimpleString, but uses
180 Py_single_input which prints the result of expressions, and does
181 not automatically print the stack on errors. */
182
183 static int
184 eval_python_command (const char *command)
185 {
186 PyObject *m, *d, *v;
187
188 m = PyImport_AddModule ("__main__");
189 if (m == NULL)
190 return -1;
191
192 d = PyModule_GetDict (m);
193 if (d == NULL)
194 return -1;
195 v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
196 if (v == NULL)
197 return -1;
198
199 Py_DECREF (v);
200 if (Py_FlushLine ())
201 PyErr_Clear ();
202
203 return 0;
204 }
205
206 /* Implementation of the gdb "python-interactive" command. */
207
208 static void
209 python_interactive_command (char *arg, int from_tty)
210 {
211 struct cleanup *cleanup;
212 int err;
213
214 cleanup = make_cleanup_restore_integer (&interpreter_async);
215 interpreter_async = 0;
216
217 while (arg && *arg && isspace (*arg))
218 ++arg;
219
220 ensure_python_env (get_current_arch (), current_language);
221
222 if (arg && *arg)
223 {
224 int len = strlen (arg);
225 char *script = xmalloc (len + 2);
226
227 strcpy (script, arg);
228 script[len] = '\n';
229 script[len + 1] = '\0';
230 err = eval_python_command (script);
231 xfree (script);
232 }
233 else
234 {
235 err = PyRun_InteractiveLoop (instream, "<stdin>");
236 dont_repeat ();
237 }
238
239 if (err)
240 {
241 gdbpy_print_stack ();
242 error (_("Error while executing Python code."));
243 }
244
245 do_cleanups (cleanup);
246 }
247
248 /* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
249 named FILENAME.
250
251 On Windows hosts few users would build Python themselves (this is no
252 trivial task on this platform), and thus use binaries built by
253 someone else instead. There may happen situation where the Python
254 library and GDB are using two different versions of the C runtime
255 library. Python, being built with VC, would use one version of the
256 msvcr DLL (Eg. msvcr100.dll), while MinGW uses msvcrt.dll.
257 A FILE * from one runtime does not necessarily operate correctly in
258 the other runtime.
259
260 To work around this potential issue, we create on Windows hosts the
261 FILE object using Python routines, thus making sure that it is
262 compatible with the Python library. */
263
264 static void
265 python_run_simple_file (FILE *file, const char *filename)
266 {
267 #ifndef _WIN32
268
269 PyRun_SimpleFile (file, filename);
270
271 #else /* _WIN32 */
272
273 char *full_path;
274 PyObject *python_file;
275 struct cleanup *cleanup;
276
277 /* Because we have a string for a filename, and are using Python to
278 open the file, we need to expand any tilde in the path first. */
279 full_path = tilde_expand (filename);
280 cleanup = make_cleanup (xfree, full_path);
281 python_file = PyFile_FromString (full_path, "r");
282 if (! python_file)
283 {
284 do_cleanups (cleanup);
285 gdbpy_print_stack ();
286 error (_("Error while opening file: %s"), full_path);
287 }
288
289 make_cleanup_py_decref (python_file);
290 PyRun_SimpleFile (PyFile_AsFile (python_file), filename);
291 do_cleanups (cleanup);
292
293 #endif /* _WIN32 */
294 }
295
296 /* Given a command_line, return a command string suitable for passing
297 to Python. Lines in the string are separated by newlines. The
298 return value is allocated using xmalloc and the caller is
299 responsible for freeing it. */
300
301 static char *
302 compute_python_string (struct command_line *l)
303 {
304 struct command_line *iter;
305 char *script = NULL;
306 int size = 0;
307 int here;
308
309 for (iter = l; iter; iter = iter->next)
310 size += strlen (iter->line) + 1;
311
312 script = xmalloc (size + 1);
313 here = 0;
314 for (iter = l; iter; iter = iter->next)
315 {
316 int len = strlen (iter->line);
317
318 strcpy (&script[here], iter->line);
319 here += len;
320 script[here++] = '\n';
321 }
322 script[here] = '\0';
323 return script;
324 }
325
326 /* Take a command line structure representing a 'python' command, and
327 evaluate its body using the Python interpreter. */
328
329 void
330 eval_python_from_control_command (struct command_line *cmd)
331 {
332 int ret;
333 char *script;
334 struct cleanup *cleanup;
335
336 if (cmd->body_count != 1)
337 error (_("Invalid \"python\" block structure."));
338
339 cleanup = ensure_python_env (get_current_arch (), current_language);
340
341 script = compute_python_string (cmd->body_list[0]);
342 ret = PyRun_SimpleString (script);
343 xfree (script);
344 if (ret)
345 error (_("Error while executing Python code."));
346
347 do_cleanups (cleanup);
348 }
349
350 /* Implementation of the gdb "python" command. */
351
352 static void
353 python_command (char *arg, int from_tty)
354 {
355 struct cleanup *cleanup;
356
357 cleanup = ensure_python_env (get_current_arch (), current_language);
358
359 make_cleanup_restore_integer (&interpreter_async);
360 interpreter_async = 0;
361
362 while (arg && *arg && isspace (*arg))
363 ++arg;
364 if (arg && *arg)
365 {
366 if (PyRun_SimpleString (arg))
367 error (_("Error while executing Python code."));
368 }
369 else
370 {
371 struct command_line *l = get_command_line (python_control, "");
372
373 make_cleanup_free_command_lines (&l);
374 execute_control_command_untraced (l);
375 }
376
377 do_cleanups (cleanup);
378 }
379
380 \f
381
382 /* Transform a gdb parameters's value into a Python value. May return
383 NULL (and set a Python exception) on error. Helper function for
384 get_parameter. */
385 PyObject *
386 gdbpy_parameter_value (enum var_types type, void *var)
387 {
388 switch (type)
389 {
390 case var_string:
391 case var_string_noescape:
392 case var_optional_filename:
393 case var_filename:
394 case var_enum:
395 {
396 char *str = * (char **) var;
397
398 if (! str)
399 str = "";
400 return PyString_Decode (str, strlen (str), host_charset (), NULL);
401 }
402
403 case var_boolean:
404 {
405 if (* (int *) var)
406 Py_RETURN_TRUE;
407 else
408 Py_RETURN_FALSE;
409 }
410
411 case var_auto_boolean:
412 {
413 enum auto_boolean ab = * (enum auto_boolean *) var;
414
415 if (ab == AUTO_BOOLEAN_TRUE)
416 Py_RETURN_TRUE;
417 else if (ab == AUTO_BOOLEAN_FALSE)
418 Py_RETURN_FALSE;
419 else
420 Py_RETURN_NONE;
421 }
422
423 case var_integer:
424 if ((* (int *) var) == INT_MAX)
425 Py_RETURN_NONE;
426 /* Fall through. */
427 case var_zinteger:
428 return PyLong_FromLong (* (int *) var);
429
430 case var_uinteger:
431 {
432 unsigned int val = * (unsigned int *) var;
433
434 if (val == UINT_MAX)
435 Py_RETURN_NONE;
436 return PyLong_FromUnsignedLong (val);
437 }
438 }
439
440 return PyErr_Format (PyExc_RuntimeError,
441 _("Programmer error: unhandled type."));
442 }
443
444 /* A Python function which returns a gdb parameter's value as a Python
445 value. */
446
447 PyObject *
448 gdbpy_parameter (PyObject *self, PyObject *args)
449 {
450 struct cmd_list_element *alias, *prefix, *cmd;
451 const char *arg;
452 char *newarg;
453 int found = -1;
454 volatile struct gdb_exception except;
455
456 if (! PyArg_ParseTuple (args, "s", &arg))
457 return NULL;
458
459 newarg = concat ("show ", arg, (char *) NULL);
460
461 TRY_CATCH (except, RETURN_MASK_ALL)
462 {
463 found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd);
464 }
465 xfree (newarg);
466 GDB_PY_HANDLE_EXCEPTION (except);
467 if (!found)
468 return PyErr_Format (PyExc_RuntimeError,
469 _("Could not find parameter `%s'."), arg);
470
471 if (! cmd->var)
472 return PyErr_Format (PyExc_RuntimeError,
473 _("`%s' is not a parameter."), arg);
474 return gdbpy_parameter_value (cmd->var_type, cmd->var);
475 }
476
477 /* Wrapper for target_charset. */
478
479 static PyObject *
480 gdbpy_target_charset (PyObject *self, PyObject *args)
481 {
482 const char *cset = target_charset (python_gdbarch);
483
484 return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
485 }
486
487 /* Wrapper for target_wide_charset. */
488
489 static PyObject *
490 gdbpy_target_wide_charset (PyObject *self, PyObject *args)
491 {
492 const char *cset = target_wide_charset (python_gdbarch);
493
494 return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
495 }
496
497 /* A Python function which evaluates a string using the gdb CLI. */
498
499 static PyObject *
500 execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
501 {
502 const char *arg;
503 PyObject *from_tty_obj = NULL, *to_string_obj = NULL;
504 int from_tty, to_string;
505 volatile struct gdb_exception except;
506 static char *keywords[] = {"command", "from_tty", "to_string", NULL };
507 char *result = NULL;
508
509 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
510 &PyBool_Type, &from_tty_obj,
511 &PyBool_Type, &to_string_obj))
512 return NULL;
513
514 from_tty = 0;
515 if (from_tty_obj)
516 {
517 int cmp = PyObject_IsTrue (from_tty_obj);
518 if (cmp < 0)
519 return NULL;
520 from_tty = cmp;
521 }
522
523 to_string = 0;
524 if (to_string_obj)
525 {
526 int cmp = PyObject_IsTrue (to_string_obj);
527 if (cmp < 0)
528 return NULL;
529 to_string = cmp;
530 }
531
532 TRY_CATCH (except, RETURN_MASK_ALL)
533 {
534 /* Copy the argument text in case the command modifies it. */
535 char *copy = xstrdup (arg);
536 struct cleanup *cleanup = make_cleanup (xfree, copy);
537
538 make_cleanup_restore_integer (&interpreter_async);
539 interpreter_async = 0;
540
541 prevent_dont_repeat ();
542 if (to_string)
543 result = execute_command_to_string (copy, from_tty);
544 else
545 {
546 result = NULL;
547 execute_command (copy, from_tty);
548 }
549
550 do_cleanups (cleanup);
551 }
552 GDB_PY_HANDLE_EXCEPTION (except);
553
554 /* Do any commands attached to breakpoint we stopped at. */
555 bpstat_do_actions ();
556
557 if (result)
558 {
559 PyObject *r = PyString_FromString (result);
560 xfree (result);
561 return r;
562 }
563 Py_RETURN_NONE;
564 }
565
566 /* Implementation of gdb.solib_name (Long) -> String.
567 Returns the name of the shared library holding a given address, or None. */
568
569 static PyObject *
570 gdbpy_solib_name (PyObject *self, PyObject *args)
571 {
572 char *soname;
573 PyObject *str_obj;
574 gdb_py_longest pc;
575
576 if (!PyArg_ParseTuple (args, GDB_PY_LL_ARG, &pc))
577 return NULL;
578
579 soname = solib_name_from_address (current_program_space, pc);
580 if (soname)
581 str_obj = PyString_Decode (soname, strlen (soname), host_charset (), NULL);
582 else
583 {
584 str_obj = Py_None;
585 Py_INCREF (Py_None);
586 }
587
588 return str_obj;
589 }
590
591 /* A Python function which is a wrapper for decode_line_1. */
592
593 static PyObject *
594 gdbpy_decode_line (PyObject *self, PyObject *args)
595 {
596 struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to
597 appease gcc. */
598 struct symtab_and_line sal;
599 const char *arg = NULL;
600 char *copy_to_free = NULL, *copy = NULL;
601 struct cleanup *cleanups;
602 PyObject *result = NULL;
603 PyObject *return_result = NULL;
604 PyObject *unparsed = NULL;
605 volatile struct gdb_exception except;
606
607 if (! PyArg_ParseTuple (args, "|s", &arg))
608 return NULL;
609
610 cleanups = make_cleanup (null_cleanup, NULL);
611
612 sals.sals = NULL;
613 TRY_CATCH (except, RETURN_MASK_ALL)
614 {
615 if (arg)
616 {
617 copy = xstrdup (arg);
618 copy_to_free = copy;
619 sals = decode_line_1 (&copy, 0, 0, 0);
620 }
621 else
622 {
623 set_default_source_symtab_and_line ();
624 sal = get_current_source_symtab_and_line ();
625 sals.sals = &sal;
626 sals.nelts = 1;
627 }
628 }
629
630 if (sals.sals != NULL && sals.sals != &sal)
631 {
632 make_cleanup (xfree, copy_to_free);
633 make_cleanup (xfree, sals.sals);
634 }
635
636 if (except.reason < 0)
637 {
638 do_cleanups (cleanups);
639 /* We know this will always throw. */
640 GDB_PY_HANDLE_EXCEPTION (except);
641 }
642
643 if (sals.nelts)
644 {
645 int i;
646
647 result = PyTuple_New (sals.nelts);
648 if (! result)
649 goto error;
650 for (i = 0; i < sals.nelts; ++i)
651 {
652 PyObject *obj;
653
654 obj = symtab_and_line_to_sal_object (sals.sals[i]);
655 if (! obj)
656 {
657 Py_DECREF (result);
658 goto error;
659 }
660
661 PyTuple_SetItem (result, i, obj);
662 }
663 }
664 else
665 {
666 result = Py_None;
667 Py_INCREF (Py_None);
668 }
669
670 return_result = PyTuple_New (2);
671 if (! return_result)
672 {
673 Py_DECREF (result);
674 goto error;
675 }
676
677 if (copy && strlen (copy) > 0)
678 {
679 unparsed = PyString_FromString (copy);
680 if (unparsed == NULL)
681 {
682 Py_DECREF (result);
683 Py_DECREF (return_result);
684 return_result = NULL;
685 goto error;
686 }
687 }
688 else
689 {
690 unparsed = Py_None;
691 Py_INCREF (Py_None);
692 }
693
694 PyTuple_SetItem (return_result, 0, unparsed);
695 PyTuple_SetItem (return_result, 1, result);
696
697 error:
698 do_cleanups (cleanups);
699
700 return return_result;
701 }
702
703 /* Parse a string and evaluate it as an expression. */
704 static PyObject *
705 gdbpy_parse_and_eval (PyObject *self, PyObject *args)
706 {
707 const char *expr_str;
708 struct value *result = NULL;
709 volatile struct gdb_exception except;
710
711 if (!PyArg_ParseTuple (args, "s", &expr_str))
712 return NULL;
713
714 TRY_CATCH (except, RETURN_MASK_ALL)
715 {
716 char *copy = xstrdup (expr_str);
717 struct cleanup *cleanup = make_cleanup (xfree, copy);
718
719 result = parse_and_eval (copy);
720 do_cleanups (cleanup);
721 }
722 GDB_PY_HANDLE_EXCEPTION (except);
723
724 return value_to_value_object (result);
725 }
726
727 /* Implementation of gdb.find_pc_line function.
728 Returns the gdb.Symtab_and_line object corresponding to a PC value. */
729
730 static PyObject *
731 gdbpy_find_pc_line (PyObject *self, PyObject *args)
732 {
733 struct symtab_and_line sal;
734 CORE_ADDR pc;
735 gdb_py_ulongest pc_llu;
736
737 if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu))
738 return NULL;
739
740 pc = (CORE_ADDR) pc_llu;
741 sal = find_pc_line (pc, 0);
742 return symtab_and_line_to_sal_object (sal);
743 }
744
745 /* Read a file as Python code.
746 FILE is the file to run. FILENAME is name of the file FILE.
747 This does not throw any errors. If an exception occurs python will print
748 the traceback and clear the error indicator. */
749
750 void
751 source_python_script (FILE *file, const char *filename)
752 {
753 struct cleanup *cleanup;
754
755 cleanup = ensure_python_env (get_current_arch (), current_language);
756 python_run_simple_file (file, filename);
757 do_cleanups (cleanup);
758 }
759
760 \f
761
762 /* Posting and handling events. */
763
764 /* A single event. */
765 struct gdbpy_event
766 {
767 /* The Python event. This is just a callable object. */
768 PyObject *event;
769 /* The next event. */
770 struct gdbpy_event *next;
771 };
772
773 /* All pending events. */
774 static struct gdbpy_event *gdbpy_event_list;
775 /* The final link of the event list. */
776 static struct gdbpy_event **gdbpy_event_list_end;
777
778 /* We use a file handler, and not an async handler, so that we can
779 wake up the main thread even when it is blocked in poll(). */
780 static struct serial *gdbpy_event_fds[2];
781
782 /* The file handler callback. This reads from the internal pipe, and
783 then processes the Python event queue. This will always be run in
784 the main gdb thread. */
785
786 static void
787 gdbpy_run_events (struct serial *scb, void *context)
788 {
789 struct cleanup *cleanup;
790
791 cleanup = ensure_python_env (get_current_arch (), current_language);
792
793 /* Flush the fd. Do this before flushing the events list, so that
794 any new event post afterwards is sure to re-awake the event
795 loop. */
796 while (serial_readchar (gdbpy_event_fds[0], 0) >= 0)
797 ;
798
799 while (gdbpy_event_list)
800 {
801 /* Dispatching the event might push a new element onto the event
802 loop, so we update here "atomically enough". */
803 struct gdbpy_event *item = gdbpy_event_list;
804 gdbpy_event_list = gdbpy_event_list->next;
805 if (gdbpy_event_list == NULL)
806 gdbpy_event_list_end = &gdbpy_event_list;
807
808 /* Ignore errors. */
809 if (PyObject_CallObject (item->event, NULL) == NULL)
810 PyErr_Clear ();
811
812 Py_DECREF (item->event);
813 xfree (item);
814 }
815
816 do_cleanups (cleanup);
817 }
818
819 /* Submit an event to the gdb thread. */
820 static PyObject *
821 gdbpy_post_event (PyObject *self, PyObject *args)
822 {
823 struct gdbpy_event *event;
824 PyObject *func;
825 int wakeup;
826
827 if (!PyArg_ParseTuple (args, "O", &func))
828 return NULL;
829
830 if (!PyCallable_Check (func))
831 {
832 PyErr_SetString (PyExc_RuntimeError,
833 _("Posted event is not callable"));
834 return NULL;
835 }
836
837 Py_INCREF (func);
838
839 /* From here until the end of the function, we have the GIL, so we
840 can operate on our global data structures without worrying. */
841 wakeup = gdbpy_event_list == NULL;
842
843 event = XNEW (struct gdbpy_event);
844 event->event = func;
845 event->next = NULL;
846 *gdbpy_event_list_end = event;
847 gdbpy_event_list_end = &event->next;
848
849 /* Wake up gdb when needed. */
850 if (wakeup)
851 {
852 char c = 'q'; /* Anything. */
853
854 if (serial_write (gdbpy_event_fds[1], &c, 1))
855 return PyErr_SetFromErrno (PyExc_IOError);
856 }
857
858 Py_RETURN_NONE;
859 }
860
861 /* Initialize the Python event handler. */
862 static void
863 gdbpy_initialize_events (void)
864 {
865 if (serial_pipe (gdbpy_event_fds) == 0)
866 {
867 gdbpy_event_list_end = &gdbpy_event_list;
868 serial_async (gdbpy_event_fds[0], gdbpy_run_events, NULL);
869 }
870 }
871
872 \f
873
874 static void
875 before_prompt_hook (const char *current_gdb_prompt)
876 {
877 struct cleanup *cleanup;
878 char *prompt = NULL;
879
880 cleanup = ensure_python_env (get_current_arch (), current_language);
881
882 if (PyObject_HasAttrString (gdb_module, "prompt_hook"))
883 {
884 PyObject *hook;
885
886 hook = PyObject_GetAttrString (gdb_module, "prompt_hook");
887 if (hook == NULL)
888 goto fail;
889
890 if (PyCallable_Check (hook))
891 {
892 PyObject *result;
893 PyObject *current_prompt;
894
895 current_prompt = PyString_FromString (current_gdb_prompt);
896 if (current_prompt == NULL)
897 goto fail;
898
899 result = PyObject_CallFunctionObjArgs (hook, current_prompt, NULL);
900
901 Py_DECREF (current_prompt);
902
903 if (result == NULL)
904 goto fail;
905
906 make_cleanup_py_decref (result);
907
908 /* Return type should be None, or a String. If it is None,
909 fall through, we will not set a prompt. If it is a
910 string, set PROMPT. Anything else, set an exception. */
911 if (result != Py_None && ! PyString_Check (result))
912 {
913 PyErr_Format (PyExc_RuntimeError,
914 _("Return from prompt_hook must " \
915 "be either a Python string, or None"));
916 goto fail;
917 }
918
919 if (result != Py_None)
920 {
921 prompt = python_string_to_host_string (result);
922
923 if (prompt == NULL)
924 goto fail;
925 else
926 make_cleanup (xfree, prompt);
927 }
928 }
929 }
930
931 /* If a prompt has been set, PROMPT will not be NULL. If it is
932 NULL, do not set the prompt. */
933 if (prompt != NULL)
934 set_prompt (prompt);
935
936 do_cleanups (cleanup);
937 return;
938
939 fail:
940 gdbpy_print_stack ();
941 do_cleanups (cleanup);
942 return;
943 }
944
945 \f
946
947 /* Printing. */
948
949 /* A python function to write a single string using gdb's filtered
950 output stream . The optional keyword STREAM can be used to write
951 to a particular stream. The default stream is to gdb_stdout. */
952
953 static PyObject *
954 gdbpy_write (PyObject *self, PyObject *args, PyObject *kw)
955 {
956 const char *arg;
957 static char *keywords[] = {"text", "stream", NULL };
958 int stream_type = 0;
959 volatile struct gdb_exception except;
960
961 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &arg,
962 &stream_type))
963 return NULL;
964
965 TRY_CATCH (except, RETURN_MASK_ALL)
966 {
967 switch (stream_type)
968 {
969 case 1:
970 {
971 fprintf_filtered (gdb_stderr, "%s", arg);
972 break;
973 }
974 case 2:
975 {
976 fprintf_filtered (gdb_stdlog, "%s", arg);
977 break;
978 }
979 default:
980 fprintf_filtered (gdb_stdout, "%s", arg);
981 }
982 }
983 GDB_PY_HANDLE_EXCEPTION (except);
984
985 Py_RETURN_NONE;
986 }
987
988 /* A python function to flush a gdb stream. The optional keyword
989 STREAM can be used to flush a particular stream. The default stream
990 is gdb_stdout. */
991
992 static PyObject *
993 gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw)
994 {
995 static char *keywords[] = {"stream", NULL };
996 int stream_type = 0;
997
998 if (! PyArg_ParseTupleAndKeywords (args, kw, "|i", keywords,
999 &stream_type))
1000 return NULL;
1001
1002 switch (stream_type)
1003 {
1004 case 1:
1005 {
1006 gdb_flush (gdb_stderr);
1007 break;
1008 }
1009 case 2:
1010 {
1011 gdb_flush (gdb_stdlog);
1012 break;
1013 }
1014 default:
1015 gdb_flush (gdb_stdout);
1016 }
1017
1018 Py_RETURN_NONE;
1019 }
1020
1021 /* Print a python exception trace, print just a message, or print
1022 nothing and clear the python exception, depending on
1023 gdbpy_should_print_stack. Only call this if a python exception is
1024 set. */
1025 void
1026 gdbpy_print_stack (void)
1027 {
1028 /* Print "none", just clear exception. */
1029 if (gdbpy_should_print_stack == python_excp_none)
1030 {
1031 PyErr_Clear ();
1032 }
1033 /* Print "full" message and backtrace. */
1034 else if (gdbpy_should_print_stack == python_excp_full)
1035 {
1036 PyErr_Print ();
1037 /* PyErr_Print doesn't necessarily end output with a newline.
1038 This works because Python's stdout/stderr is fed through
1039 printf_filtered. */
1040 begin_line ();
1041 }
1042 /* Print "message", just error print message. */
1043 else
1044 {
1045 PyObject *ptype, *pvalue, *ptraceback;
1046 char *msg = NULL, *type = NULL;
1047
1048 PyErr_Fetch (&ptype, &pvalue, &ptraceback);
1049
1050 /* Fetch the error message contained within ptype, pvalue. */
1051 msg = gdbpy_exception_to_string (ptype, pvalue);
1052 type = gdbpy_obj_to_string (ptype);
1053 if (msg == NULL)
1054 {
1055 /* An error occurred computing the string representation of the
1056 error message. */
1057 fprintf_filtered (gdb_stderr,
1058 _("Error occurred computing Python error" \
1059 "message.\n"));
1060 }
1061 else
1062 fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
1063 type, msg);
1064
1065 Py_XDECREF (ptype);
1066 Py_XDECREF (pvalue);
1067 Py_XDECREF (ptraceback);
1068 xfree (msg);
1069 }
1070 }
1071
1072 \f
1073
1074 /* Return the current Progspace.
1075 There always is one. */
1076
1077 static PyObject *
1078 gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2)
1079 {
1080 PyObject *result;
1081
1082 result = pspace_to_pspace_object (current_program_space);
1083 if (result)
1084 Py_INCREF (result);
1085 return result;
1086 }
1087
1088 /* Return a sequence holding all the Progspaces. */
1089
1090 static PyObject *
1091 gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
1092 {
1093 struct program_space *ps;
1094 PyObject *list;
1095
1096 list = PyList_New (0);
1097 if (!list)
1098 return NULL;
1099
1100 ALL_PSPACES (ps)
1101 {
1102 PyObject *item = pspace_to_pspace_object (ps);
1103
1104 if (!item || PyList_Append (list, item) == -1)
1105 {
1106 Py_DECREF (list);
1107 return NULL;
1108 }
1109 }
1110
1111 return list;
1112 }
1113
1114 \f
1115
1116 /* The "current" objfile. This is set when gdb detects that a new
1117 objfile has been loaded. It is only set for the duration of a call to
1118 source_python_script_for_objfile; it is NULL at other times. */
1119 static struct objfile *gdbpy_current_objfile;
1120
1121 /* Set the current objfile to OBJFILE and then read FILE named FILENAME
1122 as Python code. This does not throw any errors. If an exception
1123 occurs python will print the traceback and clear the error indicator. */
1124
1125 void
1126 source_python_script_for_objfile (struct objfile *objfile, FILE *file,
1127 const char *filename)
1128 {
1129 struct cleanup *cleanups;
1130
1131 cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
1132 gdbpy_current_objfile = objfile;
1133
1134 python_run_simple_file (file, filename);
1135
1136 do_cleanups (cleanups);
1137 gdbpy_current_objfile = NULL;
1138 }
1139
1140 /* Return the current Objfile, or None if there isn't one. */
1141
1142 static PyObject *
1143 gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
1144 {
1145 PyObject *result;
1146
1147 if (! gdbpy_current_objfile)
1148 Py_RETURN_NONE;
1149
1150 result = objfile_to_objfile_object (gdbpy_current_objfile);
1151 if (result)
1152 Py_INCREF (result);
1153 return result;
1154 }
1155
1156 /* Return a sequence holding all the Objfiles. */
1157
1158 static PyObject *
1159 gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
1160 {
1161 struct objfile *objf;
1162 PyObject *list;
1163
1164 list = PyList_New (0);
1165 if (!list)
1166 return NULL;
1167
1168 ALL_OBJFILES (objf)
1169 {
1170 PyObject *item = objfile_to_objfile_object (objf);
1171
1172 if (!item || PyList_Append (list, item) == -1)
1173 {
1174 Py_DECREF (list);
1175 return NULL;
1176 }
1177 }
1178
1179 return list;
1180 }
1181
1182 #else /* HAVE_PYTHON */
1183
1184 /* Dummy implementation of the gdb "python-interactive" and "python"
1185 command. */
1186
1187 static void
1188 python_interactive_command (char *arg, int from_tty)
1189 {
1190 while (arg && *arg && isspace (*arg))
1191 ++arg;
1192 if (arg && *arg)
1193 error (_("Python scripting is not supported in this copy of GDB."));
1194 else
1195 {
1196 struct command_line *l = get_command_line (python_control, "");
1197 struct cleanup *cleanups = make_cleanup_free_command_lines (&l);
1198
1199 execute_control_command_untraced (l);
1200 do_cleanups (cleanups);
1201 }
1202 }
1203
1204 static void
1205 python_command (char *arg, int from_tty)
1206 {
1207 python_interactive_command (arg, from_tty);
1208 }
1209
1210 void
1211 eval_python_from_control_command (struct command_line *cmd)
1212 {
1213 error (_("Python scripting is not supported in this copy of GDB."));
1214 }
1215
1216 void
1217 source_python_script (FILE *file, const char *filename)
1218 {
1219 throw_error (UNSUPPORTED_ERROR,
1220 _("Python scripting is not supported in this copy of GDB."));
1221 }
1222
1223 int
1224 gdbpy_should_stop (struct breakpoint_object *bp_obj)
1225 {
1226 internal_error (__FILE__, __LINE__,
1227 _("gdbpy_should_stop called when Python scripting is " \
1228 "not supported."));
1229 }
1230
1231 int
1232 gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj)
1233 {
1234 internal_error (__FILE__, __LINE__,
1235 _("gdbpy_breakpoint_has_py_cond called when Python " \
1236 "scripting is not supported."));
1237 }
1238
1239 #endif /* HAVE_PYTHON */
1240
1241 \f
1242
1243 /* Lists for 'set python' commands. */
1244
1245 static struct cmd_list_element *user_set_python_list;
1246 static struct cmd_list_element *user_show_python_list;
1247
1248 /* Function for use by 'set python' prefix command. */
1249
1250 static void
1251 user_set_python (char *args, int from_tty)
1252 {
1253 help_list (user_set_python_list, "set python ", all_commands,
1254 gdb_stdout);
1255 }
1256
1257 /* Function for use by 'show python' prefix command. */
1258
1259 static void
1260 user_show_python (char *args, int from_tty)
1261 {
1262 cmd_show_list (user_show_python_list, from_tty, "");
1263 }
1264
1265 /* Initialize the Python code. */
1266
1267 /* Provide a prototype to silence -Wmissing-prototypes. */
1268 extern initialize_file_ftype _initialize_python;
1269
1270 void
1271 _initialize_python (void)
1272 {
1273 char *cmd_name;
1274 struct cmd_list_element *cmd;
1275
1276 add_com ("python-interactive", class_obscure,
1277 python_interactive_command,
1278 #ifdef HAVE_PYTHON
1279 _("\
1280 Start a Python interactive prompt.\n\
1281 \n\
1282 Alternatively, a single-line Python command can be given as an\n\
1283 argument, and if the command is an expression, the result will be\n\
1284 printed. For example:\n\
1285 \n\
1286 (gdb) python-interactive 2 + 3\n\
1287 5\n\
1288 ")
1289 #else /* HAVE_PYTHON */
1290 _("\
1291 Start a Python interactive prompt.\n\
1292 \n\
1293 Python scripting is not supported in this copy of GDB.\n\
1294 This command is only a placeholder.")
1295 #endif /* HAVE_PYTHON */
1296 );
1297 add_com_alias ("pi", "python-interactive", class_obscure, 1);
1298
1299 add_com ("python", class_obscure, python_command,
1300 #ifdef HAVE_PYTHON
1301 _("\
1302 Evaluate a Python command.\n\
1303 \n\
1304 The command can be given as an argument, for instance:\n\
1305 \n\
1306 python print 23\n\
1307 \n\
1308 If no argument is given, the following lines are read and used\n\
1309 as the Python commands. Type a line containing \"end\" to indicate\n\
1310 the end of the command.")
1311 #else /* HAVE_PYTHON */
1312 _("\
1313 Evaluate a Python command.\n\
1314 \n\
1315 Python scripting is not supported in this copy of GDB.\n\
1316 This command is only a placeholder.")
1317 #endif /* HAVE_PYTHON */
1318 );
1319 add_com_alias ("py", "python", class_obscure, 1);
1320
1321 /* Add set/show python print-stack. */
1322 add_prefix_cmd ("python", no_class, user_show_python,
1323 _("Prefix command for python preference settings."),
1324 &user_show_python_list, "show python ", 0,
1325 &showlist);
1326
1327 add_prefix_cmd ("python", no_class, user_set_python,
1328 _("Prefix command for python preference settings."),
1329 &user_set_python_list, "set python ", 0,
1330 &setlist);
1331
1332 add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums,
1333 &gdbpy_should_print_stack, _("\
1334 Set mode for Python stack dump on error."), _("\
1335 Show the mode of Python stack printing on error."), _("\
1336 none == no stack or message will be printed.\n\
1337 full == a message and a stack will be printed.\n\
1338 message == an error message without a stack will be printed."),
1339 NULL, NULL,
1340 &user_set_python_list,
1341 &user_show_python_list);
1342
1343 #ifdef HAVE_PYTHON
1344 #ifdef WITH_PYTHON_PATH
1345 /* Work around problem where python gets confused about where it is,
1346 and then can't find its libraries, etc.
1347 NOTE: Python assumes the following layout:
1348 /foo/bin/python
1349 /foo/lib/pythonX.Y/...
1350 This must be done before calling Py_Initialize. */
1351 Py_SetProgramName (concat (ldirname (python_libdir), SLASH_STRING, "bin",
1352 SLASH_STRING, "python", NULL));
1353 #endif
1354
1355 Py_Initialize ();
1356 PyEval_InitThreads ();
1357
1358 gdb_module = Py_InitModule ("gdb", GdbMethods);
1359
1360 /* The casts to (char*) are for python 2.4. */
1361 PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version);
1362 PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name);
1363 PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG",
1364 (char*) target_name);
1365
1366 /* Add stream constants. */
1367 PyModule_AddIntConstant (gdb_module, "STDOUT", 0);
1368 PyModule_AddIntConstant (gdb_module, "STDERR", 1);
1369 PyModule_AddIntConstant (gdb_module, "STDLOG", 2);
1370
1371 /* gdb.parameter ("data-directory") doesn't necessarily exist when the python
1372 script below is run (depending on order of _initialize_* functions).
1373 Define the initial value of gdb.PYTHONDIR here. */
1374 {
1375 char *gdb_pythondir;
1376
1377 gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", NULL);
1378 PyModule_AddStringConstant (gdb_module, "PYTHONDIR", gdb_pythondir);
1379 xfree (gdb_pythondir);
1380 }
1381
1382 gdbpy_gdb_error = PyErr_NewException ("gdb.error", PyExc_RuntimeError, NULL);
1383 PyModule_AddObject (gdb_module, "error", gdbpy_gdb_error);
1384
1385 gdbpy_gdb_memory_error = PyErr_NewException ("gdb.MemoryError",
1386 gdbpy_gdb_error, NULL);
1387 PyModule_AddObject (gdb_module, "MemoryError", gdbpy_gdb_memory_error);
1388
1389 gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
1390 PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc);
1391
1392 gdbpy_initialize_gdb_readline ();
1393 gdbpy_initialize_auto_load ();
1394 gdbpy_initialize_values ();
1395 gdbpy_initialize_frames ();
1396 gdbpy_initialize_commands ();
1397 gdbpy_initialize_symbols ();
1398 gdbpy_initialize_symtabs ();
1399 gdbpy_initialize_blocks ();
1400 gdbpy_initialize_functions ();
1401 gdbpy_initialize_parameters ();
1402 gdbpy_initialize_types ();
1403 gdbpy_initialize_pspace ();
1404 gdbpy_initialize_objfile ();
1405 gdbpy_initialize_breakpoints ();
1406 gdbpy_initialize_finishbreakpoints ();
1407 gdbpy_initialize_lazy_string ();
1408 gdbpy_initialize_thread ();
1409 gdbpy_initialize_inferior ();
1410 gdbpy_initialize_events ();
1411
1412 gdbpy_initialize_eventregistry ();
1413 gdbpy_initialize_py_events ();
1414 gdbpy_initialize_event ();
1415 gdbpy_initialize_stop_event ();
1416 gdbpy_initialize_signal_event ();
1417 gdbpy_initialize_breakpoint_event ();
1418 gdbpy_initialize_continue_event ();
1419 gdbpy_initialize_exited_event ();
1420 gdbpy_initialize_thread_event ();
1421 gdbpy_initialize_new_objfile_event () ;
1422
1423 observer_attach_before_prompt (before_prompt_hook);
1424
1425 PyRun_SimpleString ("import gdb");
1426 PyRun_SimpleString ("gdb.pretty_printers = []");
1427
1428 gdbpy_to_string_cst = PyString_FromString ("to_string");
1429 gdbpy_children_cst = PyString_FromString ("children");
1430 gdbpy_display_hint_cst = PyString_FromString ("display_hint");
1431 gdbpy_doc_cst = PyString_FromString ("__doc__");
1432 gdbpy_enabled_cst = PyString_FromString ("enabled");
1433 gdbpy_value_cst = PyString_FromString ("value");
1434
1435 /* Release the GIL while gdb runs. */
1436 PyThreadState_Swap (NULL);
1437 PyEval_ReleaseLock ();
1438
1439 #endif /* HAVE_PYTHON */
1440 }
1441
1442 #ifdef HAVE_PYTHON
1443
1444 /* Perform the remaining python initializations.
1445 These must be done after GDB is at least mostly initialized.
1446 E.g., The "info pretty-printer" command needs the "info" prefix
1447 command installed. */
1448
1449 void
1450 finish_python_initialization (void)
1451 {
1452 struct cleanup *cleanup;
1453
1454 cleanup = ensure_python_env (get_current_arch (), current_language);
1455
1456 PyRun_SimpleString ("\
1457 import os\n\
1458 import sys\n\
1459 \n\
1460 class GdbOutputFile:\n\
1461 def close(self):\n\
1462 # Do nothing.\n\
1463 return None\n\
1464 \n\
1465 def isatty(self):\n\
1466 return False\n\
1467 \n\
1468 def write(self, s):\n\
1469 gdb.write(s, stream=gdb.STDOUT)\n \
1470 \n\
1471 def writelines(self, iterable):\n\
1472 for line in iterable:\n\
1473 self.write(line)\n\
1474 \n\
1475 def flush(self):\n\
1476 gdb.flush()\n\
1477 \n\
1478 sys.stdout = GdbOutputFile()\n\
1479 \n\
1480 class GdbOutputErrorFile:\n\
1481 def close(self):\n\
1482 # Do nothing.\n\
1483 return None\n\
1484 \n\
1485 def isatty(self):\n\
1486 return False\n\
1487 \n\
1488 def write(self, s):\n\
1489 gdb.write(s, stream=gdb.STDERR)\n \
1490 \n\
1491 def writelines(self, iterable):\n\
1492 for line in iterable:\n\
1493 self.write(line)\n \
1494 \n\
1495 def flush(self):\n\
1496 gdb.flush()\n\
1497 \n\
1498 sys.stderr = GdbOutputErrorFile()\n\
1499 \n\
1500 # Ideally this would live in the gdb module, but it's intentionally written\n\
1501 # in python, and we need this to bootstrap the gdb module.\n\
1502 \n\
1503 def GdbSetPythonDirectory (dir):\n\
1504 \"Set gdb.PYTHONDIR and update sys.path,etc.\"\n\
1505 old_dir = gdb.PYTHONDIR\n\
1506 gdb.PYTHONDIR = dir\n\
1507 # GDB's python scripts are stored inside gdb.PYTHONDIR. So insert\n\
1508 # that directory name at the start of sys.path to allow the Python\n\
1509 # interpreter to find them.\n\
1510 if old_dir in sys.path:\n\
1511 sys.path.remove (old_dir)\n\
1512 sys.path.insert (0, gdb.PYTHONDIR)\n\
1513 \n\
1514 # Tell python where to find submodules of gdb.\n\
1515 gdb.__path__ = [os.path.join (gdb.PYTHONDIR, 'gdb')]\n\
1516 \n\
1517 # The gdb module is implemented in C rather than in Python. As a result,\n\
1518 # the associated __init.py__ script is not not executed by default when\n\
1519 # the gdb module gets imported. Execute that script manually if it\n\
1520 # exists.\n\
1521 ipy = os.path.join (gdb.PYTHONDIR, 'gdb', '__init__.py')\n\
1522 if os.path.exists (ipy):\n\
1523 execfile (ipy)\n\
1524 \n\
1525 # Install the default gdb.PYTHONDIR.\n\
1526 GdbSetPythonDirectory (gdb.PYTHONDIR)\n\
1527 # Default prompt hook does nothing.\n\
1528 prompt_hook = None\n\
1529 # Ensure that sys.argv is set to something.\n\
1530 # We do not use PySys_SetArgvEx because it did not appear until 2.6.6.\n\
1531 sys.argv = ['']\n\
1532 ");
1533
1534 do_cleanups (cleanup);
1535 }
1536
1537 #endif /* HAVE_PYTHON */
1538
1539 \f
1540
1541 #ifdef HAVE_PYTHON
1542
1543 static PyMethodDef GdbMethods[] =
1544 {
1545 { "history", gdbpy_history, METH_VARARGS,
1546 "Get a value from history" },
1547 { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS,
1548 "Execute a gdb command" },
1549 { "parameter", gdbpy_parameter, METH_VARARGS,
1550 "Return a gdb parameter's value" },
1551
1552 { "breakpoints", gdbpy_breakpoints, METH_NOARGS,
1553 "Return a tuple of all breakpoint objects" },
1554
1555 { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS,
1556 "Find the default visualizer for a Value." },
1557
1558 { "current_progspace", gdbpy_get_current_progspace, METH_NOARGS,
1559 "Return the current Progspace." },
1560 { "progspaces", gdbpy_progspaces, METH_NOARGS,
1561 "Return a sequence of all progspaces." },
1562
1563 { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS,
1564 "Return the current Objfile being loaded, or None." },
1565 { "objfiles", gdbpy_objfiles, METH_NOARGS,
1566 "Return a sequence of all loaded objfiles." },
1567
1568 { "newest_frame", gdbpy_newest_frame, METH_NOARGS,
1569 "newest_frame () -> gdb.Frame.\n\
1570 Return the newest frame object." },
1571 { "selected_frame", gdbpy_selected_frame, METH_NOARGS,
1572 "selected_frame () -> gdb.Frame.\n\
1573 Return the selected frame object." },
1574 { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS,
1575 "stop_reason_string (Integer) -> String.\n\
1576 Return a string explaining unwind stop reason." },
1577
1578 { "lookup_type", (PyCFunction) gdbpy_lookup_type,
1579 METH_VARARGS | METH_KEYWORDS,
1580 "lookup_type (name [, block]) -> type\n\
1581 Return a Type corresponding to the given name." },
1582 { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol,
1583 METH_VARARGS | METH_KEYWORDS,
1584 "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\
1585 Return a tuple with the symbol corresponding to the given name (or None) and\n\
1586 a boolean indicating if name is a field of the current implied argument\n\
1587 `this' (when the current language is object-oriented)." },
1588 { "lookup_global_symbol", (PyCFunction) gdbpy_lookup_global_symbol,
1589 METH_VARARGS | METH_KEYWORDS,
1590 "lookup_global_symbol (name [, domain]) -> symbol\n\
1591 Return the symbol corresponding to the given name (or None)." },
1592 { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS,
1593 "Return the block containing the given pc value, or None." },
1594 { "solib_name", gdbpy_solib_name, METH_VARARGS,
1595 "solib_name (Long) -> String.\n\
1596 Return the name of the shared library holding a given address, or None." },
1597 { "decode_line", gdbpy_decode_line, METH_VARARGS,
1598 "decode_line (String) -> Tuple. Decode a string argument the way\n\
1599 that 'break' or 'edit' does. Return a tuple containing two elements.\n\
1600 The first element contains any unparsed portion of the String parameter\n\
1601 (or None if the string was fully parsed). The second element contains\n\
1602 a tuple that contains all the locations that match, represented as\n\
1603 gdb.Symtab_and_line objects (or None)."},
1604 { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
1605 "parse_and_eval (String) -> Value.\n\
1606 Parse String as an expression, evaluate it, and return the result as a Value."
1607 },
1608 { "find_pc_line", gdbpy_find_pc_line, METH_VARARGS,
1609 "find_pc_line (pc) -> Symtab_and_line.\n\
1610 Return the gdb.Symtab_and_line object corresponding to the pc value." },
1611
1612 { "post_event", gdbpy_post_event, METH_VARARGS,
1613 "Post an event into gdb's event loop." },
1614
1615 { "target_charset", gdbpy_target_charset, METH_NOARGS,
1616 "target_charset () -> string.\n\
1617 Return the name of the current target charset." },
1618 { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS,
1619 "target_wide_charset () -> string.\n\
1620 Return the name of the current target wide charset." },
1621
1622 { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS,
1623 "string_to_argv (String) -> Array.\n\
1624 Parse String and return an argv-like array.\n\
1625 Arguments are separate by spaces and may be quoted."
1626 },
1627 { "write", (PyCFunction)gdbpy_write, METH_VARARGS | METH_KEYWORDS,
1628 "Write a string using gdb's filtered stream." },
1629 { "flush", (PyCFunction)gdbpy_flush, METH_VARARGS | METH_KEYWORDS,
1630 "Flush gdb's filtered stdout stream." },
1631 { "selected_thread", gdbpy_selected_thread, METH_NOARGS,
1632 "selected_thread () -> gdb.InferiorThread.\n\
1633 Return the selected thread object." },
1634 { "selected_inferior", gdbpy_selected_inferior, METH_NOARGS,
1635 "selected_inferior () -> gdb.Inferior.\n\
1636 Return the selected inferior object." },
1637 { "inferiors", gdbpy_inferiors, METH_NOARGS,
1638 "inferiors () -> (gdb.Inferior, ...).\n\
1639 Return a tuple containing all inferiors." },
1640 {NULL, NULL, 0, NULL}
1641 };
1642
1643 #endif /* HAVE_PYTHON */