This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [python][patch] Preserve string length, and embedded-nulls in pretty-print string emission.


Phil> +      PyObject *string = python_string_to_target_python_string (py_str);
Phil> +      if (string)
Phil> +	{
Phil> +	  gdb_byte *output = PyString_AsString (string);
Phil> +	  int len = PyString_Size (string) + 1;
Phil> +
Phil> +	  if (hint && !strcmp (hint, "string"))
Phil> +	    LA_PRINT_STRING (stream, output,
Phil> +			     len, 1, 0, options);
Phil> +	  else
Phil> +	    fputs_filtered (output, stream);
Phil> +	  Py_DECREF (string);
Phil> +	}

This needs an 'else' case to call gdbpy_print_stack.

Also, why the '+ 1' in the computation of 'len'?
Won't this make LA_PRINT_STRING print one too many bytes?

Phil> +PyObject *
Phil>  apply_varobj_pretty_printer (PyObject *printer_obj,
Phil>  			     struct value **replacement)
Phil>  {
Phil> -  char *result;
Phil> +  int size = 0;
Phil>    PyGILState_STATE state = PyGILState_Ensure ();
Phil> +  PyObject *py_str = NULL;
 
Phil>    *replacement = NULL;
Phil> -  result = pretty_print_one_value (printer_obj, replacement);
Phil> -  if (result == NULL);
Phil> +  py_str  = pretty_print_one_value (printer_obj, replacement);
Phil> +
Phil> +  if (replacement == NULL && py_str == NULL)
Phil>      gdbpy_print_stack ();
Phil>    PyGILState_Release (state);
 
Phil> -  return result;
Phil> +  return py_str;
Phil>  }
 
Phil>  /* Find a pretty-printer object for the varobj module.  Returns a new
Phil> diff --git a/gdb/testsuite/gdb.python/python-prettyprint.c b/gdb/testsuite/gdb.python/python-prettyprint.c
Phil> index 0d9110d..5cc35be 100644
Phil> --- a/gdb/testsuite/gdb.python/python-prettyprint.c
Phil> +++ b/gdb/testsuite/gdb.python/python-prettyprint.c
Phil> @@ -29,6 +29,11 @@ struct ss
Phil>    struct s b;
Phil>  };
 
Phil> +struct ns {
Phil> +  const char *null_str;
Phil> +  int length;
Phil> +};
Phil> +
Phil>  #ifdef __cplusplus
Phil>  struct S : public s {
Phil>    int zs;
Phil> @@ -166,6 +171,10 @@ main ()
Phil>    init_ss(ssa+1, 5, 6);
Phil>    memset (&nullstr, 0, sizeof nullstr);
 
Phil> +  struct ns  ns;
Phil> +  ns.null_str = "embedded\0null\0string";
Phil> +  ns.length = 20;
Phil> +
Phil>  #ifdef __cplusplus
Phil>    S cps;
 
Phil> diff --git a/gdb/testsuite/gdb.python/python-prettyprint.exp b/gdb/testsuite/gdb.python/python-prettyprint.exp
Phil> index 47d2fa8..b2dc85d 100644
Phil> --- a/gdb/testsuite/gdb.python/python-prettyprint.exp
Phil> +++ b/gdb/testsuite/gdb.python/python-prettyprint.exp
Phil> @@ -75,6 +75,7 @@ proc run_lang_tests {lang} {
Phil>  	gdb_test "print ref" "= a=<15> b=< a=<8> b=<$hex>>"
Phil>  	gdb_test "print derived" \
Phil>  	    " = \{.*<Vbase1> = pp class name: Vbase1.*<Vbase2> = \{.*<VirtualTest> = pp value variable is: 1,.*members of Vbase2:.*_vptr.Vbase2 = $hex.*<Vbase3> = \{.*members of Vbase3.*members of Derived:.*value = 2.*"
Phil> +	gdb_test "print ns " "\"embedded\\\\000null\\\\000string\""
Phil>      }
 
Phil>      gdb_test "print x" " = $hex \"this is x\""
Phil> diff --git a/gdb/testsuite/gdb.python/python-prettyprint.py b/gdb/testsuite/gdb.python/python-prettyprint.py
Phil> index 82e5331..c3e0dc4 100644
Phil> --- a/gdb/testsuite/gdb.python/python-prettyprint.py
Phil> +++ b/gdb/testsuite/gdb.python/python-prettyprint.py
Phil> @@ -99,6 +99,19 @@ class pp_nullstr:
Phil>      def to_string(self):
Phil>          return self.val['s'].string(gdb.parameter('target-charset'))
 
Phil> +class pp_ns:
Phil> +    "Print a std::basic_string of some kind"
Phil> +
Phil> +    def __init__(self, val):
Phil> +        self.val = val
Phil> +
Phil> +    def to_string(self):
Phil> +        len = self.val['length']
Phil> +        return self.val['null_str'].string (gdb.parameter ('target-charset'), length = len)
Phil> +
Phil> +    def display_hint (self):
Phil> +        return 'string'
Phil> +
Phil>  def lookup_function (val):
Phil>      "Look-up and return a pretty-printer that can print val."
 
Phil> @@ -155,6 +168,8 @@ def register_pretty_printers ():
Phil>      pretty_printers_dict[re.compile ('^string_repr$')] = string_print
Phil>      pretty_printers_dict[re.compile ('^container$')] = ContainerPrinter
     
Phil> +    pretty_printers_dict[re.compile ('^struct ns$')]  = pp_ns
Phil> +    pretty_printers_dict[re.compile ('^ns$')]  = pp_ns
Phil>  pretty_printers_dict = {}
 
Phil>  register_pretty_printers ()
Phil> diff --git a/gdb/varobj.c b/gdb/varobj.c
Phil> index 49b4a43..75f3f17 100644
Phil> --- a/gdb/varobj.c
Phil> +++ b/gdb/varobj.c
Phil> @@ -2250,8 +2250,9 @@ value_get_print_value (struct value *value, enum varobj_display_formats format,
Phil>    long dummy;
Phil>    struct ui_file *stb;
Phil>    struct cleanup *old_chain;
Phil> -  char *thevalue = NULL;
Phil> +  gdb_byte *thevalue = NULL;
Phil>    struct value_print_options opts;
Phil> +  int len = 0;
 
Phil>    if (value == NULL)
Phil>      return NULL;
Phil> @@ -2265,6 +2266,7 @@ value_get_print_value (struct value *value, enum varobj_display_formats format,
Phil>  	char *hint;
Phil>  	struct value *replacement;
Phil>  	int string_print = 0;
Phil> +	PyObject *output = NULL;
 
Phil>  	hint = gdbpy_get_display_hint (value_formatter);
Phil>  	if (hint)
Phil> @@ -2274,15 +2276,27 @@ value_get_print_value (struct value *value, enum varobj_display_formats format,
Phil>  	    xfree (hint);
Phil>  	  }
 
Phil> -	thevalue = apply_varobj_pretty_printer (value_formatter,
Phil> -						&replacement);
Phil> -	if (thevalue && !string_print)
Phil> +	output = apply_varobj_pretty_printer (value_formatter, &replacement);
Phil> +
Phil> +	if (output)
Phil>  	  {
Phil> -	    PyGILState_Release (state);
Phil> -	    return thevalue;
Phil> +	    PyObject *py_str = python_string_to_target_python_string (output);
Phil> +	    if (py_str)
Phil> +	      {
Phil> +		char *s = PyString_AsString (py_str);
Phil> +		len = PyString_Size (py_str) + 1;
Phil> +		thevalue = xmemdup (s, len, len);
Phil> +		Py_DECREF (py_str);
Phil> +	      }
Phil> +	    Py_DECREF (output);
Phil>  	  }
Phil>  	if (replacement)
Phil>  	  value = replacement;
Phil> +	if (thevalue && !string_print)
Phil> +         {
Phil> +           PyGILState_Release (state);
Phil> +           return thevalue;
Phil> +         }
Phil>        }
Phil>      PyGILState_Release (state);
Phil>    }
Phil> @@ -2297,8 +2311,7 @@ value_get_print_value (struct value *value, enum varobj_display_formats format,
Phil>    if (thevalue)
Phil>      {
Phil>        make_cleanup (xfree, thevalue);
Phil> -      LA_PRINT_STRING (stb, (gdb_byte *) thevalue, strlen (thevalue),
Phil> -		       1, 0, &opts);
Phil> +      LA_PRINT_STRING (stb, thevalue, len, 1, 0, &opts);
Phil>      }
Phil>    else
Phil>      common_val_print (value, stb, 0, &opts, current_language);

Tom


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]