Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: Yao Qi <qiyaoltc@gmail.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 4/6] Disassembly unit test: disassemble one instruction
Date: Fri, 20 Jan 2017 00:04:00 -0000	[thread overview]
Message-ID: <620db0ef-5e3d-3b93-5596-33d24a78f6a9@redhat.com> (raw)
In-Reply-To: <1484560977-8693-5-git-send-email-yao.qi@linaro.org>

On 01/16/2017 10:02 AM, Yao Qi wrote:

> +static void
> +gdb_disassembler_print_one_insn_test (struct gdbarch *gdbarch)
> +{
> +  size_t len = 0;
> +  const gdb_byte *insn = NULL;
> +

> +      insn = gdbarch_sw_breakpoint_from_kind (gdbarch, 4, (int *) &len);

That "(int *) &len" is invalid code.  It's an aliasing violation.
And even if that weren't a problem, consider what happens when
sizeof size_t != sizeof int, on big endian and little endian.

Use a temporary variable of the right type, like e.g.:

      int bplen;
      insn = gdbarch_sw_breakpoint_from_kind (gdbarch, 4, &bplen);
      len = bplen;

> +      break;
> +    default:
> +      {
> +	/* Test disassemble breakpoint instruction.  */
> +	CORE_ADDR pc = 0;
> +	int kind = gdbarch_breakpoint_kind_from_pc (gdbarch, &pc);
> +
> +	insn = gdbarch_sw_breakpoint_from_kind (gdbarch, kind,
> +						(int *) &len);

Ditto.


> +      len = sizeof (xstormy16_insn);
> +      break;
> +    case bfd_arch_arc:
> +      {
> +	/* PR 21003 */
> +	if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_arc_arc601)
> +	  return;
> +      }

Odd that this case got braces when it doesn't declare any variable,
and when other cases don't.  Also, is the fallthrough intended?
If so, add a comment otherwise we may get a warning with GCC 7.

> +    case bfd_arch_nios2:
> +    case bfd_arch_score:
> +      /* nios2 and score need to know the current instruction to select
> +	 breakpoint instruction.  Give the breakpoint instruction kind
> +	 explicitly.  */
> +      insn = gdbarch_sw_breakpoint_from_kind (gdbarch, 4, (int *) &len);
> +      break;
> +    default:


> +
> +	break;
> +      }
> +    }
> +  SELF_CHECK (len > 0);
> +
> +  /* Test gdb_disassembler for a given gdbarch by reading data from a
> +     pre-allocated buffer.  If you want to see the disassembled
> +     instruction printed to gdb_stdout, set DISASSEMBLER_TEST_VERBOSE
> +     to true.  */
> +
> +  class gdb_disassembler_test : public gdb_disassembler
> +  {
> +  public:
> +
> +    const bool DISASSEMBLER_TEST_VERBOSE = false;

static.  We give macros long unique names in order to
avoid naming conflicts, but if this is no longer a macro,
the name could be shortened, to e.g., just:

 static const bool verbose = false;

> +
> +    explicit gdb_disassembler_test (struct gdbarch *gdbarch,
> +				    const gdb_byte *insn,
> +				    size_t len)
> +      : gdb_disassembler (gdbarch,
> +			  (DISASSEMBLER_TEST_VERBOSE
> +			   ? gdb_stdout : null_stream ()),
> +			  gdb_disassembler_test::read_memory),
> +	m_insn (insn), m_len (len)
> +    {
> +    }
> +
> +    int
> +    print_insn (CORE_ADDR memaddr)
> +    {
> +      if (DISASSEMBLER_TEST_VERBOSE)
> +	{
> +	  fprintf_unfiltered (stream (), "%s ",
> +			      gdbarch_bfd_arch_info (arch ())->arch_name);
> +	}
> +
> +      int len = gdb_disassembler::print_insn (memaddr);
> +
> +      if (DISASSEMBLER_TEST_VERBOSE)
> +	fprintf_unfiltered (stream (), "\n");
> +
> +      return len;
> +    }
> +
> +  private:
> +    /* A buffer contain one instruction.  */
> +    const gdb_byte *m_insn;
> +
> +    /* Length of the buffer.  */
> +    size_t m_len;
> +
> +    static int read_memory (bfd_vma memaddr, gdb_byte *myaddr,
> +			    unsigned int len, struct disassemble_info *info)
> +    {
> +      gdb_disassembler_test *self
> +	= static_cast<gdb_disassembler_test *>(info->application_data);
> +
> +      /* The disassembler in opcodes may read more data than one
> +	 instruction.  */

I suggest:

      /* The opcodes disassembler may read more data than one
         instruction.  Supply infinite consecutive copies
         of the same instruction.

> +      for (unsigned int i = 0; i < len; i++)

size_t.

> +	myaddr[i] = self->m_insn[(memaddr + i) % self->m_len];

Clever.  :-)

> +
> +      return 0;
> +    }
> +  };
> +
> +  gdb_disassembler_test di (gdbarch, insn, len);
> +
> +  SELF_CHECK (di.print_insn (0) == len);
> +}
> +
> +} // namespace selftests
> +#endif /* GDB_SELF_TEST */
> +
> +/* Suppress warning from -Wmissing-prototypes.  */
> +extern initialize_file_ftype _initialize_disasm_test;
> +
> +void
> +_initialize_disasm_test (void)

The standard is to name the _initialize_foo function after
the file/module name:

 _initialize_disasm_selftests

> +
> +static void
> +tests_with_arch (void)

We longer need the "void" in C++.

> +{
> +  int failed = 0;
> +
> +  for (const auto &f : gdbarch_tests)
> +    {
> +      const char **arches = gdbarch_printable_names ();
> +      int i;
> +
> +      for (i = 0; arches[i] != NULL; i++)

Can be "for (int i ..." now.


> +/* Suppress warning from -Wmissing-prototypes.  */
> +extern initialize_file_ftype _initialize_selftests_with_arch;
> +
> +void
> +_initialize_selftests_with_arch (void)

Likewise (naming / void).

> +#ifndef SELFTEST_ARCH_H
> +#define SELFTEST_ARCH_H
> +
> +typedef void self_test_function_with_gdbarch (struct gdbarch *);
> +
> +extern void register_self_test (self_test_function_with_gdbarch *function);

IMO, overloading the "register_self_test" function is confusing.
This function and the register_self_test() function in selftest.c
are semantically different, not two ways to do the same
thing (like e.g. const char * vs std::string).

If nothing else, it makes it a bit harder to grep for / find
arch self tests (only) in the future.

I'd prefer calling this something else that indicates more clearly
what the selftest being registered is about.  That's why I had
suggested before the distinct:

  register_arch_self_test

Perhaps better would be:

  register_self_test_foreach_arch

And then self_test_function_with_gdbarch -> self_test_foreach_arch_function.

WDYT?

Thanks,
Pedro Alves


  reply	other threads:[~2017-01-20  0:04 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
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 7/8] Disassembly unit test: memory error 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 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 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: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 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 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 4/6] Disassembly unit test: disassemble one instruction Yao Qi
2017-01-20  0:04     ` Pedro Alves [this message]
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 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-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 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-25  8:38   ` [PATCH 0/6 v3] Handle memory error on disassembly 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 4/6] Disassembly unit test: disassemble one instruction 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 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=620db0ef-5e3d-3b93-5596-33d24a78f6a9@redhat.com \
    --to=palves@redhat.com \
    --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