2010-08-11 Phil Muldoon <pmuldoon@redhat.com>
[deliverable/binutils-gdb.git] / gdb / python / python.c
1 /* General python/gdb code
2
3 Copyright (C) 2008, 2009, 2010 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
32 #include <ctype.h>
33
34 /* True if we should print the stack when catching a Python error,
35 false otherwise. */
36 static int gdbpy_should_print_stack = 1;
37
38 #ifdef HAVE_PYTHON
39
40 #include "python.h"
41 #include "libiberty.h"
42 #include "cli/cli-decode.h"
43 #include "charset.h"
44 #include "top.h"
45 #include "solib.h"
46 #include "python-internal.h"
47 #include "linespec.h"
48 #include "source.h"
49 #include "version.h"
50 #include "target.h"
51 #include "gdbthread.h"
52
53 static PyMethodDef GdbMethods[];
54
55 PyObject *gdb_module;
56
57 /* Some string constants we may wish to use. */
58 PyObject *gdbpy_to_string_cst;
59 PyObject *gdbpy_children_cst;
60 PyObject *gdbpy_display_hint_cst;
61 PyObject *gdbpy_doc_cst;
62 PyObject *gdbpy_enabled_cst;
63
64 /* The GdbError exception. */
65 PyObject *gdbpy_gdberror_exc;
66
67 /* Architecture and language to be used in callbacks from
68 the Python interpreter. */
69 struct gdbarch *python_gdbarch;
70 const struct language_defn *python_language;
71
72 /* Restore global language and architecture and Python GIL state
73 when leaving the Python interpreter. */
74
75 struct python_env
76 {
77 PyGILState_STATE state;
78 struct gdbarch *gdbarch;
79 const struct language_defn *language;
80 };
81
82 static void
83 restore_python_env (void *p)
84 {
85 struct python_env *env = (struct python_env *)p;
86
87 PyGILState_Release (env->state);
88 python_gdbarch = env->gdbarch;
89 python_language = env->language;
90 xfree (env);
91 }
92
93 /* Called before entering the Python interpreter to install the
94 current language and architecture to be used for Python values. */
95
96 struct cleanup *
97 ensure_python_env (struct gdbarch *gdbarch,
98 const struct language_defn *language)
99 {
100 struct python_env *env = xmalloc (sizeof *env);
101
102 env->state = PyGILState_Ensure ();
103 env->gdbarch = python_gdbarch;
104 env->language = python_language;
105
106 python_gdbarch = gdbarch;
107 python_language = language;
108
109 return make_cleanup (restore_python_env, env);
110 }
111
112
113 /* Given a command_line, return a command string suitable for passing
114 to Python. Lines in the string are separated by newlines. The
115 return value is allocated using xmalloc and the caller is
116 responsible for freeing it. */
117
118 static char *
119 compute_python_string (struct command_line *l)
120 {
121 struct command_line *iter;
122 char *script = NULL;
123 int size = 0;
124 int here;
125
126 for (iter = l; iter; iter = iter->next)
127 size += strlen (iter->line) + 1;
128
129 script = xmalloc (size + 1);
130 here = 0;
131 for (iter = l; iter; iter = iter->next)
132 {
133 int len = strlen (iter->line);
134
135 strcpy (&script[here], iter->line);
136 here += len;
137 script[here++] = '\n';
138 }
139 script[here] = '\0';
140 return script;
141 }
142
143 /* Take a command line structure representing a 'python' command, and
144 evaluate its body using the Python interpreter. */
145
146 void
147 eval_python_from_control_command (struct command_line *cmd)
148 {
149 int ret;
150 char *script;
151 struct cleanup *cleanup;
152
153 if (cmd->body_count != 1)
154 error (_("Invalid \"python\" block structure."));
155
156 cleanup = ensure_python_env (get_current_arch (), current_language);
157
158 script = compute_python_string (cmd->body_list[0]);
159 ret = PyRun_SimpleString (script);
160 xfree (script);
161 if (ret)
162 {
163 gdbpy_print_stack ();
164 error (_("Error while executing Python code."));
165 }
166
167 do_cleanups (cleanup);
168 }
169
170 /* Implementation of the gdb "python" command. */
171
172 static void
173 python_command (char *arg, int from_tty)
174 {
175 struct cleanup *cleanup;
176
177 cleanup = ensure_python_env (get_current_arch (), current_language);
178 while (arg && *arg && isspace (*arg))
179 ++arg;
180 if (arg && *arg)
181 {
182 if (PyRun_SimpleString (arg))
183 {
184 gdbpy_print_stack ();
185 error (_("Error while executing Python code."));
186 }
187 }
188 else
189 {
190 struct command_line *l = get_command_line (python_control, "");
191
192 make_cleanup_free_command_lines (&l);
193 execute_control_command_untraced (l);
194 }
195
196 do_cleanups (cleanup);
197 }
198
199 \f
200
201 /* Transform a gdb parameters's value into a Python value. May return
202 NULL (and set a Python exception) on error. Helper function for
203 get_parameter. */
204 PyObject *
205 gdbpy_parameter_value (enum var_types type, void *var)
206 {
207 switch (type)
208 {
209 case var_string:
210 case var_string_noescape:
211 case var_optional_filename:
212 case var_filename:
213 case var_enum:
214 {
215 char *str = * (char **) var;
216
217 if (! str)
218 str = "";
219 return PyString_Decode (str, strlen (str), host_charset (), NULL);
220 }
221
222 case var_boolean:
223 {
224 if (* (int *) var)
225 Py_RETURN_TRUE;
226 else
227 Py_RETURN_FALSE;
228 }
229
230 case var_auto_boolean:
231 {
232 enum auto_boolean ab = * (enum auto_boolean *) var;
233
234 if (ab == AUTO_BOOLEAN_TRUE)
235 Py_RETURN_TRUE;
236 else if (ab == AUTO_BOOLEAN_FALSE)
237 Py_RETURN_FALSE;
238 else
239 Py_RETURN_NONE;
240 }
241
242 case var_integer:
243 if ((* (int *) var) == INT_MAX)
244 Py_RETURN_NONE;
245 /* Fall through. */
246 case var_zinteger:
247 return PyLong_FromLong (* (int *) var);
248
249 case var_uinteger:
250 {
251 unsigned int val = * (unsigned int *) var;
252
253 if (val == UINT_MAX)
254 Py_RETURN_NONE;
255 return PyLong_FromUnsignedLong (val);
256 }
257 }
258
259 return PyErr_Format (PyExc_RuntimeError,
260 _("Programmer error: unhandled type."));
261 }
262
263 /* A Python function which returns a gdb parameter's value as a Python
264 value. */
265
266 PyObject *
267 gdbpy_parameter (PyObject *self, PyObject *args)
268 {
269 struct cmd_list_element *alias, *prefix, *cmd;
270 char *arg, *newarg;
271 int found = -1;
272 volatile struct gdb_exception except;
273
274 if (! PyArg_ParseTuple (args, "s", &arg))
275 return NULL;
276
277 newarg = concat ("show ", arg, (char *) NULL);
278
279 TRY_CATCH (except, RETURN_MASK_ALL)
280 {
281 found = lookup_cmd_composition (newarg, &alias, &prefix, &cmd);
282 }
283 xfree (newarg);
284 GDB_PY_HANDLE_EXCEPTION (except);
285 if (!found)
286 return PyErr_Format (PyExc_RuntimeError,
287 _("Could not find parameter `%s'."), arg);
288
289 if (! cmd->var)
290 return PyErr_Format (PyExc_RuntimeError,
291 _("`%s' is not a parameter."), arg);
292 return gdbpy_parameter_value (cmd->var_type, cmd->var);
293 }
294
295 /* Wrapper for target_charset. */
296
297 static PyObject *
298 gdbpy_target_charset (PyObject *self, PyObject *args)
299 {
300 const char *cset = target_charset (python_gdbarch);
301
302 return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
303 }
304
305 /* Wrapper for target_wide_charset. */
306
307 static PyObject *
308 gdbpy_target_wide_charset (PyObject *self, PyObject *args)
309 {
310 const char *cset = target_wide_charset (python_gdbarch);
311
312 return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
313 }
314
315 /* A Python function which evaluates a string using the gdb CLI. */
316
317 static PyObject *
318 execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw)
319 {
320 char *arg;
321 PyObject *from_tty_obj = NULL, *to_string_obj = NULL;
322 int from_tty, to_string;
323 volatile struct gdb_exception except;
324 static char *keywords[] = {"command", "from_tty", "to_string", NULL };
325 char *result = NULL;
326
327 if (! PyArg_ParseTupleAndKeywords (args, kw, "s|O!O!", keywords, &arg,
328 &PyBool_Type, &from_tty_obj,
329 &PyBool_Type, &to_string_obj))
330 return NULL;
331
332 from_tty = 0;
333 if (from_tty_obj)
334 {
335 int cmp = PyObject_IsTrue (from_tty_obj);
336 if (cmp < 0)
337 return NULL;
338 from_tty = cmp;
339 }
340
341 to_string = 0;
342 if (to_string_obj)
343 {
344 int cmp = PyObject_IsTrue (to_string_obj);
345 if (cmp < 0)
346 return NULL;
347 to_string = cmp;
348 }
349
350 TRY_CATCH (except, RETURN_MASK_ALL)
351 {
352 /* Copy the argument text in case the command modifies it. */
353 char *copy = xstrdup (arg);
354 struct cleanup *cleanup = make_cleanup (xfree, copy);
355
356 if (to_string)
357 result = execute_command_to_string (copy, from_tty);
358 else
359 {
360 result = NULL;
361 execute_command (copy, from_tty);
362 }
363
364 do_cleanups (cleanup);
365 }
366 GDB_PY_HANDLE_EXCEPTION (except);
367
368 /* Do any commands attached to breakpoint we stopped at. */
369 bpstat_do_actions ();
370
371 if (result)
372 {
373 PyObject *r = PyString_FromString (result);
374 xfree (result);
375 return r;
376 }
377 Py_RETURN_NONE;
378 }
379
380 /* Implementation of gdb.solib_name (Long) -> String.
381 Returns the name of the shared library holding a given address, or None. */
382
383 static PyObject *
384 gdbpy_solib_name (PyObject *self, PyObject *args)
385 {
386 char *soname;
387 PyObject *str_obj;
388 #ifdef PY_LONG_LONG
389 unsigned PY_LONG_LONG pc;
390 const char *format = "K";
391 #else
392 unsigned long pc;
393 const char *format = "k";
394 #endif
395
396 if (!PyArg_ParseTuple (args, format, &pc))
397 return NULL;
398
399 soname = solib_name_from_address (current_program_space, pc);
400 if (soname)
401 str_obj = PyString_Decode (soname, strlen (soname), host_charset (), NULL);
402 else
403 {
404 str_obj = Py_None;
405 Py_INCREF (Py_None);
406 }
407
408 return str_obj;
409 }
410
411 /* A Python function which is a wrapper for decode_line_1. */
412
413 static PyObject *
414 gdbpy_decode_line (PyObject *self, PyObject *args)
415 {
416 struct symtabs_and_lines sals = { NULL, 0 }; /* Initialize to appease gcc. */
417 struct symtab_and_line sal;
418 char *arg = NULL;
419 char *copy = NULL;
420 struct cleanup *cleanups;
421 PyObject *result = NULL;
422 PyObject *return_result = NULL;
423 PyObject *unparsed = NULL;
424 volatile struct gdb_exception except;
425
426 if (! PyArg_ParseTuple (args, "|s", &arg))
427 return NULL;
428
429 cleanups = ensure_python_env (get_current_arch (), current_language);
430
431 TRY_CATCH (except, RETURN_MASK_ALL)
432 {
433 if (arg)
434 {
435 arg = xstrdup (arg);
436 make_cleanup (xfree, arg);
437 copy = arg;
438 sals = decode_line_1 (&copy, 0, 0, 0, 0, 0);
439 make_cleanup (xfree, sals.sals);
440 }
441 else
442 {
443 set_default_source_symtab_and_line ();
444 sal = get_current_source_symtab_and_line ();
445 sals.sals = &sal;
446 sals.nelts = 1;
447 }
448 }
449 if (except.reason < 0)
450 {
451 do_cleanups (cleanups);
452 /* We know this will always throw. */
453 GDB_PY_HANDLE_EXCEPTION (except);
454 }
455
456 if (sals.nelts)
457 {
458 int i;
459
460 result = PyTuple_New (sals.nelts);
461 if (! result)
462 goto error;
463 for (i = 0; i < sals.nelts; ++i)
464 {
465 PyObject *obj;
466 char *str;
467
468 obj = symtab_and_line_to_sal_object (sals.sals[i]);
469 if (! obj)
470 {
471 Py_DECREF (result);
472 goto error;
473 }
474
475 PyTuple_SetItem (result, i, obj);
476 }
477 }
478 else
479 {
480 result = Py_None;
481 Py_INCREF (Py_None);
482 }
483
484 return_result = PyTuple_New (2);
485 if (! return_result)
486 {
487 Py_DECREF (result);
488 goto error;
489 }
490
491 if (copy && strlen (copy) > 0)
492 unparsed = PyString_FromString (copy);
493 else
494 {
495 unparsed = Py_None;
496 Py_INCREF (Py_None);
497 }
498
499 PyTuple_SetItem (return_result, 0, unparsed);
500 PyTuple_SetItem (return_result, 1, result);
501
502 do_cleanups (cleanups);
503
504 return return_result;
505
506 error:
507 do_cleanups (cleanups);
508 return NULL;
509 }
510
511 /* Parse a string and evaluate it as an expression. */
512 static PyObject *
513 gdbpy_parse_and_eval (PyObject *self, PyObject *args)
514 {
515 char *expr_str;
516 struct value *result = NULL;
517 volatile struct gdb_exception except;
518
519 if (!PyArg_ParseTuple (args, "s", &expr_str))
520 return NULL;
521
522 TRY_CATCH (except, RETURN_MASK_ALL)
523 {
524 result = parse_and_eval (expr_str);
525 }
526 GDB_PY_HANDLE_EXCEPTION (except);
527
528 return value_to_value_object (result);
529 }
530
531 /* Read a file as Python code. STREAM is the input file; FILE is the
532 name of the file.
533 STREAM is not closed, that is the caller's responsibility. */
534
535 void
536 source_python_script (FILE *stream, const char *file)
537 {
538 struct cleanup *cleanup;
539
540 cleanup = ensure_python_env (get_current_arch (), current_language);
541
542 /* Note: If an exception occurs python will print the traceback and
543 clear the error indicator. */
544 PyRun_SimpleFile (stream, file);
545
546 do_cleanups (cleanup);
547 }
548
549 \f
550
551 /* Printing. */
552
553 /* A python function to write a single string using gdb's filtered
554 output stream. */
555 static PyObject *
556 gdbpy_write (PyObject *self, PyObject *args)
557 {
558 char *arg;
559
560 if (! PyArg_ParseTuple (args, "s", &arg))
561 return NULL;
562 printf_filtered ("%s", arg);
563 Py_RETURN_NONE;
564 }
565
566 /* A python function to flush gdb's filtered output stream. */
567 static PyObject *
568 gdbpy_flush (PyObject *self, PyObject *args)
569 {
570 gdb_flush (gdb_stdout);
571 Py_RETURN_NONE;
572 }
573
574 /* Print a python exception trace, or print nothing and clear the
575 python exception, depending on gdbpy_should_print_stack. Only call
576 this if a python exception is set. */
577 void
578 gdbpy_print_stack (void)
579 {
580 if (gdbpy_should_print_stack)
581 {
582 PyErr_Print ();
583 /* PyErr_Print doesn't necessarily end output with a newline.
584 This works because Python's stdout/stderr is fed through
585 printf_filtered. */
586 begin_line ();
587 }
588 else
589 PyErr_Clear ();
590 }
591
592 \f
593
594 /* Return the current Progspace.
595 There always is one. */
596
597 static PyObject *
598 gdbpy_get_current_progspace (PyObject *unused1, PyObject *unused2)
599 {
600 PyObject *result;
601
602 result = pspace_to_pspace_object (current_program_space);
603 if (result)
604 Py_INCREF (result);
605 return result;
606 }
607
608 /* Return a sequence holding all the Progspaces. */
609
610 static PyObject *
611 gdbpy_progspaces (PyObject *unused1, PyObject *unused2)
612 {
613 struct program_space *ps;
614 PyObject *list;
615
616 list = PyList_New (0);
617 if (!list)
618 return NULL;
619
620 ALL_PSPACES (ps)
621 {
622 PyObject *item = pspace_to_pspace_object (ps);
623
624 if (!item || PyList_Append (list, item) == -1)
625 {
626 Py_DECREF (list);
627 return NULL;
628 }
629 }
630
631 return list;
632 }
633
634 \f
635
636 /* The "current" objfile. This is set when gdb detects that a new
637 objfile has been loaded. It is only set for the duration of a call to
638 source_python_script_for_objfile; it is NULL at other times. */
639 static struct objfile *gdbpy_current_objfile;
640
641 /* Set the current objfile to OBJFILE and then read STREAM,FILE as
642 Python code. */
643
644 void
645 source_python_script_for_objfile (struct objfile *objfile,
646 FILE *stream, const char *file)
647 {
648 struct cleanup *cleanups;
649
650 cleanups = ensure_python_env (get_objfile_arch (objfile), current_language);
651 gdbpy_current_objfile = objfile;
652
653 /* Note: If an exception occurs python will print the traceback and
654 clear the error indicator. */
655 PyRun_SimpleFile (stream, file);
656
657 do_cleanups (cleanups);
658 gdbpy_current_objfile = NULL;
659 }
660
661 /* Return the current Objfile, or None if there isn't one. */
662
663 static PyObject *
664 gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2)
665 {
666 PyObject *result;
667
668 if (! gdbpy_current_objfile)
669 Py_RETURN_NONE;
670
671 result = objfile_to_objfile_object (gdbpy_current_objfile);
672 if (result)
673 Py_INCREF (result);
674 return result;
675 }
676
677 /* Return a sequence holding all the Objfiles. */
678
679 static PyObject *
680 gdbpy_objfiles (PyObject *unused1, PyObject *unused2)
681 {
682 struct objfile *objf;
683 PyObject *list;
684
685 list = PyList_New (0);
686 if (!list)
687 return NULL;
688
689 ALL_OBJFILES (objf)
690 {
691 PyObject *item = objfile_to_objfile_object (objf);
692
693 if (!item || PyList_Append (list, item) == -1)
694 {
695 Py_DECREF (list);
696 return NULL;
697 }
698 }
699
700 return list;
701 }
702
703 #else /* HAVE_PYTHON */
704
705 /* Dummy implementation of the gdb "python" command. */
706
707 static void
708 python_command (char *arg, int from_tty)
709 {
710 while (arg && *arg && isspace (*arg))
711 ++arg;
712 if (arg && *arg)
713 error (_("Python scripting is not supported in this copy of GDB."));
714 else
715 {
716 struct command_line *l = get_command_line (python_control, "");
717 struct cleanup *cleanups = make_cleanup_free_command_lines (&l);
718
719 execute_control_command_untraced (l);
720 do_cleanups (cleanups);
721 }
722 }
723
724 void
725 eval_python_from_control_command (struct command_line *cmd)
726 {
727 error (_("Python scripting is not supported in this copy of GDB."));
728 }
729
730 void
731 source_python_script (FILE *stream, const char *file)
732 {
733 throw_error (UNSUPPORTED_ERROR,
734 _("Python scripting is not supported in this copy of GDB."));
735 }
736
737 #endif /* HAVE_PYTHON */
738
739 \f
740
741 /* Lists for 'maint set python' commands. */
742
743 struct cmd_list_element *set_python_list;
744 struct cmd_list_element *show_python_list;
745
746 /* Function for use by 'maint set python' prefix command. */
747
748 static void
749 set_python (char *args, int from_tty)
750 {
751 help_list (set_python_list, "maintenance set python ", -1, gdb_stdout);
752 }
753
754 /* Function for use by 'maint show python' prefix command. */
755
756 static void
757 show_python (char *args, int from_tty)
758 {
759 cmd_show_list (show_python_list, from_tty, "");
760 }
761
762 /* Initialize the Python code. */
763
764 /* Provide a prototype to silence -Wmissing-prototypes. */
765 extern initialize_file_ftype _initialize_python;
766
767 void
768 _initialize_python (void)
769 {
770 add_com ("python", class_obscure, python_command,
771 #ifdef HAVE_PYTHON
772 _("\
773 Evaluate a Python command.\n\
774 \n\
775 The command can be given as an argument, for instance:\n\
776 \n\
777 python print 23\n\
778 \n\
779 If no argument is given, the following lines are read and used\n\
780 as the Python commands. Type a line containing \"end\" to indicate\n\
781 the end of the command.")
782 #else /* HAVE_PYTHON */
783 _("\
784 Evaluate a Python command.\n\
785 \n\
786 Python scripting is not supported in this copy of GDB.\n\
787 This command is only a placeholder.")
788 #endif /* HAVE_PYTHON */
789 );
790
791 add_prefix_cmd ("python", no_class, show_python,
792 _("Prefix command for python maintenance settings."),
793 &show_python_list, "maintenance show python ", 0,
794 &maintenance_show_cmdlist);
795 add_prefix_cmd ("python", no_class, set_python,
796 _("Prefix command for python maintenance settings."),
797 &set_python_list, "maintenance set python ", 0,
798 &maintenance_set_cmdlist);
799
800 add_setshow_boolean_cmd ("print-stack", class_maintenance,
801 &gdbpy_should_print_stack, _("\
802 Enable or disable printing of Python stack dump on error."), _("\
803 Show whether Python stack will be printed on error."), _("\
804 Enables or disables printing of Python stack traces."),
805 NULL, NULL,
806 &set_python_list,
807 &show_python_list);
808
809 #ifdef HAVE_PYTHON
810 #ifdef WITH_PYTHON_PATH
811 /* Work around problem where python gets confused about where it is,
812 and then can't find its libraries, etc.
813 NOTE: Python assumes the following layout:
814 /foo/bin/python
815 /foo/lib/pythonX.Y/...
816 This must be done before calling Py_Initialize. */
817 Py_SetProgramName (concat (ldirname (python_libdir), SLASH_STRING, "bin",
818 SLASH_STRING, "python", NULL));
819 #endif
820
821 Py_Initialize ();
822 PyEval_InitThreads ();
823
824 gdb_module = Py_InitModule ("gdb", GdbMethods);
825
826 /* The casts to (char*) are for python 2.4. */
827 PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version);
828 PyModule_AddStringConstant (gdb_module, "HOST_CONFIG", (char*) host_name);
829 PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name);
830 {
831 char *gdb_pythondir;
832
833 gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", NULL);
834 PyModule_AddStringConstant (gdb_module, "PYTHONDIR", gdb_pythondir);
835 xfree (gdb_pythondir);
836 }
837
838 gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL);
839 PyModule_AddObject (gdb_module, "GdbError", gdbpy_gdberror_exc);
840
841 gdbpy_initialize_auto_load ();
842 gdbpy_initialize_values ();
843 gdbpy_initialize_frames ();
844 gdbpy_initialize_commands ();
845 gdbpy_initialize_symbols ();
846 gdbpy_initialize_symtabs ();
847 gdbpy_initialize_blocks ();
848 gdbpy_initialize_functions ();
849 gdbpy_initialize_parameters ();
850 gdbpy_initialize_types ();
851 gdbpy_initialize_pspace ();
852 gdbpy_initialize_objfile ();
853 gdbpy_initialize_breakpoints ();
854 gdbpy_initialize_lazy_string ();
855 gdbpy_initialize_thread ();
856 gdbpy_initialize_inferior ();
857
858 PyRun_SimpleString ("import gdb");
859 PyRun_SimpleString ("gdb.pretty_printers = []");
860
861 gdbpy_to_string_cst = PyString_FromString ("to_string");
862 gdbpy_children_cst = PyString_FromString ("children");
863 gdbpy_display_hint_cst = PyString_FromString ("display_hint");
864 gdbpy_doc_cst = PyString_FromString ("__doc__");
865 gdbpy_enabled_cst = PyString_FromString ("enabled");
866
867 /* Create a couple objects which are used for Python's stdout and
868 stderr. */
869 PyRun_SimpleString ("\
870 import sys\n\
871 class GdbOutputFile:\n\
872 def close(self):\n\
873 # Do nothing.\n\
874 return None\n\
875 \n\
876 def isatty(self):\n\
877 return False\n\
878 \n\
879 def write(self, s):\n\
880 gdb.write(s)\n\
881 \n\
882 def writelines(self, iterable):\n\
883 for line in iterable:\n\
884 self.write(line)\n\
885 \n\
886 def flush(self):\n\
887 gdb.flush()\n\
888 \n\
889 sys.stderr = GdbOutputFile()\n\
890 sys.stdout = GdbOutputFile()\n\
891 \n\
892 # GDB's python scripts are stored inside gdb.PYTHONDIR. So insert\n\
893 # that directory name at the start of sys.path to allow the Python\n\
894 # interpreter to find them.\n\
895 sys.path.insert(0, gdb.PYTHONDIR)\n\
896 \n\
897 # The gdb module is implemented in C rather than in Python. As a result,\n\
898 # the associated __init.py__ script is not not executed by default when\n\
899 # the gdb module gets imported. Execute that script manually if it exists.\n\
900 gdb.__path__ = [gdb.PYTHONDIR + '/gdb']\n\
901 from os.path import exists\n\
902 ipy = gdb.PYTHONDIR + '/gdb/__init__.py'\n\
903 if exists (ipy):\n\
904 execfile (ipy)\n\
905 ");
906
907 /* Release the GIL while gdb runs. */
908 PyThreadState_Swap (NULL);
909 PyEval_ReleaseLock ();
910
911 #endif /* HAVE_PYTHON */
912 }
913
914 \f
915
916 #if HAVE_PYTHON
917
918 static PyMethodDef GdbMethods[] =
919 {
920 { "history", gdbpy_history, METH_VARARGS,
921 "Get a value from history" },
922 { "execute", (PyCFunction) execute_gdb_command, METH_VARARGS | METH_KEYWORDS,
923 "Execute a gdb command" },
924 { "parameter", gdbpy_parameter, METH_VARARGS,
925 "Return a gdb parameter's value" },
926
927 { "breakpoints", gdbpy_breakpoints, METH_NOARGS,
928 "Return a tuple of all breakpoint objects" },
929
930 { "default_visualizer", gdbpy_default_visualizer, METH_VARARGS,
931 "Find the default visualizer for a Value." },
932
933 { "current_progspace", gdbpy_get_current_progspace, METH_NOARGS,
934 "Return the current Progspace." },
935 { "progspaces", gdbpy_progspaces, METH_NOARGS,
936 "Return a sequence of all progspaces." },
937
938 { "current_objfile", gdbpy_get_current_objfile, METH_NOARGS,
939 "Return the current Objfile being loaded, or None." },
940 { "objfiles", gdbpy_objfiles, METH_NOARGS,
941 "Return a sequence of all loaded objfiles." },
942
943 { "selected_frame", gdbpy_selected_frame, METH_NOARGS,
944 "selected_frame () -> gdb.Frame.\n\
945 Return the selected frame object." },
946 { "frame_stop_reason_string", gdbpy_frame_stop_reason_string, METH_VARARGS,
947 "stop_reason_string (Integer) -> String.\n\
948 Return a string explaining unwind stop reason." },
949
950 { "lookup_type", (PyCFunction) gdbpy_lookup_type,
951 METH_VARARGS | METH_KEYWORDS,
952 "lookup_type (name [, block]) -> type\n\
953 Return a Type corresponding to the given name." },
954 { "lookup_symbol", (PyCFunction) gdbpy_lookup_symbol,
955 METH_VARARGS | METH_KEYWORDS,
956 "lookup_symbol (name [, block] [, domain]) -> (symbol, is_field_of_this)\n\
957 Return a tuple with the symbol corresponding to the given name (or None) and\n\
958 a boolean indicating if name is a field of the current implied argument\n\
959 `this' (when the current language is object-oriented)." },
960 { "block_for_pc", gdbpy_block_for_pc, METH_VARARGS,
961 "Return the block containing the given pc value, or None." },
962 { "solib_name", gdbpy_solib_name, METH_VARARGS,
963 "solib_name (Long) -> String.\n\
964 Return the name of the shared library holding a given address, or None." },
965 { "decode_line", gdbpy_decode_line, METH_VARARGS,
966 "decode_line (String) -> Tuple. Decode a string argument the way\n\
967 that 'break' or 'edit' does. Return a tuple containing two elements.\n\
968 The first element contains any unparsed portion of the String parameter\n\
969 (or None if the string was fully parsed). The second element contains\n\
970 a tuple that contains all the locations that match, represented as\n\
971 gdb.Symtab_and_line objects (or None)."},
972 { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
973 "parse_and_eval (String) -> Value.\n\
974 Parse String as an expression, evaluate it, and return the result as a Value."
975 },
976
977 { "target_charset", gdbpy_target_charset, METH_NOARGS,
978 "target_charset () -> string.\n\
979 Return the name of the current target charset." },
980 { "target_wide_charset", gdbpy_target_wide_charset, METH_NOARGS,
981 "target_wide_charset () -> string.\n\
982 Return the name of the current target wide charset." },
983
984 { "string_to_argv", gdbpy_string_to_argv, METH_VARARGS,
985 "string_to_argv (String) -> Array.\n\
986 Parse String and return an argv-like array.\n\
987 Arguments are separate by spaces and may be quoted."
988 },
989
990 { "write", gdbpy_write, METH_VARARGS,
991 "Write a string using gdb's filtered stream." },
992 { "flush", gdbpy_flush, METH_NOARGS,
993 "Flush gdb's filtered stdout stream." },
994 { "selected_thread", gdbpy_selected_thread, METH_NOARGS,
995 "selected_thread () -> gdb.InferiorThread.\n\
996 Return the selected thread object." },
997 { "inferiors", gdbpy_inferiors, METH_NOARGS,
998 "inferiors () -> (gdb.Inferior, ...).\n\
999 Return a tuple containing all inferiors." },
1000 {NULL, NULL, 0, NULL}
1001 };
1002
1003 #endif /* HAVE_PYTHON */
This page took 0.049893 seconds and 4 git commands to generate.