Patchwork Remove MAX_REGISTER_SIZE from py-unwind.c

login
register
mail settings
Submitter Alan Hayward
Date June 12, 2017, 9:28 a.m.
Message ID <E4218BDE-2A92-490F-9443-27E25F4237C4@arm.com>
Download mbox | patch
Permalink /patch/20941/
State New
Headers show

Comments

Alan Hayward - June 12, 2017, 9:28 a.m.
I've moved cached_reg_t from remote.c to regcache.h,
then updated py-unwind.c to use cached_reg_t instead of reg_info.

Had to make sure registers are individually allocated/deallocated
(in the same way as remote.c).


Tested on a --enable-targets=all build.
All tests in gdb.python/python.exp pass.
Also tested with board files unix and native-gdbserver.

Ok to commit?

Alan.

2017-06-12  Alan Hayward  <alan.hayward@arm.com>

	* gdb/remote.c (cached_reg): Move from here...
	* gdb/regcache.h (cached_reg): ...to here.
	* gdb/python/py-unwind.c (struct reg_info): Remove.
	(cached_frame_info): Use cached_reg_t.
	(pyuw_prev_register): Likewise.
	(pyuw_sniffer): Use cached_reg_t and allocate registers.
	(pyuw_dealloc_cache): Free all registers.
Alan Hayward - June 21, 2017, 9:30 a.m.
PING

> On 12 Jun 2017, at 10:28, Alan Hayward <Alan.Hayward@arm.com> wrote:
> 
> I've moved cached_reg_t from remote.c to regcache.h,
> then updated py-unwind.c to use cached_reg_t instead of reg_info.
> 
> Had to make sure registers are individually allocated/deallocated
> (in the same way as remote.c).
> 
> 
> Tested on a --enable-targets=all build.
> All tests in gdb.python/python.exp pass.
> Also tested with board files unix and native-gdbserver.
> 
> Ok to commit?
> 
> Alan.
> 
> 2017-06-12  Alan Hayward  <alan.hayward@arm.com>
> 
> 	* gdb/remote.c (cached_reg): Move from here...
> 	* gdb/regcache.h (cached_reg): ...to here.
> 	* gdb/python/py-unwind.c (struct reg_info): Remove.
> 	(cached_frame_info): Use cached_reg_t.
> 	(pyuw_prev_register): Likewise.
> 	(pyuw_sniffer): Use cached_reg_t and allocate registers.
> 	(pyuw_dealloc_cache): Free all registers.
> 
> 
> 
> diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
> index a5c2873697944c1293fdd9f8265dde078378e6c2..91bd02ead7a56325b391a8f307e628d2293d57a2 100644
> --- a/gdb/python/py-unwind.c
> +++ b/gdb/python/py-unwind.c
> @@ -73,15 +73,6 @@ typedef struct
> /* The data we keep for a frame we can unwind: frame ID and an array of
>    (register_number, register_value) pairs.  */
> 
> -struct reg_info
> -{
> -  /* Register number.  */
> -  int number;
> -
> -  /* Register data bytes pointer.  */
> -  gdb_byte data[MAX_REGISTER_SIZE];
> -};
> -
> typedef struct
> {
>   /* Frame ID.  */
> @@ -93,7 +84,7 @@ typedef struct
>   /* Length of the `reg' array below.  */
>   int reg_count;
> 
> -  struct reg_info reg[];
> +  cached_reg_t reg[];
> } cached_frame_info;
> 
> extern PyTypeObject pending_frame_object_type
> @@ -483,14 +474,14 @@ pyuw_prev_register (struct frame_info *this_frame, void **cache_ptr,
>                     int regnum)
> {
>   cached_frame_info *cached_frame = (cached_frame_info *) *cache_ptr;
> -  struct reg_info *reg_info = cached_frame->reg;
> -  struct reg_info *reg_info_end = reg_info + cached_frame->reg_count;
> +  cached_reg_t *reg_info = cached_frame->reg;
> +  cached_reg_t *reg_info_end = reg_info + cached_frame->reg_count;
> 
>   TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__, this_frame,
>                    regnum);
>   for (; reg_info < reg_info_end; ++reg_info)
>     {
> -      if (regnum == reg_info->number)
> +      if (regnum == reg_info->num)
>         return frame_unwind_got_bytes (this_frame, regnum, reg_info->data);
>     }
> 
> @@ -567,9 +558,7 @@ pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
>     int i;
> 
>     cached_frame
> -      = ((cached_frame_info *)
> -	 xmalloc (sizeof (*cached_frame)
> -		  + reg_count * sizeof (cached_frame->reg[0])));
> +      = ((cached_frame_info *) xmalloc (sizeof (*cached_frame)));
>     cached_frame->gdbarch = gdbarch;
>     cached_frame->frame_id = unwind_info->frame_id;
>     cached_frame->reg_count = reg_count;
> @@ -580,13 +569,14 @@ pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
>         struct value *value = value_object_to_value (reg->value);
>         size_t data_size = register_size (gdbarch, reg->number);
> 
> -        cached_frame->reg[i].number = reg->number;
> +	cached_frame->reg[i].num = reg->number;
> 
>         /* `value' validation was done before, just assert.  */
>         gdb_assert (value != NULL);
>         gdb_assert (data_size == TYPE_LENGTH (value_type (value)));
>         gdb_assert (data_size <= MAX_REGISTER_SIZE);
> 
> +	cached_frame->reg[i].data = (gdb_byte *) xmalloc (data_size);
>         memcpy (cached_frame->reg[i].data, value_contents (value), data_size);
>       }
>   }
> @@ -601,6 +591,13 @@ static void
> pyuw_dealloc_cache (struct frame_info *this_frame, void *cache)
> {
>   TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__);
> +  cached_frame_info *cached_frame = (cached_frame_info *) cache;
> +
> +  for (int i = 0; cached_frame->reg_count; i++)
> +    {
> +      xfree (cached_frame->reg[i].data);
> +    }
> +
>   xfree (cache);
> }
> 
> diff --git a/gdb/regcache.h b/gdb/regcache.h
> index 24521e40ec0bb221d3fd37418d2aa485c08403b7..82026b49a57d64b9688dbce82bbf5c6529fdc651 100644
> --- a/gdb/regcache.h
> +++ b/gdb/regcache.h
> @@ -234,6 +234,14 @@ enum regcache_dump_what
>   regcache_dump_remote
> };
> 
> +/* A (register_number, register_value) pair.  */
> +
> +typedef struct cached_reg
> +{
> +  int num;
> +  gdb_byte *data;
> +} cached_reg_t;
> +
> /* The register cache for storing raw register values.  */
> 
> class regcache
> diff --git a/gdb/remote.c b/gdb/remote.c
> index 1f8607988444fb0221b87e9ef3e93fbec04c42ea..4a0660f7d91574b84ff2e3978e1795f7cb69ad93 100644
> --- a/gdb/remote.c
> +++ b/gdb/remote.c
> @@ -6308,12 +6308,6 @@ remote_console_output (char *msg)
>   gdb_flush (gdb_stdtarg);
> }
> 
> -typedef struct cached_reg
> -{
> -  int num;
> -  gdb_byte *data;
> -} cached_reg_t;
> -
> DEF_VEC_O(cached_reg_t);
> 
> typedef struct stop_reply
> 
>
Yao Qi - June 22, 2017, 9:08 a.m.
Alan Hayward <Alan.Hayward@arm.com> writes:

>      cached_frame
> -      = ((cached_frame_info *)
> -	 xmalloc (sizeof (*cached_frame)
> -		  + reg_count * sizeof (cached_frame->reg[0])));
> +      = ((cached_frame_info *) xmalloc (sizeof (*cached_frame)));

Use XNEW, the code can be shorter,

> @@ -601,6 +591,13 @@ static void
>  pyuw_dealloc_cache (struct frame_info *this_frame, void *cache)
>  {
>    TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__);
> +  cached_frame_info *cached_frame = (cached_frame_info *) cache;
> +
> +  for (int i = 0; cached_frame->reg_count; i++)
> +    {
> +      xfree (cached_frame->reg[i].data);
> +    }

No need to use braces.

Otherwise, patch is good to me.

Patch

diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index a5c2873697944c1293fdd9f8265dde078378e6c2..91bd02ead7a56325b391a8f307e628d2293d57a2 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -73,15 +73,6 @@  typedef struct
 /* The data we keep for a frame we can unwind: frame ID and an array of
    (register_number, register_value) pairs.  */

-struct reg_info
-{
-  /* Register number.  */
-  int number;
-
-  /* Register data bytes pointer.  */
-  gdb_byte data[MAX_REGISTER_SIZE];
-};
-
 typedef struct
 {
   /* Frame ID.  */
@@ -93,7 +84,7 @@  typedef struct
   /* Length of the `reg' array below.  */
   int reg_count;

-  struct reg_info reg[];
+  cached_reg_t reg[];
 } cached_frame_info;

 extern PyTypeObject pending_frame_object_type
@@ -483,14 +474,14 @@  pyuw_prev_register (struct frame_info *this_frame, void **cache_ptr,
                     int regnum)
 {
   cached_frame_info *cached_frame = (cached_frame_info *) *cache_ptr;
-  struct reg_info *reg_info = cached_frame->reg;
-  struct reg_info *reg_info_end = reg_info + cached_frame->reg_count;
+  cached_reg_t *reg_info = cached_frame->reg;
+  cached_reg_t *reg_info_end = reg_info + cached_frame->reg_count;

   TRACE_PY_UNWIND (1, "%s (frame=%p,...,reg=%d)\n", __FUNCTION__, this_frame,
                    regnum);
   for (; reg_info < reg_info_end; ++reg_info)
     {
-      if (regnum == reg_info->number)
+      if (regnum == reg_info->num)
         return frame_unwind_got_bytes (this_frame, regnum, reg_info->data);
     }

@@ -567,9 +558,7 @@  pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
     int i;

     cached_frame
-      = ((cached_frame_info *)
-	 xmalloc (sizeof (*cached_frame)
-		  + reg_count * sizeof (cached_frame->reg[0])));
+      = ((cached_frame_info *) xmalloc (sizeof (*cached_frame)));
     cached_frame->gdbarch = gdbarch;
     cached_frame->frame_id = unwind_info->frame_id;
     cached_frame->reg_count = reg_count;
@@ -580,13 +569,14 @@  pyuw_sniffer (const struct frame_unwind *self, struct frame_info *this_frame,
         struct value *value = value_object_to_value (reg->value);
         size_t data_size = register_size (gdbarch, reg->number);

-        cached_frame->reg[i].number = reg->number;
+	cached_frame->reg[i].num = reg->number;

         /* `value' validation was done before, just assert.  */
         gdb_assert (value != NULL);
         gdb_assert (data_size == TYPE_LENGTH (value_type (value)));
         gdb_assert (data_size <= MAX_REGISTER_SIZE);

+	cached_frame->reg[i].data = (gdb_byte *) xmalloc (data_size);
         memcpy (cached_frame->reg[i].data, value_contents (value), data_size);
       }
   }
@@ -601,6 +591,13 @@  static void
 pyuw_dealloc_cache (struct frame_info *this_frame, void *cache)
 {
   TRACE_PY_UNWIND (3, "%s: enter", __FUNCTION__);
+  cached_frame_info *cached_frame = (cached_frame_info *) cache;
+
+  for (int i = 0; cached_frame->reg_count; i++)
+    {
+      xfree (cached_frame->reg[i].data);
+    }
+
   xfree (cache);
 }

diff --git a/gdb/regcache.h b/gdb/regcache.h
index 24521e40ec0bb221d3fd37418d2aa485c08403b7..82026b49a57d64b9688dbce82bbf5c6529fdc651 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -234,6 +234,14 @@  enum regcache_dump_what
   regcache_dump_remote
 };

+/* A (register_number, register_value) pair.  */
+
+typedef struct cached_reg
+{
+  int num;
+  gdb_byte *data;
+} cached_reg_t;
+
 /* The register cache for storing raw register values.  */

 class regcache
diff --git a/gdb/remote.c b/gdb/remote.c
index 1f8607988444fb0221b87e9ef3e93fbec04c42ea..4a0660f7d91574b84ff2e3978e1795f7cb69ad93 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -6308,12 +6308,6 @@  remote_console_output (char *msg)
   gdb_flush (gdb_stdtarg);
 }

-typedef struct cached_reg
-{
-  int num;
-  gdb_byte *data;
-} cached_reg_t;
-
 DEF_VEC_O(cached_reg_t);

 typedef struct stop_reply