Commit Graph

49494 Commits

Author SHA1 Message Date
Tom Tromey
3a3bb6eb36 Remove c_printstr
This renames c_printstr, removing a layer of indirection.
2022-10-10 10:43:34 -06:00
Tom Tromey
c1c7fe59f6 Remove c_emit_char
This renames c_emit_char, removing a layer of indirection.
2022-10-10 10:43:34 -06:00
Tom Tromey
3a64633804 Boolify need_escape in generic_emit_char
This changes 'need_escape' in generic_emit_char to be of type bool,
rather than int.
2022-10-10 10:43:34 -06:00
Tom Tromey
1be8435c74 Fix latent quote char bug in generic_printstr
generic_printstr prints an empty string like:

      fputs_filtered ("\"\"", stream);

However, this seems wrong to me if the quote character is something
other than double quote.  This patch fixes this latent bug.  Thanks to
Andrew for the test case.

Co-authored-by: Andrew Burgess <aburgess@redhat.com>
2022-10-10 10:43:33 -06:00
Tom Tromey
79aafec96b Fix the guile build
The frame_info_ptr patches broke the build with Guile.  This patch
fixes the problem.  In mos cases I chose to preserve the use of
frame_info_ptr, at least where I could be sure that the object
lifetime did not interact with Guile's longjmp-based exception scheme.

Tested on x86-64 Fedora 34.
2022-10-10 09:34:09 -06:00
Tom de Vries
c52ce60356 [gdb/testsuite] Detect trailing ^C/^D in command
Detect a trailing ^C/^D in the command argument of gdb_test_multiple, and
error out.

Tested on x86_64-linux.
2022-10-10 16:24:38 +02:00
Tom de Vries
ed6cd15957 [gdb/testsuite] Fix error message for cmd with trailing newline
I noticed that the error message in gdb_test_multiple about trailing newline
in a command does not mention the offending command, nor the word command:
...
    if [string match "*\[\r\n\]" $command] {
        error "Invalid trailing newline in \"$message\" test"
    }
...

Fix this by using instead:
...
        error "Invalid trailing newline in \"$command\" command"
...

Also add a test-case to trigger this: gdb.testsuite/gdb-test.exp.

Tested on x86_64-linux.
2022-10-10 14:44:40 +02:00
Andrew Burgess
66984afd29 gdb: include the base address in in-memory bfd filenames
The struct target_buffer (in gdb_bfd.c) is used to hold information
about an in-memory BFD object created by GDB.  For now this mechanism
is used by GDB when loading information about JIT symfiles.

This commit updates target_buffer (in gdb_bfd.c) to be more C++ like,
and, at the same time, adds the base address of the symfile into the
BFD filename.

Right now, every in-memory BFD is given the filename "<in-memory>".
This filename is visible in things like 'maint info symtabs' and
'maint info line-table'.  If there are multiple in-memory BFD objects
then it can be hard to match keep track if which BFD is which.  This
commit changes the name to be "<in-memory@ADDRESS>" where ADDRESS is
replaced with the base address for where the in-memory symbol file was
read from.

As an example of how this is useful, here's the output of 'maint info
jit' showing a single loaded JIT symfile:

  (gdb) maintenance info jit
  jit_code_entry address symfile address    symfile size
  0x00000000004056b0     0x0000000007000000 17320

And here's part of the output from 'maint info symtabs':

  (gdb) maintenance info symtabs
  ...snip...
  { objfile <in-memory@0x7000000> ((struct objfile *) 0x5258250)
    { ((struct compunit_symtab *) 0x4f0afb0)
      debugformat DWARF 4
      producer GNU C17 9.3.1 20200408 (Red Hat 9.3.1-2) -mtune=generic -march=x86-64 -g -fno-stack-protector -fpic
      name jit-elf-solib.c
      dirname /tmp/binutils-gdb/build/gdb/testsuite
      blockvector ((struct blockvector *) 0x5477850)
      user ((struct compunit_symtab *) (null))
  	{ symtab /tmp/binutils-gdb/build/gdb/testsuite/../../../src/gdb/testsuite/gdb.base/jit-elf-solib.c ((struct symtab *) 0x4f0b030)
  	  fullname (null)
  	  linetable ((struct linetable *) 0x5477880)
  	}
    }
  }

I've added a new test that checks the new in-memory file names are
generated correctly, and also checks that the in-memory JIT files can
be dumped back out using 'dump binary memory'.
2022-10-10 13:05:27 +01:00
Andrew Burgess
322dd71cbf gdb: remove filename arg from gdb_bfd_open_from_target_memory
The filename argument to gdb_bfd_open_from_target_memory was never
used; this argument had a default value of nullptr, and the only call
to this function, in jit.c, relied on the default value.

In the next commit I'm going to make some changes to the
gdb_bfd_open_from_target_memory function, and, though I could take
account of a filename parameter, it seems pointless to maintain an
unused argument.

This commit removes the filename argument.

There should be no user visible changes after this commit.
2022-10-10 13:05:27 +01:00
Andrew Burgess
e4014689b9 gdb: add infcall specific debugging
Add two new commands:

  set debug infcall on|off
  show debug infcall

These enable some new debugging related to when GDB makes inferior
function calls.  I've added some basic debugging for what I think are
the major steps in the inferior function call process, but I'm sure we
might want to add more later.
2022-10-10 12:18:00 +01:00
Andrew Burgess
58fd1204aa gdb: extra debug output in thread.c
Add some extra 'threads' debug in a couple of places in thread.c.
I've also added an additional gdb_assert in one case.
2022-10-10 12:16:18 +01:00
Andrew Burgess
adaef65695 gdb: improve infrun_debug_show_threads output
This commit switches to use INFRUN_SCOPED_DEBUG_START_END in the
infrun_debug_show_threads function, which means the output will get an
extra level of indentation, this looks a little nicer I think.
2022-10-10 12:16:18 +01:00
Bruno Larsen
c29a6445a9 gdb/frame: Add reinflation method for frame_info_ptr
Currently, despite having a smart pointer for frame_infos, GDB may
attempt to use an invalidated frame_info_ptr, which would cause internal
errors to happen.  One such example has been documented as PR
python/28856, that happened when printing frame arguments calls an
inferior function.

To avoid failures, the smart wrapper was changed to also cache the frame
id, so the pointer can be reinflated later.  For this to work, the
frame-id stuff had to be moved to their own .h file, which is included
by frame-info.h.

Frame_id caching is done explicitly using the prepare_reinflate method.
Caching is done manually so that only the pointers that need to be saved
will be, and reinflating has to be done manually using the reinflate
method because the get method and the -> operator must not change
the internals of the class.  Finally, attempting to reinflate when the
pointer is being invalidated causes the following assertion errors:

check_ptrace_stopped_lwp_gone: assertion `lp->stopped` failed.
get_frame_pc: Assertion `frame->next != NULL` failed.

As for performance concerns, my personal testing with `time make
chec-perf GDB_PERFTEST_MODE=run` showed an actual reduction of around
10% of time running.

This commit also adds a testcase that exercises the python/28856 bug with
7 different triggers, run, continue, step, backtrace, finish, up and down.
Some of them can seem to be testing the same thing twice, but since this
test relies on stale pointers, there is always a chance that GDB got lucky
when testing, so better to test extra.

Regression tested on x86_64, using both gcc and clang.

Approved-by: Tom Tomey <tom@tromey.com>
2022-10-10 11:57:10 +02:00
Tom Tromey
bd2b40ac12 Change GDB to use frame_info_ptr
This changes GDB to use frame_info_ptr instead of frame_info *
The substitution was done with multiple sequential `sed` commands:

sed 's/^struct frame_info;/class frame_info_ptr;/'
sed 's/struct frame_info \*/frame_info_ptr /g' - which left some
    issues in a few files, that were manually fixed.
sed 's/\<frame_info \*/frame_info_ptr /g'
sed 's/frame_info_ptr $/frame_info_ptr/g' - used to remove whitespace
    problems.

The changed files were then manually checked and some 'sed' changes
undone, some constructors and some gets were added, according to what
made sense, and what Tromey originally did

Co-Authored-By: Bruno Larsen <blarsen@redhat.com>
Approved-by: Tom Tomey <tom@tromey.com>
2022-10-10 11:57:10 +02:00
Tom Tromey
ba380b3e51 Introduce frame_info_ptr smart pointer class
This adds frame_info_ptr, a smart pointer class.  Every instance of
the class is kept on an intrusive list.  When reinit_frame_cache is
called, the list is traversed and all the pointers are invalidated.
This should help catch the typical GDB bug of keeping a frame_info
pointer alive where a frame ID was needed instead.

Co-Authored-By: Bruno Larsen <blarsen@redhat.com>
Approved-by: Tom Tomey <tom@tromey.com>
2022-10-10 11:57:10 +02:00
Tom Tromey
a0cbd6505e Remove frame_id_eq
This replaces frame_id_eq with operator== and operator!=.  I wrote
this for a version of this series that I later abandoned; but since it
simplifies the code, I left this patch in.

Approved-by: Tom Tomey <tom@tromey.com>
2022-10-10 11:57:10 +02:00
Andrew Burgess
f91822c2b9 gdb/testsuite: use 'end' at the end of python blocks
Within the testsuite, use the keyword 'end' to terminate blocks of
Python code being sent to GDB, rather than sending \004.  I could only
find three instances of this, all in tests that I originally wrote.  I
have no memory of there being any special reason why I used \004
instead of 'end' - I assume I copied this from somewhere else that has
since changed.

Non of the tests being changed here are specifically about whether
\004 can be used to terminate a Python block, so I think switching to
the more standard 'end' keyword is the right choice.
2022-10-10 10:23:59 +01:00
Tom Tromey
4482b068b2 Merge both implementations of debug_names::insert
The class debug_names has two 'insert' overloads, but only one of them
is ever called externally, and it simply forwards to the other
implementation.  It seems cleaner to me to have a single method, so
this patch merges the two.
2022-10-08 09:00:06 -06:00
Tom de Vries
db95282ce9 [gdb/testsuite] Fix silent fail in gdb.server/connect-with-no-symbol-file.exp
With native and target boards native-gdbserver, remote-gdbserver-on-localhost and
remote-stdio-gdbserver I have for gdb.server/connect-with-no-symbol-file.exp:
...
 # of expected passes            8
...
but with native-extended-gdbserver I have instead:
...
 # of expected passes            8
 # of unexpected failures        4
...

The extra FAILs are of the form:
...
(gdb) detach^M
Detaching from pid process 28985^M
[Inferior 1 (process 28985) detached]^M
(gdb) FAIL: gdb.server/connect-with-no-symbol-file.exp: sysroot=: \
  action=permission: connection to GDBserver succeeded
...
and are due to the fact that the actual gdb output doesn't match the regexp:
...
    gdb_test "detach" \
       ".*Detaching from program: , process.*Ending remote debugging.*" \
       "connection to GDBserver succeeded"
...

With native, the actual gdb output is:
...
(gdb) detach^M
Detaching from pid process 29657^M
Ending remote debugging.^M
[Inferior 1 (process 29657) detached]^M
(gdb) Remote debugging from host ::1, port 51028^M
...
and because the regexp doesn't match, it triggers an implicit clause for
"Ending remote debugging" in gdb_test_multiple, which has the consequence
that the FAIL is silent.

Fix:
- the regexp by making it less strict
- the silent fail by rewriting into a gdb_test_multiple, and adding an
  explicit fail clause.

Tested on x86_64-linux, using native and aforementioned target boards.
2022-10-08 10:47:47 +02:00
Lancelot SIX
ae17d05a4a gdb/testsuite: fix gdb.threads/linux-dp.exp regex
On ubuntu 22.04 with the libc6-dbg package installed, I have the
following failure:

    where
    #0  print_philosopher (n=3, left=33 '!', right=33 '!') at .../gdb/testsuite/gdb.threads/linux-dp.c:105
    #1  0x000055555555576a in philosopher (data=0x55555555937c) at .../gdb/testsuite/gdb.threads/linux-dp.c:148
    #2  0x00007ffff7e11b43 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:442
    #3  0x00007ffff7ea3a00 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
    (gdb) FAIL: gdb.threads/linux-dp.exp: first thread-specific breakpoint hit

The regex for this test accounts for different situations (with /
without debug symbol) but assumes that if debug info is present the
backtrace shows execution under pthread_create.  However, for the
implementation under test, we are under start_thread.

Update the regex to accept start_thread.

Tested on Ubuntu-22.04 x86_64 with and without libc6-dbg debug symbols
available.

Change-Id: I1e1536279890bca2cd07f038e026b41e46af44e0
2022-10-07 20:15:21 +01:00
Tom de Vries
7808a1f7f6 [gdb/testsuite] Handle host cleanfiles
When running test-case gdb.server/abspath.exp with host board
local-remote-host-notty, I get:
...
$ git sti
  ...
        deleted:    gdb/testsuite/gdb.xml/trivial.xml
...

This happens as follows.  The test-case calls skip_gdbserver_test, which calls
gdb_skip_xml_test, which does:
...
    set xml_file [gdb_remote_download host "${srcdir}/gdb.xml/trivial.xml"]
...

Then proc gdb_remote_download appends $xml_file (which for this particular
host board happens to be ${srcdir}/gdb.xml/trivial.xml) to cleanfiles, which
ends up being handled in gdb_finish by:
...
       eval remote_file target delete $cleanfiles
...

The problem is that a host file is deleted using target delete.

Fix this by splitting cleanfiles up in cleanfiles_target and cleanfiles_host.

Tested on x86_64-linux.
2022-10-07 18:08:00 +02:00
Tom de Vries
8c7cb01a29 [gdb/testsuite] Remove unnecessary warning in gdb.base/default.exp
When running test-case gdb.base/default.exp with target board
native-gdbserver, we get:
...
WARNING: Skipping backtrace and break tests because of GDB stub.
...

There's no need for such a warning, so remove it.

Tested on x86_64-linux with native and target board native-gdbserver.
2022-10-07 16:17:35 +02:00
Tom de Vries
43792b0de4 [gdb/testsuite] Fix have_mpx with remote-gdbserver-on-localhost
With target board remote-gdbserver-on-localhost and gdb.arch/i386-mpx-call.exp
I run into:
...
FAIL: gdb.arch/i386-mpx-call.exp: upper_bnd0: continue to a bnd violation
...

This is due to the have_mpx test which should return 0, but instead returns 1
because the captured output:
...
No MPX support
No MPX support
...
does not match the used regexp:
...
    set status [expr ($status == 0) \
                   && ![regexp "^No MPX support\r\n" $output]]
...
which does match the captured output with native:
...
No MPX support^M
No MPX support^M
...

Fix this by making the \r in the regexp optional.

Tested on x86_64-linux, with native and target board
remote-gdbserver-on-localhost.
2022-10-07 16:17:35 +02:00
Tom de Vries
7fe3f84145 [gdb/testsuite] Fix DUPLICATEs with remote-gdbserver-on-localhost
Fix some DUPLICATEs that we run into with target board
remote-gdbserver-on-localhost, by using test_with_prefix.

Tested on x86_64-linux, with native and target board
remote-gdbserver-on-localhost.
2022-10-07 16:17:34 +02:00
Tom de Vries
8ed73e3164 [gdb/testsuite] Fix path in test name in gdb_load_shlib
When running test-case gdb.server/solib-list.exp with target board
remote-gdbserver-on-localhost, I run into:
...
(gdb) set solib-search-path $outputs/gdb.server/solib-list^M
(gdb) PASS: gdb.server/solib-list.exp: non-stop 0: \
  set solib-search-path $outputs/gdb.server/solib-list
PATH: gdb.server/solib-list.exp: non-stop 0: \
  set solib-search-path $outputs/gdb.server/solib-list
...

This is due to this code in gdb_load_shlib:
...
       gdb_test "set solib-search-path [file dirname $file]" "" ""
...

Fix this by setting an explicit test name.

Tested on x86_64-linux, with native and target boards
remote-gdbserver-on-localhost, native-gdbserver and native-extended-gdbserver.
2022-10-07 16:17:34 +02:00
Tom Tromey
0bce60ac7f Fix indentation in riscv-tdep.c
This just fixes some indentation in riscv-tdep.c.
2022-10-06 11:27:11 -06:00
Torbjörn SVENSSON
60c90d8c6d gdb/arm: Handle lazy FPU state preservation
Read LSPEN, ASPEN and LSPACT bits from FPCCR and use them together
with FPCAR to identify if lazy FPU state preservation is active for
the current frame.  See "Lazy context save of FP state", in B1.5.7,
also ARM AN298, supported by Cortex-M4F architecture for details on
lazy FPU register stacking.  The same conditions are valid for other
Cortex-M cores with FPU.

This patch has been verified on a STM32F4-Discovery board by:
a) writing a non-zero value (lets use 0x1122334455667788 as an
   example) to all the D-registers in the main function
b) configured the SysTick to fire
c) in the SysTick_Handler, write some other value (lets use
   0x0022446688aaccee as an example) to one of the D-registers (D0 as
   an example) and then do "SVC #0"
d) in the SVC_Handler, write some other value (lets use
   0x0099aabbccddeeff) to one of the D-registers (D0 as an example)

In GDB, suspend the execution in the SVC_Handler function and compare
the value of the D-registers for the SVC_handler frame and the
SysTick_Handler frame.  With the patch, the value of the modified
D-register (D0) should be the new value (0x009..eff) on the
SVC_Handler frame, and the intermediate value (0x002..cee) for the
SysTick_Handler frame.  Now compare the D-register value for the
SysTick_Handler frame and the main frame.  The main frame should
have the initial value (0x112..788).

Signed-off-by: Torbjörn SVENSSON  <torbjorn.svensson@foss.st.com>
Signed-off-by: Yvan ROUX  <yvan.roux@foss.st.com>
2022-10-06 16:01:10 +02:00
Tom de Vries
ca10a126c6 [gdb/symtab] Factor out have_complaint
After committing 8ba677d356 ("[gdb/symtab] Don't complain about function
decls") I noticed that quite a bit of code in read_func_scope is used to decide
whether to issue the "cannot get low and high bounds for subprogram DIE at
$hex" complaint, which executes unnecessarily if we have the default
"set complaints 0".

Fix this by (NFC):
- factoring out new static function have_complaint from macro complaint, and
- using it to wrap the relevant code in read_func_scope.

Tested on x86_64-linux.
2022-10-06 14:53:07 +02:00
Andrew Burgess
80e0c6dc91 gdb: add missing nullptr checks in bpstat_check_breakpoint_conditions
Add a couple of missing nullptr checks in the function
bpstat_check_breakpoint_conditions.

No user visible change after this commit.
2022-10-06 10:01:15 +01:00
Andrew Burgess
d8a77e4c80 gdb: more infrun debug from breakpoint.c
This commit adds additional infrun debug from the breakpoint.c file.
The new debug output all relates to breakpoint condition evaluation.

There is already some infrun debug emitted from the breakpoint.c file,
so hopefully, adding more will not be contentious.  I think the
functions being instrumented make sense as part of the infrun process,
the inferior stops, evaluates the condition, and then either stops or
continues.  This new debug gives more insight into that process.

I had to make the bp_location* argument to find_loc_num_by_location
const, and add a declaration for find_loc_num_by_location.

There should be no user visible changes unless they turn on debug
output.
2022-10-06 10:01:15 +01:00
Andrew Burgess
07443de62b gdb: add some additional debug in mark_async_event_handler
Extend the existing debug printf call to include the previous state of
the async_event_handler object.
2022-10-06 10:01:14 +01:00
Tom Tromey
8f5bc64185 Remove decode_location_spec_default
This removes decode_location_spec_default, inlining it into its sole
caller.

Regression tested on x86-64 Fedora 34.
2022-10-04 16:17:08 -06:00
Tom de Vries
8ba677d356 [gdb/symtab] Don't complain about function decls
[ Requires "[gdb/symtab] Don't complain about inlined functions" as
submitted here (
https://sourceware.org/pipermail/gdb-patches/2022-September/191762.html ). ]

With the test-case included in this patch, we get:
...
(gdb) ptype main^M
During symbol reading: cannot get low and high bounds for subprogram DIE \
  at 0xc1^M
type = int (void)^M
(gdb) FAIL: gdb.dwarf2/anon-ns-fn.exp: ptype main without complaints
...

The DIE causing the complaint is a function declaration:
...
 <2><c1>: Abbrev Number: 3 (DW_TAG_subprogram)
    <c2>   DW_AT_name        : foo
    <c8>   DW_AT_declaration : 1
...
which is referred to from the DIE representing the function definition:
...
 <1><f4>: Abbrev Number: 7 (DW_TAG_subprogram)
    <f5>   DW_AT_specification: <0xc1>
    <f9>   DW_AT_low_pc      : 0x4004c7
    <101>   DW_AT_high_pc     : 0x7
...
which does contain the low and high bounds.

Fix this by not complaining about function declarations.

Tested on x86_64-linux.
2022-10-04 16:51:03 +02:00
Tom de Vries
3aeba5cd1c [gdb/symtab] Don't complain about inlined functions
With the test-case included in this patch, we get:
...
(gdb) ptype main^M
During symbol reading: cannot get low and high bounds for subprogram DIE \
  at 0x113^M
During symbol reading: cannot get low and high bounds for subprogram DIE \
  at 0x11f^M
type = int (void)^M
(gdb) FAIL: gdb.dwarf2/inline.exp: ptype main
...

The complaints are about foo, with DW_AT_inline == DW_INL_inlined:
...
 <1><11f>: Abbrev Number: 6 (DW_TAG_subprogram)
    <120>   DW_AT_name        : foo
    <126>   DW_AT_prototyped  : 1
    <126>   DW_AT_type        : <0x10c>
    <12a>   DW_AT_inline      : 1       (inlined)
...
and foo2, with DW_AT_inline == DW_INL_declared_inlined:
...
 <1><113>: Abbrev Number: 5 (DW_TAG_subprogram)
    <114>   DW_AT_name        : foo2
    <11a>   DW_AT_prototyped  : 1
    <11a>   DW_AT_type        : <0x10c>
    <11e>   DW_AT_inline      : 3       (declared as inline and inlined)
...

Fix this by not complaining about inlined functions.

Tested on x86_64-linux.
2022-10-04 16:51:03 +02:00
Tsukasa OI
436a7b5ef2 gdb/riscv: Partial support for instructions up to 176-bit
Because riscv_insn_length started to support instructions up to 176-bit,
we need to increase buf size to 176-bit in size.

Also, that would break an assumption in riscv_insn::decode so this commit
fixes it, noting that instructions longer than 64-bit are not fully
supported yet.
2022-10-04 13:21:41 +00:00
Luis Machado
f4b581f2d1 [AArch64] Update FPSR/FPCR fields for FPU and SVE
I noticed some missing flags/fields from FPSR and FPCR registers in
both the FPU and SVE target descriptions.

This patch adds those and makes the SVE versions of FPSR and FPCR
use the proper flags/bitfields types.
2022-10-04 09:14:34 +01:00
Simon Marchi
d1d3123c7b gdb: constify inferior::target_is_pushed
Change-Id: Ia4143b9c63cb76e2c824ba773c66f5c5cd94b2aa
2022-10-03 09:33:01 -04:00
Luis Machado
e63ae49b6a [AArch64] Handle W registers as pseudo-registers instead of aliases of X registers
The aarch64 port handles W registers as aliases of X registers. This is
incorrect because X registers are 64-bit and W registers are 32-bit.

This patch teaches GDB how to handle W registers as pseudo-registers of
32-bit, the bottom half of the X registers.

Testcase included.
2022-10-03 14:15:25 +01:00
Luis Machado
1ba3a32220 [AArch64] Fix pseudo-register numbering in the presence of unexpected additional registers
When using AArch64 GDB with the QEMU debugging stub (in user mode), we get
additional system registers that GDB doesn't particularly care about, so
it doesn't number those explicitly.

But given the pseudo-register numbers are above the number of real registers,
we need to setup/account for the real registers first before going ahead and
numbering the pseudo-registers.  This has to happen at the end of
aarch64_gdbarch_init, after the call to tdesc_use_registers, as that
updates the total number of real registers.

This is in preparation to supporting pointer authentication for bare metal
aarch64 (QEMU).
2022-10-03 14:15:16 +01:00
Bruno Larsen
2820f08f23 Improve GDB's baseclass detection with typedefs
When a class inherits from a typedef'd baseclass, GDB may be unable to
find the baseclass if the user is not using the typedef'd name, as is
tested on gdb.cp/virtbase2.exp; the reason that test case is working
under gcc is that the dwarf generated by gcc links the class to the
original definition of the baseclass, not to the typedef.  If the
inheritance is linked to the typedef, such as how clang does it,
gdb.cp/virtbase2.exp starts failing.

This can also be seen in gdb.cp/impl-this.exp, when attempting to print
D::Bint::i, and GDB not being able to find the baseclass Bint.

This happens because searching for baseclasses only uses the macro
TYPE_BASECLASS_NAME, which returns the typedef'd name. However, we can't
switch that macro to checking for typedefs, otherwise we wouldn't be
able to find the typedef'd name anymore. This is fixed by searching for
members or baseclasses by name, we check both the saved name and the
name after checking for typedefs.

This also fixes said long-standing bug in gdb.cp/impl-this.exp when the
compiler adds information about typedefs in the debuginfo.
2022-10-03 10:09:30 +02:00
Tom de Vries
4cfa9edb35 [gdb/testsuite] Fix waitpid testing in next-fork-other-thread.c
In next-fork-other-thread.c, there's this loop:
...
      do
        {
          ret = waitpid (pid, &stat, 0);
        } while (ret == EINTR);
...

The loop condition tests for "ret == EINTR" but waitpid signals EINTR by
returning -1 and setting errno to EINTR.

Fix this by changing the loop condition to "ret == -1 && errno == EINTR".

Tested on x86_64-linux.
2022-10-02 20:18:00 +02:00
Andrew Burgess
c3d64d467d gdb/testsuite: handle invalid .exp names passed in TESTS
I ran some tests like:

  $ make check-gdb TESTS="gdb.base/break.exp"

then, then I went to rerun the tests later, I managed to corrupt the
command line, like this:

  $ make check-gdb TESTS="gdb.base/breakff.exp"

the make command did exit with an error, but DejaGnu appeared to
report that every test passed!  The tail end of the output looks like
this:

  Illegal Argument "no-matching-tests-found"
  try "runtest --help" for option list
  		=== gdb Summary ===

  # of expected passes		115
  /tmp/build/gdb/gdb version  13.0.50.20220831-git -nw -nx -iex "set height 0" -iex "set width 0" -data-directory /tmp/build/gdb/testsuite/../data-directory

  make[3]: *** [Makefile:212: check-single] Error 1
  make[3]: Leaving directory '/tmp/build/gdb/testsuite'
  make[2]: *** [Makefile:161: check] Error 2
  make[2]: Leaving directory '/tmp/build/gdb/testsuite'
  make[1]: *** [Makefile:1916: check] Error 2
  make[1]: Leaving directory '/tmp/build/gdb'
  make: *** [Makefile:13565: check-gdb] Error 2

For a while, I didn't spot that DejaGnu had failed at all, I saw the
115 passes, and thought everything had run correctly - though I was
puzzled that make was reporting an error.

What happens is that in gdb/testsuite/Makefile, in the check-single
rule, we first run DejaGnu, then run the dg-add-core-file-count.sh
script, and finally, we use sed to extract the results from the
gdb.sum file.

In my case, with the invalid test name, DejaGnu fails, but the
following steps are still run, the final result, the 115 passes, is
then extracted from the pre-existing gdb.sum file.

If I use 'make -jN' then the 'check-parallel' rule, rather than the
'check-single' rule is used.  In this case the behaviour is slightly
different, the tail end of the output now looks like this:

  No matching tests found.

  make[4]: Leaving directory '/tmp/build/gdb/testsuite'
  find: ‘outputs’: No such file or directory
  Usage: ../../../src/gdb/testsuite/../../contrib/dg-extract-results.py [-t tool] [-l variant-list] [-L] log-or-sum-file ...

      tool           The tool (e.g. g++, libffi) for which to create a
                     new test summary file.  If not specified then output
                     is created for all tools.
      variant-list   One or more test variant names.  If the list is
                     not specified then one is constructed from all
                     variants in the files for <tool>.
      sum-file       A test summary file with the format of those
                     created by runtest from DejaGnu.
      If -L is used, merge *.log files instead of *.sum.  In this
      mode the exact order of lines may not be preserved, just different
      Running *.exp chunks should be in correct order.
  find: ‘outputs’: No such file or directory
  Usage: ../../../src/gdb/testsuite/../../contrib/dg-extract-results.py [-t tool] [-l variant-list] [-L] log-or-sum-file ...

      tool           The tool (e.g. g++, libffi) for which to create a
                     new test summary file.  If not specified then output
                     is created for all tools.
      variant-list   One or more test variant names.  If the list is
                     not specified then one is constructed from all
                     variants in the files for <tool>.
      sum-file       A test summary file with the format of those
                     created by runtest from DejaGnu.
      If -L is used, merge *.log files instead of *.sum.  In this
      mode the exact order of lines may not be preserved, just different
      Running *.exp chunks should be in correct order.
  make[3]: Leaving directory '/tmp/build/gdb/testsuite'
  make[2]: Leaving directory '/tmp/build/gdb/testsuite'
  make[1]: Leaving directory '/tmp/build/gdb'

Rather than DejaGnu failing, we now get a nice 'No matching tests
found' message, followed by some other noise.  This other noise is
first `find` failing, followed by the dg-extract-results.py script
failing.

What happens here is that, in the check-parallel rule, the outputs
directory is deleted before DejaGnu is invoked.  Then we try to run
all the tests, and finally we use find and dg-extract-results.py to
combine all the separate .sun and .log files together.  However, if
there are no tests run then the outputs/ directory is never created,
so the find command and consequently the dg-extract-results.py script,
fail.

This commit aims to fix the following issues:

 (1) For check-single and check-parallel rules, don't run any of the
 post-processing steps if DejaGnu failed to run.  This will avoid all
 the noise after the initial failure of DejaGnu,

 (2) For check-single ensure that we don't accidentally report
 previous results, this is related to the above, but is worth calling
 out as a separate point, and

 (3) For check-single, print the 'No matching tests found' message
 just like we do for a parallel test run.  This makes the parallel and
 non-parallel testing behaviour more similar, and I think is clearer
 than the current 'Illegal Argument' error message.

Points (1) and (2) will be handled by moving the post processing steps
inside an if block within the recipe.  For check-single I propose
deleting the gdb.sum and gdb.log files before running DejaGnu, this is
similar (I think) to how we delete the outputs/ directory in the
check-parallel rule.

For point (3) I plan to split the check-single rule in two, the
existing check-single will be renamed do-check-single, then a new
check-single rule will be added.  The new check-single rule can either
depend on the new do-check-single, or will ensure the 'No matching
tests found' message is printed when appropriate.
2022-10-02 17:43:55 +01:00
Andrew Burgess
6deb7a8185 gdb/disasm: better intel flavour disassembly styling with Pygments
This commit was inspired by this stackoverflow post:

  https://stackoverflow.com/questions/73491793/why-is-there-a-%C2%B1-in-lea-rax-rip-%C2%B1-0xeb3

One of the comments helpfully links to this Python test case:

  from pygments import formatters, lexers, highlight

  def colorize_disasm(content, gdbarch):
      try:
          lexer = lexers.get_lexer_by_name("asm")
          formatter = formatters.TerminalFormatter()
          return highlight(content, lexer, formatter).rstrip().encode()
      except:
          return None

  print(colorize_disasm("lea [rip+0x211]  # COMMENT", None).decode())

Run the test case and you should see that the '+' character is
underlined, and could be confused with a combined +/- symbol.

What's happening is that Pygments is failing to parse the input text,
and the '+' is actually being marked in the error style.  The error
style is red and underlined.

It is worth noting that the assembly instruction being disassembled
here is an x86-64 instruction in the 'intel' disassembly style, rather
than the default att style.  Clearly the Pygments module expects the
att syntax by default.

If we change the test case to this:

  from pygments import formatters, lexers, highlight

  def colorize_disasm(content, gdbarch):
      try:
          lexer = lexers.get_lexer_by_name("asm")
          lexer.add_filter('raiseonerror')
          formatter = formatters.TerminalFormatter()
          return highlight(content, lexer, formatter).rstrip().encode()
      except:
          return None

  res = colorize_disasm("lea rax,[rip+0xeb3] # COMMENT", None)
  if res:
      print(res.decode())
  else:
      print("No result!")

Here I've added the call: lexer.add_filter('raiseonerror'), and I am
now checking to see if the result is None or not.  Running this and
the test now print 'No result!' - instead of styling the '+' in the
error style, we instead give up on the styling attempt.

There are two things we need to fix relating to this disassembly
text.  First, Pygments is expecting att style disassembly, not the
intel style that this example uses.  Fortunately, Pygments also
supports the intel style, all we need to do is use the 'nasm' lexer
instead of the 'asm' lexer.

However, this leads to the second problem; in our disassembler line we
have '# COMMENT'.  The "official" Intel disassembler style uses ';'
for its comment character, however, gas and libopcodes use '#' as the
comment character, as gas uses ';' for an instruction separator.

Unfortunately, Pygments expects ';' as the comment character, and
treats '#' as an error, which means, with the addition of the
'raiseonerror' filter, that any line containing a '#' comment, will
not get styled correctly.

However, as the i386 disassembler never produces a '#' character other
than for comments, we can easily "fix" Pygments parsing of the
disassembly line.  This is done by creating a filter.  This filter
looks for an Error token with the value '#', we then change this into
a comment token.  Every token after this (until the end of the line)
is also converted into a comment.

In this commit I do the following:

  1. Check the 'disassembly-flavor' setting and select between the
  'asm' and 'nasm' lexers based on the setting.  If the setting is not
  available then the 'asm' lexer is used by default,

  2. Use "add_filter('raiseonerror')" to ensure that the formatted
  output will not include any error text, which would be underlined,
  and might be confusing,

  3. If the 'nasm' lexer is selected, then add an additional filter
  that will format '#' and all other text on the line, as a comment,
  and

  4. If Pygments throws an exception, instead of returning None,
  return the original, unmodified content.  This will mean that this
  one instruction is printed without styling, but GDB will continue to
  call into the Python code to style later instructions.

I haven't included a test specifically for the above error case,
though I have manually check that the above case now styles
correctly (with no underline).  The existing style tests check that
the disassembler styling still works though, so I know I've not
generally broken things.

One final thought I have after looking at this issue is that I wonder
now if using Pygments for styling disassembly from every architecture
is actually a good idea?

Clearly, the 'asm' lexer is OK with att style x86-64, but not OK with
intel style x86-64, so who knows how well it will handle other random
architectures?

When I first added this feature I tested it against some random
RISC-V, ARM, and X86-64 (att style) code, and it seemed fine, but I
never tried to make an exhaustive check of all instructions, so its
quite possible that there are corner cases where things are styled
incorrectly.

With the above changes I think that things should be a bit better
now.  If a particular instruction doesn't parse correctly then our
Pygments based styling code will just not style that one instruction.
This is combined with the fact that many architectures are now moving
to libopcodes based styling, which is much more reliable.

So, I think it is fine to keep using Pygments as a fallback mechanism
for styling all architectures, even if we know it might not be perfect
in all cases.
2022-10-02 17:30:04 +01:00
Andrew Burgess
f22c50c22c gdb: improve disassembler styling when Pygments raises an exception
While working on another issue relating to GDB's use of the Python
Pygments package for disassembly styling I noticed an issue in the
case where the Pygments package raises an exception.

The intention of the current code is that, should the Pygments package
raise an exception, GDB will disable future attempts to call into the
Pygments code.  This was intended to prevent repeated errors during
disassembly if, for some reason, the Pygments code isn't working.

Since the Pygments based styling was added, GDB now supports
disassembly styling using libopcodes, but this is only available for
some architectures.  For architectures not covered by libopcodes
Pygments is still the only option.

What I observed is that, if I disable the libopcodes styling, then
setup GDB so that the Pygments based styling code will indicate an
error (by returning None), GDB does, as expected, stop using the
Pygments based styling.  However, the libopcodes based styling will
instead be used, despite this feature having been disabled.

The problem is that the disassembler output is produced into a
string_file buffer.  When we are using Pygments, this buffer is
created without styling support.  However, when Pygments fails, we
recreate the buffer with styling support.

The problem is that we should only recreate the buffer with styling
support only if libopcodes styling is enabled.  This was an oversight
in commit:

  commit 4cbe4ca5da
  Date:   Mon Feb 14 14:40:52 2022 +0000

      gdb: add support for disassembler styling using libopcodes

This commit:

  1. Factors out some of the condition checking logic into two new
  helper functions use_ext_lang_for_styling and
  use_libopcodes_for_styling,

  2. Reorders gdb_disassembler::m_buffer and gdb_disassembler::m_dest,
  this allows these fields to be initialised m_dest first, which means
  that the new condition checking functions can rely on m_dest being
  set, even when called from the gdb_disassembler constructor,

  3. Make use of the new condition checking functions each time
  m_buffer is initialised,

  4. Add a new test that forces the Python disassembler styling
  function to return None, this will cause GDB to disable use of
  Pygments for styling, and

  5. When we reinitialise m_buffer, and re-disassemble the
  instruction, call reset the in-comment flag.  If the instruction
  being disassembler ends in a comment then the first disassembly pass
  will have set the in-comment flag to true.  This shouldn't be a
  problem as we will only be using Pygments, and thus performing a
  re-disassembly pass, if libopcodes is disabled, so the in-comment
  flag will never be checked, even if it is set incorrectly.  However,
  I think that having the flag set correctly is a good thing, even if
  we don't check it (you never know what future uses might come up).
2022-10-02 17:30:04 +01:00
Andrew Burgess
5b0e2b48ed gdb/testsuite: extend styling test for libopcodes styling
This commit extends the gdb.base/style.exp test to cover disassembler
styling using libopcodes (where available).

The test will try to enable libopcode based styling, if this
works (because such styling is available) then some tests are run to
check that the output is styled, and that the styling can be disabled
using 'set style disassembler enabled off'.  If libopcodes styling is
not available on the current architecture then these new tests will be
skipped.

I've moved the Python Pygments module check inside the
test_disable_disassembler_styling function now, so that the test will
be run even when Python Pygments is not available, this allows the new
tests discussed above.

If the Pygments module is not available then the Pygments based tests
will be skipped just as they were before.
2022-10-02 17:30:04 +01:00
Andrew Burgess
637b2f8613 gdb: update now gdbarch_register_name doesn't return nullptr
After the previous few commit, gdbarch_register_name no longer returns
nullptr.  This commit audits all the calls to gdbarch_register_name
and removes any code that checks the result against nullptr.

There should be no visible change after this commit.
2022-10-02 14:21:25 +01:00
Andrew Burgess
9b9e61c7cf gdb: final cleanup of various gdbarch_register_name methods
Building on the previous commits, this commit goes through the various
gdbarch_register_name methods and removes all the remaining 'return
NULL' cases, I claim that these either couldn't be hit, or should be
returning the empty string.

In all cases the return of NULL was used if the register number being
passed to gdbarch_register_name was "invalid", i.e. negative, or
greater than the total number of declared registers.  I don't believe
either of these cases can occur, and the previous commit asserts that
this is the case.  As a result we can simplify the code by removing
these checks.  In many cases, where the register names are held in an
array, I was able to add a static assert that the array contains the
correct number of strings, after that, a direct access into the array
is fine.

I don't have any means of testing these changes.
2022-10-02 14:21:25 +01:00
Andrew Burgess
7ac20d65a8 gdb/csky: remove nullptr return from csky_pseudo_register_name
Building on the previous commits, in this commit I remove two
instances of 'return NULL' from csky_pseudo_register_name, and replace
them with a return of the empty string.

These two are particularly interesting, and worth pulling into their
own commit, because these returns of NULL appear to be depended on
within other parts of the csky code.

In csky-linux-tdep.c in the register collect/supply code, GDB checks
for the register name being nullptr in order to decide if a target
supports a particular feature or not.  I've updated the code to check
for the empty string.

I have no way of testing this change.
2022-10-02 14:21:24 +01:00
Andrew Burgess
7df4240040 gdb: add asserts to gdbarch_register_name
This commit adds asserts to gdbarch_register_name that validate the
parameters, and the return value.

The interesting thing here is that gdbarch_register_name is generated
by gdbarch.py, and so, to add these asserts, I need to update the
generation script.

I've added two new arguments for Functions and Methods (as declared in
gdbarch-components.py), these arguments are 'param_checks' and
'result_checks'.  Each of these new arguments can be used to list some
expressions that are then used within gdb_assert calls in the
generated code.

The asserts that validate the API as described in the comment I added
to gdbarch_register_name a few commits back; the register number
passed in needs to be a valid cooked register number, and the result
being returned should not be nullptr.
2022-10-02 14:21:24 +01:00
Andrew Burgess
9a103324fe gdb: check for duplicate register names in selftest
Building on the previous commit, this commit extends the register_name
selftest to check for duplicate register names.

If two registers in the cooked register set (real + pseudo registers)
have the same name, then this will show up as duplicate registers in
the 'info all-registers' output, but the user will only be able to
interact with one copy of the register.

In this commit I extend the selftest that I added in the previous
commit to check for duplicate register names, I didn't include this
functionality in the previous commit because one architecture needed
fixing, and I wanted to keep those fixes separate from the fixes in
the previous commit.

The problematic architecture(s) are powerpc:750 and powerpc:604.  In
both of these cases the 'dabr' register appears twice, there's a
definition of dabr in power-oea.xml which is included into both
powerpc-604.xml and powerpc-750.xml.  Both of these later two xml
files also define the dabr register.

I'm hopeful that this change shouldn't break anything, but I don't
have the ability to actually test this change, however:

On the gdbserver side, neither powerpc-604.xml nor powerpc-750.xml are
mentioned in gdbserver/configure.srv, which I think means that
gdbserver will never use these descriptions, and,

Within GDB the problematic descriptions are held in the variables
tdesc_powerpc_604 and tdesc_powerpc_750, which are only mentioned in
the variants array in rs6000-tdep.c, this is used when looking up a
description based on the architecture.

For a native Linux target however, this will not be used as
ppc_linux_nat_target::read_description exists, which calls
ppc_linux_match_description, which I don't believe can return either
of the problematic descriptions.

This leaves the other native targets, FreeBSD, AIX, etc.  These don't
appear to override the ::read_description method, so will potentially
return the problematic descriptions, but, in each case I think the
::fetch_registers and ::store_registers methods will ignore the dabr
register, which will leave the register as <unavailable>.

So, my proposed solution is to just remove the duplicate register from
each of powerpc-604.xml and powerpc-750.xml, then regenerate the
corresponding C++ source file.  With this change made, the selftest
now passes for all architectures.
2022-10-02 14:21:24 +01:00