]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Expose type alignment on gdb.Type
authorTom Tromey <tom@tromey.com>
Fri, 20 Apr 2018 21:43:56 +0000 (15:43 -0600)
committerTom Tromey <tom@tromey.com>
Mon, 30 Apr 2018 17:25:31 +0000 (11:25 -0600)
This adds an "alignof" attribute to gdb.Type in the Python API.

2018-04-30  Tom Tromey  <tom@tromey.com>

* NEWS: Mention Type.align.
* python/py-type.c (typy_get_alignof): New function.
(type_object_getset): Add "alignof".

2018-04-30  Tom Tromey  <tom@tromey.com>

* python.texi (Types In Python): Document Type.align.

2018-04-30  Tom Tromey  <tom@tromey.com>

* gdb.python/py-type.exp: Check align attribute.
* gdb.python/py-type.c: New "aligncheck" global.

gdb/ChangeLog
gdb/NEWS
gdb/doc/ChangeLog
gdb/doc/python.texi
gdb/python/py-type.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-type.c
gdb/testsuite/gdb.python/py-type.exp

index 27bf1dd8a11ecdc3bc95e426cf031c76fefe9834..0053b901af216ab99ab3ba280cf1b0d83315b43d 100644 (file)
@@ -1,3 +1,9 @@
+2018-04-30  Tom Tromey  <tom@tromey.com>
+
+       * NEWS: Mention Type.align.
+       * python/py-type.c (typy_get_alignof): New function.
+       (type_object_getset): Add "alignof".
+
 2018-04-30  Tom Tromey  <tom@tromey.com>
 
        PR exp/17095:
index 6631b534756f27c12cf2d91ecb53d3e35fc9d896..61930700231f4956ad12164fd82f5034d4c86c45 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -27,6 +27,10 @@ set|show record btrace cpu
   Controls the processor to be used for enabling errata workarounds for
   branch trace decode.
 
+* Python API
+
+  ** Type alignment is now exposed via the "align" attribute of a gdb.Type.
+
 * New targets
 
 RiscV ELF                      riscv*-*-elf
index 83d48781f904bebf0d13c68ad0745ae865874c43..2da4984ff9bd65bc9f18140ae4ad7207983e9389 100644 (file)
@@ -1,3 +1,7 @@
+2018-04-30  Tom Tromey  <tom@tromey.com>
+
+       * python.texi (Types In Python): Document Type.align.
+
 2018-04-13  Andreas Arnez  <arnez@linux.vnet.ibm.com>
 
        * gdb.texinfo (Symbols): Mention the fact that "info
index ebd48fffe7de7cb660ea5f0695f39704cd3bbd10..05703fbde6cb8fe1436851ab3abab0b9a0593a73 100644 (file)
@@ -930,6 +930,13 @@ description of the @code{Type.fields} method for a description of the
 
 An instance of @code{Type} has the following attributes:
 
+@defvar Type.alignof
+The alignment of this type, in bytes.  Type alignment comes from the
+debugging information; if it was not specified, then @value{GDBN} will
+use the relevant ABI to try to determine the alignment.  In some
+cases, even this is not possible, and zero will be returned.
+@end defvar
+
 @defvar Type.code
 The type code for this type.  The type code will be one of the
 @code{TYPE_CODE_} constants defined below.
index 8a948eeaa648e6bc0ba2ab76b1a652f53334943f..cbdcd21156ccc9ef963a4af3262f602975e1842c 100644 (file)
@@ -730,6 +730,28 @@ typy_get_sizeof (PyObject *self, void *closure)
   return gdb_py_long_from_longest (TYPE_LENGTH (type));
 }
 
+/* Return the alignment of the type represented by SELF, in bytes.  */
+static PyObject *
+typy_get_alignof (PyObject *self, void *closure)
+{
+  struct type *type = ((type_object *) self)->type;
+
+  ULONGEST align = 0;
+  TRY
+    {
+      align = type_align (type);
+    }
+  CATCH (except, RETURN_MASK_ALL)
+    {
+      align = 0;
+    }
+  END_CATCH
+
+  /* Ignore exceptions.  */
+
+  return gdb_py_object_from_ulongest (align);
+}
+
 static struct type *
 typy_lookup_typename (const char *type_name, const struct block *block)
 {
@@ -1410,6 +1432,8 @@ gdbpy_initialize_types (void)
 
 static gdb_PyGetSetDef type_object_getset[] =
 {
+  { "alignof", typy_get_alignof, NULL,
+    "The alignment of this type, in bytes.", NULL },
   { "code", typy_get_code, NULL,
     "The code for this type.", NULL },
   { "name", typy_get_name, NULL,
index e8b55aac9050f4910d5213b4cfd8f9d249c723fa..4e48934cbf0429e3f1b7f7f7a18cbc56fbd98df2 100644 (file)
@@ -1,3 +1,8 @@
+2018-04-30  Tom Tromey  <tom@tromey.com>
+
+       * gdb.python/py-type.exp: Check align attribute.
+       * gdb.python/py-type.c: New "aligncheck" global.
+
 2018-04-30  Tom Tromey  <tom@tromey.com>
 
        PR exp/17095:
index 2626d4e4184a492e27a28e6500787821c60d48e1..9531c9e6cb52089ea1a6d625062cdf64a90d248c 100644 (file)
@@ -30,6 +30,8 @@ struct SS
 typedef struct s TS;
 TS ts;
 
+int aligncheck;
+
 #ifdef __cplusplus
 struct C
 {
index b87e86cdf6a3470d8c86c03757edb56d065c12c2..8fe02216c747e74a4cd9867ae9ee4bb4edac6da2 100644 (file)
@@ -278,6 +278,10 @@ if { [build_inferior "${binfile}" "c"] == 0 } {
   gdb_test "python print(gdb.lookup_type('int').optimized_out())" \
       "<optimized out>"
 
+  set sint [get_sizeof int 0]
+  gdb_test "python print(gdb.parse_and_eval('aligncheck').type.alignof)" \
+      $sint
+
   with_test_prefix "lang_c" {
       runto_bp "break to inspect struct and array."
       test_fields "c"