public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA v2 4/6] Expose type alignment on gdb.Type
Date: Fri, 27 Apr 2018 14:01:00 -0000	[thread overview]
Message-ID: <20180427140139.7957-5-tom@tromey.com> (raw)
In-Reply-To: <20180427140139.7957-1-tom@tromey.com>

This adds an "align" attribute to gdb.Type in the Python API.

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

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

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

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

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

	* gdb.python/py-type.exp: Check align attribute.
	* gdb.python/py-type.c: New "aligncheck" global.
---
 gdb/ChangeLog                        |  6 ++++++
 gdb/NEWS                             |  4 ++++
 gdb/doc/ChangeLog                    |  4 ++++
 gdb/doc/python.texi                  |  7 +++++++
 gdb/python/py-type.c                 | 24 ++++++++++++++++++++++++
 gdb/testsuite/ChangeLog              |  5 +++++
 gdb/testsuite/gdb.python/py-type.c   |  2 ++
 gdb/testsuite/gdb.python/py-type.exp |  4 ++++
 8 files changed, 56 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index caab3a7284..4e1f52dbdf 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,9 @@
+2018-04-27  Tom Tromey  <tom@tromey.com>
+
+	* NEWS: Mention Type.align.
+	* python/py-type.c (typy_get_align): New function.
+	(type_object_getset): Add "align".
+
 2018-04-27  Tom Tromey  <tom@tromey.com>
 
 	* python/py-type.c (type_object_getset): Reindent.
diff --git a/gdb/NEWS b/gdb/NEWS
index 6631b53475..6193070023 100644
--- 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
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 83d48781f9..4e7c96bfd4 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,7 @@
+2018-04-27  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
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index ebd48fffe7..e5fd713e0a 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -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.align
+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.
diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c
index fcd6ed5621..bc49ec66ac 100644
--- a/gdb/python/py-type.c
+++ b/gdb/python/py-type.c
@@ -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_align (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[] =
 {
+ { "align", typy_get_align, 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,
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 6909e8e0b1..40e5bff571 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2018-04-27  Tom Tromey  <tom@tromey.com>
+
+	* gdb.python/py-type.exp: Check align attribute.
+	* gdb.python/py-type.c: New "aligncheck" global.
+
 2018-04-27  Tom Tromey  <tom@tromey.com>
 
 	PR exp/17095:
diff --git a/gdb/testsuite/gdb.python/py-type.c b/gdb/testsuite/gdb.python/py-type.c
index 2626d4e418..9531c9e6cb 100644
--- a/gdb/testsuite/gdb.python/py-type.c
+++ b/gdb/testsuite/gdb.python/py-type.c
@@ -30,6 +30,8 @@ struct SS
 typedef struct s TS;
 TS ts;
 
+int aligncheck;
+
 #ifdef __cplusplus
 struct C
 {
diff --git a/gdb/testsuite/gdb.python/py-type.exp b/gdb/testsuite/gdb.python/py-type.exp
index b87e86cdf6..382bdff118 100644
--- a/gdb/testsuite/gdb.python/py-type.exp
+++ b/gdb/testsuite/gdb.python/py-type.exp
@@ -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.align)" \
+      $sint
+
   with_test_prefix "lang_c" {
       runto_bp "break to inspect struct and array."
       test_fields "c"
-- 
2.14.3

  parent reply	other threads:[~2018-04-27 14:01 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-27 14:01 [RFA v2 0/6] Type alignment Tom Tromey
2018-04-27 14:01 ` [RFA v2 6/6] Remove long_long_align_bit gdbarch attribute Tom Tromey
2018-04-27 18:47   ` Pedro Alves
2018-04-27 14:01 ` [RFA v2 3/6] Reindent type_object_getset in py-type.c Tom Tromey
2018-04-27 18:47   ` Pedro Alves
2018-04-27 20:39     ` Tom Tromey
2018-04-27 14:01 ` [RFA v2 2/6] Handle alignof and _Alignof Tom Tromey
2018-04-27 14:19   ` Eli Zaretskii
2018-04-27 18:47   ` Pedro Alves
2018-04-27 20:55     ` Tom Tromey
2018-04-27 20:59       ` Tom Tromey
2018-04-30 16:50     ` Tom Tromey
2018-04-30 17:17       ` Pedro Alves
2018-04-27 14:01 ` [RFA v2 1/6] Add initial type alignment support Tom Tromey
2018-04-27 18:46   ` Pedro Alves
2018-04-27 14:01 ` Tom Tromey [this message]
2018-04-27 14:18   ` [RFA v2 4/6] Expose type alignment on gdb.Type Eli Zaretskii
2018-04-27 18:47   ` Pedro Alves
2018-04-27 20:41     ` Tom Tromey
2018-04-27 14:01 ` [RFA v2 5/6] Remove rust_type_alignment Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180427140139.7957-5-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).