OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gdb-7.1/] [gdb/] [python/] [py-prettyprint.c] - Blame information for rev 227

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 227 jeremybenn
/* Python pretty-printing
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 "exceptions.h"
22
#include "objfiles.h"
23
#include "symtab.h"
24
#include "language.h"
25
#include "valprint.h"
26
 
27
#include "python.h"
28
 
29
#ifdef HAVE_PYTHON
30
#include "python-internal.h"
31
 
32
 
33
/* Helper function for find_pretty_printer which iterates over a list,
34
   calls each function and inspects output.  This will return a
35
   printer object if one recognizes VALUE.  If no printer is found, it
36
   will return None.  On error, it will set the Python error and
37
   return NULL.  */
38
static PyObject *
39
search_pp_list (PyObject *list, PyObject *value)
40
{
41
  Py_ssize_t pp_list_size, list_index;
42
  PyObject *function, *printer = NULL;
43
 
44
  pp_list_size = PyList_Size (list);
45
  for (list_index = 0; list_index < pp_list_size; list_index++)
46
    {
47
      function = PyList_GetItem (list, list_index);
48
      if (! function)
49
        return NULL;
50
 
51
      printer = PyObject_CallFunctionObjArgs (function, value, NULL);
52
      if (! printer)
53
        return NULL;
54
      else if (printer != Py_None)
55
        return printer;
56
 
57
      Py_DECREF (printer);
58
    }
59
 
60
  Py_RETURN_NONE;
61
}
62
 
63
/* Find the pretty-printing constructor function for VALUE.  If no
64
   pretty-printer exists, return None.  If one exists, return a new
65
   reference.  On error, set the Python error and return NULL.  */
66
static PyObject *
67
find_pretty_printer (PyObject *value)
68
{
69
  PyObject *pp_list = NULL;
70
  PyObject *function = NULL;
71
  struct objfile *obj;
72
  volatile struct gdb_exception except;
73
 
74
  /* Look at the pretty-printer dictionary for each objfile.  */
75
  ALL_OBJFILES (obj)
76
  {
77
    PyObject *objf = objfile_to_objfile_object (obj);
78
    if (!objf)
79
      {
80
        /* Ignore the error and continue.  */
81
        PyErr_Clear ();
82
        continue;
83
      }
84
 
85
    pp_list = objfpy_get_printers (objf, NULL);
86
    function = search_pp_list (pp_list, value);
87
 
88
    /* If there is an error in any objfile list, abort the search and
89
       exit.  */
90
    if (! function)
91
      {
92
        Py_XDECREF (pp_list);
93
        return NULL;
94
      }
95
 
96
    if (function != Py_None)
97
      goto done;
98
 
99
    Py_DECREF (function);
100
    Py_XDECREF (pp_list);
101
  }
102
 
103
  pp_list = NULL;
104
  /* Fetch the global pretty printer dictionary.  */
105
  if (! PyObject_HasAttrString (gdb_module, "pretty_printers"))
106
    {
107
      function = Py_None;
108
      Py_INCREF (function);
109
      goto done;
110
    }
111
  pp_list = PyObject_GetAttrString (gdb_module, "pretty_printers");
112
  if (! pp_list)
113
    goto done;
114
  if (! PyList_Check (pp_list))
115
    goto done;
116
 
117
  function = search_pp_list (pp_list, value);
118
 
119
 done:
120
  Py_XDECREF (pp_list);
121
 
122
  return function;
123
}
124
 
125
 
126
/* Pretty-print a single value, via the printer object PRINTER.
127
   If the function returns a string, a PyObject containing the string
128
   is returned.  Otherwise, if the function returns a value,
129
   *OUT_VALUE is set to the value, and NULL is returned.  On error,
130
   *OUT_VALUE is set to NULL, and NULL is returned.  */
131
static PyObject *
132
pretty_print_one_value (PyObject *printer, struct value **out_value)
133
{
134
  volatile struct gdb_exception except;
135
  PyObject *result = NULL;
136
 
137
  *out_value = NULL;
138
  TRY_CATCH (except, RETURN_MASK_ALL)
139
    {
140
      result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL);
141
      if (result)
142
        {
143
          if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result))
144
            {
145
              *out_value = convert_value_from_python (result);
146
              if (PyErr_Occurred ())
147
                *out_value = NULL;
148
              Py_DECREF (result);
149
              result = NULL;
150
            }
151
        }
152
    }
153
 
154
  return result;
155
}
156
 
157
/* Return the display hint for the object printer, PRINTER.  Return
158
   NULL if there is no display_hint method, or if the method did not
159
   return a string.  On error, print stack trace and return NULL.  On
160
   success, return an xmalloc()d string.  */
161
char *
162
gdbpy_get_display_hint (PyObject *printer)
163
{
164
  PyObject *hint;
165
  char *result = NULL;
166
 
167
  if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
168
    return NULL;
169
 
170
  hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL);
171
  if (gdbpy_is_string (hint))
172
    result = python_string_to_host_string (hint);
173
  if (hint)
174
    Py_DECREF (hint);
175
  else
176
    gdbpy_print_stack ();
177
 
178
  return result;
179
}
180
 
181
/* Helper for apply_val_pretty_printer which calls to_string and
182
   formats the result.  */
183
static void
184
print_string_repr (PyObject *printer, const char *hint,
185
                   struct ui_file *stream, int recurse,
186
                   const struct value_print_options *options,
187
                   const struct language_defn *language,
188
                   struct gdbarch *gdbarch)
189
{
190
  struct value *replacement = NULL;
191
  PyObject *py_str = NULL;
192
 
193
  py_str = pretty_print_one_value (printer, &replacement);
194
  if (py_str)
195
    {
196
      gdb_byte *output = NULL;
197
      long length;
198
      struct type *type;
199
      char *encoding = NULL;
200
      PyObject *string = NULL;
201
      int is_lazy;
202
 
203
      is_lazy = gdbpy_is_lazy_string (py_str);
204
      if (is_lazy)
205
        output = gdbpy_extract_lazy_string (py_str, &type, &length, &encoding);
206
      else
207
        {
208
          string = python_string_to_target_python_string (py_str);
209
          if (string)
210
            {
211
              output = PyString_AsString (string);
212
              length = PyString_Size (string);
213
              type = builtin_type (gdbarch)->builtin_char;
214
            }
215
          else
216
            gdbpy_print_stack ();
217
 
218
        }
219
 
220
      if (output)
221
        {
222
          if (is_lazy || (hint && !strcmp (hint, "string")))
223
            LA_PRINT_STRING (stream, type, output, length, encoding,
224
                             0, options);
225
          else
226
            fputs_filtered (output, stream);
227
        }
228
      else
229
        gdbpy_print_stack ();
230
 
231
      if (string)
232
        Py_DECREF (string);
233
      else
234
        xfree (output);
235
 
236
      xfree (encoding);
237
      Py_DECREF (py_str);
238
    }
239
  else if (replacement)
240
    {
241
      struct value_print_options opts = *options;
242
 
243
      opts.addressprint = 0;
244
      common_val_print (replacement, stream, recurse, &opts, language);
245
    }
246
  else
247
    gdbpy_print_stack ();
248
}
249
 
250
static void
251
py_restore_tstate (void *p)
252
{
253
  PyFrameObject *frame = p;
254
  PyThreadState *tstate = PyThreadState_GET ();
255
  tstate->frame = frame;
256
}
257
 
258
/* Create a dummy PyFrameObject, needed to work around
259
   a Python-2.4 bug with generators.  */
260
static PyObject *
261
push_dummy_python_frame ()
262
{
263
  PyObject *empty_string, *null_tuple, *globals;
264
  PyCodeObject *code;
265
  PyFrameObject *frame;
266
  PyThreadState *tstate;
267
 
268
  empty_string = PyString_FromString ("");
269
  if (!empty_string)
270
    return NULL;
271
 
272
  null_tuple = PyTuple_New (0);
273
  if (!null_tuple)
274
    {
275
      Py_DECREF (empty_string);
276
      return NULL;
277
    }
278
 
279
  code = PyCode_New (0,                  /* argcount */
280
                     0,                  /* nlocals */
281
                     0,                  /* stacksize */
282
                     0,                  /* flags */
283
                     empty_string,      /* code */
284
                     null_tuple,        /* consts */
285
                     null_tuple,        /* names */
286
                     null_tuple,        /* varnames */
287
#if PYTHON_API_VERSION >= 1010
288
                     null_tuple,        /* freevars */
289
                     null_tuple,        /* cellvars */
290
#endif
291
                     empty_string,      /* filename */
292
                     empty_string,      /* name */
293
                     1,                 /* firstlineno */
294
                     empty_string       /* lnotab */
295
                    );
296
 
297
  Py_DECREF (empty_string);
298
  Py_DECREF (null_tuple);
299
 
300
  if (!code)
301
    return NULL;
302
 
303
  globals = PyDict_New ();
304
  if (!globals)
305
    {
306
      Py_DECREF (code);
307
      return NULL;
308
    }
309
 
310
  tstate = PyThreadState_GET ();
311
 
312
  frame = PyFrame_New (tstate, code, globals, NULL);
313
 
314
  Py_DECREF (globals);
315
  Py_DECREF (code);
316
 
317
  if (!frame)
318
    return NULL;
319
 
320
  tstate->frame = frame;
321
  make_cleanup (py_restore_tstate, frame->f_back);
322
  return (PyObject *) frame;
323
}
324
 
325
/* Helper for apply_val_pretty_printer that formats children of the
326
   printer, if any exist.  */
327
static void
328
print_children (PyObject *printer, const char *hint,
329
                struct ui_file *stream, int recurse,
330
                const struct value_print_options *options,
331
                const struct language_defn *language)
332
{
333
  int is_map, is_array, done_flag, pretty;
334
  unsigned int i;
335
  PyObject *children, *iter, *frame;
336
  struct cleanup *cleanups;
337
 
338
  if (! PyObject_HasAttr (printer, gdbpy_children_cst))
339
    return;
340
 
341
  /* If we are printing a map or an array, we want some special
342
     formatting.  */
343
  is_map = hint && ! strcmp (hint, "map");
344
  is_array = hint && ! strcmp (hint, "array");
345
 
346
  children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
347
                                         NULL);
348
  if (! children)
349
    {
350
      gdbpy_print_stack ();
351
      return;
352
    }
353
 
354
  cleanups = make_cleanup_py_decref (children);
355
 
356
  iter = PyObject_GetIter (children);
357
  if (!iter)
358
    {
359
      gdbpy_print_stack ();
360
      goto done;
361
    }
362
  make_cleanup_py_decref (iter);
363
 
364
  /* Use the prettyprint_arrays option if we are printing an array,
365
     and the pretty option otherwise.  */
366
  pretty = is_array ? options->prettyprint_arrays : options->pretty;
367
 
368
  /* Manufacture a dummy Python frame to work around Python 2.4 bug,
369
     where it insists on having a non-NULL tstate->frame when
370
     a generator is called.  */
371
  frame = push_dummy_python_frame ();
372
  if (!frame)
373
    {
374
      gdbpy_print_stack ();
375
      goto done;
376
    }
377
  make_cleanup_py_decref (frame);
378
 
379
  done_flag = 0;
380
  for (i = 0; i < options->print_max; ++i)
381
    {
382
      PyObject *py_v, *item = PyIter_Next (iter);
383
      char *name;
384
      struct cleanup *inner_cleanup;
385
 
386
      if (! item)
387
        {
388
          if (PyErr_Occurred ())
389
            gdbpy_print_stack ();
390
          /* Set a flag so we can know whether we printed all the
391
             available elements.  */
392
          else
393
            done_flag = 1;
394
          break;
395
        }
396
 
397
      if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
398
        {
399
          gdbpy_print_stack ();
400
          Py_DECREF (item);
401
          continue;
402
        }
403
      inner_cleanup = make_cleanup_py_decref (item);
404
 
405
      /* Print initial "{".  For other elements, there are three
406
         cases:
407
         1. Maps.  Print a "," after each value element.
408
         2. Arrays.  Always print a ",".
409
         3. Other.  Always print a ",".  */
410
      if (i == 0)
411
        fputs_filtered (" = {", stream);
412
      else if (! is_map || i % 2 == 0)
413
        fputs_filtered (pretty ? "," : ", ", stream);
414
 
415
      /* In summary mode, we just want to print "= {...}" if there is
416
         a value.  */
417
      if (options->summary)
418
        {
419
          /* This increment tricks the post-loop logic to print what
420
             we want.  */
421
          ++i;
422
          /* Likewise.  */
423
          pretty = 0;
424
          break;
425
        }
426
 
427
      if (! is_map || i % 2 == 0)
428
        {
429
          if (pretty)
430
            {
431
              fputs_filtered ("\n", stream);
432
              print_spaces_filtered (2 + 2 * recurse, stream);
433
            }
434
          else
435
            wrap_here (n_spaces (2 + 2 *recurse));
436
        }
437
 
438
      if (is_map && i % 2 == 0)
439
        fputs_filtered ("[", stream);
440
      else if (is_array)
441
        {
442
          /* We print the index, not whatever the child method
443
             returned as the name.  */
444
          if (options->print_array_indexes)
445
            fprintf_filtered (stream, "[%d] = ", i);
446
        }
447
      else if (! is_map)
448
        {
449
          fputs_filtered (name, stream);
450
          fputs_filtered (" = ", stream);
451
        }
452
 
453
      if (gdbpy_is_lazy_string (py_v) || gdbpy_is_string (py_v))
454
        {
455
          gdb_byte *output = NULL;
456
 
457
          if (gdbpy_is_lazy_string (py_v))
458
            {
459
              struct type *type;
460
              long length;
461
              char *encoding = NULL;
462
 
463
              output = gdbpy_extract_lazy_string (py_v, &type,
464
                                                  &length, &encoding);
465
              if (!output)
466
                gdbpy_print_stack ();
467
              LA_PRINT_STRING (stream, type, output, length, encoding,
468
                               0, options);
469
              xfree (encoding);
470
              xfree (output);
471
            }
472
          else
473
            {
474
              output = python_string_to_host_string (py_v);
475
              fputs_filtered (output, stream);
476
              xfree (output);
477
            }
478
        }
479
      else
480
        {
481
          struct value *value = convert_value_from_python (py_v);
482
 
483
          if (value == NULL)
484
            {
485
              gdbpy_print_stack ();
486
              error (_("Error while executing Python code."));
487
            }
488
          else
489
            common_val_print (value, stream, recurse + 1, options, language);
490
        }
491
 
492
      if (is_map && i % 2 == 0)
493
        fputs_filtered ("] = ", stream);
494
 
495
      do_cleanups (inner_cleanup);
496
    }
497
 
498
  if (i)
499
    {
500
      if (!done_flag)
501
        {
502
          if (pretty)
503
            {
504
              fputs_filtered ("\n", stream);
505
              print_spaces_filtered (2 + 2 * recurse, stream);
506
            }
507
          fputs_filtered ("...", stream);
508
        }
509
      if (pretty)
510
        {
511
          fputs_filtered ("\n", stream);
512
          print_spaces_filtered (2 * recurse, stream);
513
        }
514
      fputs_filtered ("}", stream);
515
    }
516
 
517
 done:
518
  do_cleanups (cleanups);
519
}
520
 
521
int
522
apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
523
                          int embedded_offset, CORE_ADDR address,
524
                          struct ui_file *stream, int recurse,
525
                          const struct value_print_options *options,
526
                          const struct language_defn *language)
527
{
528
  struct gdbarch *gdbarch = get_type_arch (type);
529
  PyObject *printer = NULL;
530
  PyObject *val_obj = NULL;
531
  struct value *value;
532
  char *hint = NULL;
533
  struct cleanup *cleanups;
534
  int result = 0;
535
 
536
  cleanups = ensure_python_env (gdbarch, language);
537
 
538
  /* Instantiate the printer.  */
539
  if (valaddr)
540
    valaddr += embedded_offset;
541
  value = value_from_contents_and_address (type, valaddr,
542
                                           address + embedded_offset);
543
 
544
  val_obj = value_to_value_object (value);
545
  if (! val_obj)
546
    goto done;
547
 
548
  /* Find the constructor.  */
549
  printer = find_pretty_printer (val_obj);
550
  Py_DECREF (val_obj);
551
  make_cleanup_py_decref (printer);
552
  if (! printer || printer == Py_None)
553
    goto done;
554
 
555
  /* If we are printing a map, we want some special formatting.  */
556
  hint = gdbpy_get_display_hint (printer);
557
  make_cleanup (free_current_contents, &hint);
558
 
559
  /* Print the section */
560
  print_string_repr (printer, hint, stream, recurse, options, language,
561
                     gdbarch);
562
  print_children (printer, hint, stream, recurse, options, language);
563
  result = 1;
564
 
565
 
566
 done:
567
  if (PyErr_Occurred ())
568
    gdbpy_print_stack ();
569
  do_cleanups (cleanups);
570
  return result;
571
}
572
 
573
 
574
/* Apply a pretty-printer for the varobj code.  PRINTER_OBJ is the
575
   print object.  It must have a 'to_string' method (but this is
576
   checked by varobj, not here) which takes no arguments and
577
   returns a string.  The printer will return a value and in the case
578
   of a Python string being returned, this function will return a
579
   PyObject containing the string.  For any other type, *REPLACEMENT is
580
   set to the replacement value and this function returns NULL.  On
581
   error, *REPLACEMENT is set to NULL and this function also returns
582
   NULL.  */
583
PyObject *
584
apply_varobj_pretty_printer (PyObject *printer_obj,
585
                             struct value **replacement)
586
{
587
  int size = 0;
588
  PyObject *py_str = NULL;
589
 
590
  *replacement = NULL;
591
  py_str = pretty_print_one_value (printer_obj, replacement);
592
 
593
  if (*replacement == NULL && py_str == NULL)
594
    gdbpy_print_stack ();
595
 
596
  return py_str;
597
}
598
 
599
/* Find a pretty-printer object for the varobj module.  Returns a new
600
   reference to the object if successful; returns NULL if not.  VALUE
601
   is the value for which a printer tests to determine if it
602
   can pretty-print the value.  */
603
PyObject *
604
gdbpy_get_varobj_pretty_printer (struct value *value)
605
{
606
  PyObject *val_obj;
607
  PyObject *pretty_printer = NULL;
608
  volatile struct gdb_exception except;
609
 
610
  TRY_CATCH (except, RETURN_MASK_ALL)
611
    {
612
      value = value_copy (value);
613
    }
614
  GDB_PY_HANDLE_EXCEPTION (except);
615
 
616
  val_obj = value_to_value_object (value);
617
  if (! val_obj)
618
    return NULL;
619
 
620
  pretty_printer = find_pretty_printer (val_obj);
621
  Py_DECREF (val_obj);
622
  return pretty_printer;
623
}
624
 
625
/* A Python function which wraps find_pretty_printer and instantiates
626
   the resulting class.  This accepts a Value argument and returns a
627
   pretty printer instance, or None.  This function is useful as an
628
   argument to the MI command -var-set-visualizer.  */
629
PyObject *
630
gdbpy_default_visualizer (PyObject *self, PyObject *args)
631
{
632
  PyObject *val_obj;
633
  PyObject *cons, *printer = NULL;
634
  struct value *value;
635
 
636
  if (! PyArg_ParseTuple (args, "O", &val_obj))
637
    return NULL;
638
  value = value_object_to_value (val_obj);
639
  if (! value)
640
    {
641
      PyErr_SetString (PyExc_TypeError, "argument must be a gdb.Value");
642
      return NULL;
643
    }
644
 
645
  cons = find_pretty_printer (val_obj);
646
  return cons;
647
}
648
 
649
#else /* HAVE_PYTHON */
650
 
651
int
652
apply_val_pretty_printer (struct type *type, const gdb_byte *valaddr,
653
                          int embedded_offset, CORE_ADDR address,
654
                          struct ui_file *stream, int recurse,
655
                          const struct value_print_options *options,
656
                          const struct language_defn *language)
657
{
658
  return 0;
659
}
660
 
661
#endif /* HAVE_PYTHON */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.