Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Yao Qi <qiyaoltc@gmail.com>,
	binutils@sourceware.org, gdb-patches@sourceware.org
Subject: Re: [PATCH 3/8] Disassembly unit test: disassemble one instruction
Date: Thu, 12 Jan 2017 14:35:00 -0000	[thread overview]
Message-ID: <1be65603-63b3-5273-973a-84b6e6006798@redhat.com> (raw)
In-Reply-To: <1484051178-16013-4-git-send-email-yao.qi@linaro.org>

On 01/10/2017 12:26 PM, Yao Qi wrote:
> +static void
> +gdb_disassembler_print_one_insn_test (struct gdbarch *gdbarch)
> +{
> +  int len = -1;
> +  const gdb_byte *insn = NULL;
> +
> +  switch (gdbarch_bfd_arch_info (gdbarch)->arch)
> +    {
> +    case bfd_arch_bfin:
> +      /* M3.L = 0xe117 */
> +      insn = (const gdb_byte[]) {0x17, 0xe1, 0xff, 0xff};
> +      len = 4;
> +      break;

This is construct is problematic.

Unfortunately, compound literals aren't valid C++.  They're
valid in C, but not in C++.  G++ accepts them as an extension.

Maybe all C++ compilers we care about support them too, so that's
not the real problem I'm pointing at.

The problem is that compound literal above may have automatic
storage duration, and thus die at the end of the switch scope.

From http://en.cppreference.com/w/c/language/compound_literal

 "The unnamed object to which the compound literal evaluates has static
 storage duration if the compound literal occurs at file scope and automatic
 storage duration if the compound literal occurs at block scope (in which
 case the object's lifetime ends at the end of the enclosing block)."

I didn't check the C standard, but I assume that's correct.

At <https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html>,
we see:

"As an optimization, G++ sometimes gives array compound literals
 longer lifetimes: when the array either appears outside a function
 or has a const-qualified type. If foo and its initializer had elements
 of type char *const rather than char *, or if foo were a global
 variable, the array would have static storage duration. But it is probably
 safest just to avoid the use of array compound literals in C++ code. "

Given all the warnings, I'd think it best to avoid the construct.
We can just name the arrays:

static void
gdb_disassembler_print_one_insn_test (struct gdbarch *gdbarch)
{
  int len = -1;
  const gdb_byte *insn = NULL;

  switch (gdbarch_bfd_arch_info (gdbarch)->arch)
    {
    case bfd_arch_bfin:
      /* M3.L = 0xe117 */
      static const gdb_byte bfin_insn[] = { 0x17, 0xe1, 0xff, 0xff };
      insn = bfin_insn;
      len = 4;
      break;
    case bfd_arch_bfin:
      /* mov     r0, #0 */
      static const gdb_byte arm_insn[] = { 0x0, 0x0, 0xa0, 0xe3 };
      insn = arm_insn;
      len = 4;


Or maybe make the compiler do the "sizeof" for us,
maybe eliminating copy/paste mistakes:

struct test_insn
 {
   template<size_t SIZE>
   explicit test_insn (const gdb_byte (&insn_)[Len])
    : insn (insn_), len_ (Len)
   {}
   gdb_byte *insn;
   int len;
 };

static void
gdb_disassembler_print_one_insn_test (struct gdbarch *gdbarch)
{
  test_insn insn = { NULL, -1 };

  switch (gdbarch_bfd_arch_info (gdbarch)->arch)
    {
    case bfd_arch_bfin:
      /* M3.L = 0xe117 */
      static const gdb_byte bfin_insn[] = { 0x17, 0xe1, 0xff, 0xff };
      insn = test_insn (bfin_insn);
      break;
    case bfd_arch_bfin:
      /* mov     r0, #0 */
      static const gdb_byte arm_insn[] = { 0x0, 0x0, 0xa0, 0xe3 };
      insn = test_insn (arm_insn);
  [....]


Maybe split the insns out of the switch:

/* M3.L = 0xe117 */
static const gdb_byte bfin_test_insn[] = { 0x17, 0xe1, 0xff, 0xff };
/* mov     r0, #0 */
static const gdb_byte arm_test_insn[] = { 0x0, 0x0, 0xa0, 0xe3 };

static void
gdb_disassembler_print_one_insn_test (struct gdbarch *gdbarch)
{
  test_insn insn = { NULL, -1 };

  switch (gdbarch_bfd_arch_info (gdbarch)->arch)
    {
    case bfd_arch_bfin:
      insn = test_insn (bfin_test_insn);
      break;
    case bfd_arch_bfin:
      insn = test_insn (arm_test_insn);
      break;
  [....]


Just some ideas.

Thanks,
Pedro Alves


  parent reply	other threads:[~2017-01-12 14:35 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-10 12:26 [PATCH 0/8] Handle memory error on disassemble Yao Qi
2017-01-10 12:26 ` [PATCH 3/8] Disassembly unit test: disassemble one instruction Yao Qi
2017-01-11 21:15   ` Simon Marchi
2017-01-12 13:06   ` Pedro Alves
2017-01-12 17:03     ` Yao Qi
2017-01-12 17:43       ` Pedro Alves
2017-01-12 21:04         ` Yao Qi
2017-01-12 14:35   ` Pedro Alves [this message]
2017-01-12 15:15   ` Pedro Alves
2017-01-12 15:35     ` Yao Qi
2017-01-12 15:44       ` Pedro Alves
2017-01-12 16:06     ` Pedro Alves
2017-01-10 12:26 ` [PATCH 4/8] Return -1 on memory error in print_insn_msp430 Yao Qi
2017-01-11 21:54   ` Alan Modra
2017-01-12  9:43     ` Yao Qi
2017-01-10 12:26 ` [PATCH 6/8] Return -1 on memory error in print_insn_m68k Yao Qi
2017-01-11 22:15   ` Alan Modra
2017-01-12 11:50     ` Yao Qi
2017-01-12 14:38       ` Alan Modra
2017-01-12 14:52         ` Yao Qi
2017-01-13  1:54           ` Alan Modra
2017-01-13 12:29             ` Yao Qi
2017-01-10 12:26 ` [PATCH 5/8] Remove magic numbers in m68k-dis.c:print_insn_arg Yao Qi
2017-01-11 22:14   ` Alan Modra
2017-01-13 12:23     ` Yao Qi
2017-01-10 12:26 ` [PATCH 7/8] Disassembly unit test: memory error Yao Qi
2017-01-10 12:27 ` [PATCH 1/8] Refactor disassembly code Yao Qi
2017-01-11 20:43   ` Simon Marchi
2017-01-12 12:19     ` Yao Qi
2017-01-12 12:36       ` Pedro Alves
2017-01-12 15:29         ` Simon Marchi
2017-01-10 12:27 ` [PATCH 8/8] Don't throw exception in dis_asm_memory_error Yao Qi
2017-01-12 16:40   ` Pedro Alves
2017-01-12 21:09     ` Yao Qi
2017-01-10 12:27 ` [PATCH 2/8] Call print_insn_mep in mep_gdb_print_insn Yao Qi
2017-01-11 20:50   ` Simon Marchi
2017-01-12 12:21     ` Yao Qi
2017-01-16 10:03 ` [PATCH 0/6 v2] Handle memory error on disassemble Yao Qi
2017-01-16 10:03   ` [PATCH 4/6] Disassembly unit test: disassemble one instruction Yao Qi
2017-01-20  0:04     ` Pedro Alves
2017-01-24 15:23       ` Yao Qi
2017-02-02 16:46         ` Pedro Alves
2017-02-02 22:12           ` Yao Qi
2017-02-02 23:39             ` [pushed] Fix "maintenance selftest" printing stray instructions (Re: [PATCH 4/6] Disassembly unit test: disassemble one instruction) Pedro Alves
2017-01-16 10:03   ` [PATCH 1/6] New function null_stream Yao Qi
2017-01-17 13:49     ` Luis Machado
2017-01-18 14:45       ` Yao Qi
2017-01-18 14:53         ` Luis Machado
2017-01-18 14:57           ` Simon Marchi
2017-01-18 15:02             ` Luis Machado
2017-01-18 15:18               ` Simon Marchi
2017-01-18 15:29                 ` Luis Machado
2017-01-18 15:54                   ` Simon Marchi
2017-01-18 16:36                     ` Luis Machado
2017-01-16 10:03   ` [PATCH 5/6] Disassembly unit test: memory error Yao Qi
2017-01-17 14:38     ` Luis Machado
2017-01-24 15:33       ` Yao Qi
2017-01-20  0:08     ` Pedro Alves
2017-01-16 10:03   ` [PATCH 2/6] Refactor disassembly code Yao Qi
2017-01-17 14:14     ` Luis Machado
2017-01-18 16:34       ` Yao Qi
2017-01-18 16:53         ` Luis Machado
2017-01-16 10:03   ` [PATCH 3/6] Call print_insn_mep in mep_gdb_print_insn Yao Qi
2017-01-17 14:19     ` Luis Machado
2017-01-24 10:08       ` Yao Qi
2017-01-24 13:41         ` Luis Machado
2017-01-16 10:03   ` [PATCH 6/6] Don't throw exception in dis_asm_memory_error Yao Qi
2017-01-17 14:42     ` Luis Machado
2017-01-18 14:54       ` Yao Qi
2017-01-18 14:58         ` Luis Machado
2017-01-25  8:38   ` [PATCH 0/6 v3] Handle memory error on disassembly Yao Qi
2017-01-25  8:38     ` [PATCH 1/6] New function null_stream Yao Qi
2017-01-25  8:38     ` [PATCH 5/6] Disassembly unit test: memory error Yao Qi
2017-01-25  8:38     ` [PATCH 4/6] Disassembly unit test: disassemble one instruction Yao Qi
2017-01-25  8:38     ` [PATCH 3/6] Call print_insn_mep in mep_gdb_print_insn Yao Qi
2017-01-25  8:38     ` [PATCH 6/6] Don't throw exception in dis_asm_memory_error Yao Qi
2017-01-25  8:38     ` [PATCH 2/6] Refactor disassembly code Yao Qi
2017-01-26 11:34     ` [PATCH 0/6 v3] Handle memory error on disassembly Pedro Alves
2017-01-26 15:00       ` Yao Qi

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=1be65603-63b3-5273-973a-84b6e6006798@redhat.com \
    --to=palves@redhat.com \
    --cc=binutils@sourceware.org \
    --cc=gdb-patches@sourceware.org \
    --cc=qiyaoltc@gmail.com \
    /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