Commit Graph

114832 Commits

Author SHA1 Message Date
Andrew Burgess
889bdea181 gdb: handle core files with .reg/0 section names
The previous commit added the test gdb.arch/core-file-pid0.exp which
tests GDB's ability to load a core file containing threads with an
lwpid of 0, which is something we GDB can encounter when loading a
vmcore file -- a core file generated by the Linux kernel.  The threads
with an lwpid of 0 represents idle cores.

While the previous commit added the test, which confirms GDB doesn't
crash when confronted with such a core file, there are still some
problems with GDB's handling of these core files.  These problems all
originate from the fact that the core file (once opened by bfd)
contains multiple sections called .reg/0, these sections all
represents different threads (cpu cores in the original vmcore dump),
but GDB gets confused and thinks all of these .reg/0 sections are all
referencing the same thread.

Here is a GDB session on an x86-64 machine which loads the core file
from the gdb.arch/core-file-pid0.exp, this core file contains two
threads, both of which have a pid of 0:

  $ ./gdb/gdb --data-directory ./gdb/data-directory/ -q
  (gdb) core-file /tmp/x86_64-pid0-core.core
  [New process 1]
  [New process 1]
  Failed to read a valid object file image from memory.
  Core was generated by `./segv-mt'.
  Program terminated with signal SIGSEGV, Segmentation fault.
  The current thread has terminated
  (gdb) info threads
    Id   Target Id         Frame
    2    process 1         0x00000000004017c2 in ?? ()

  The current thread <Thread ID 1> has terminated.  See `help thread'.
  (gdb) maintenance info sections
  Core file: `/tmp/x86_64-pid0-core.core', file type elf64-x86-64.
   [0]      0x00000000->0x000012d4 at 0x00000318: note0 READONLY HAS_CONTENTS
   [1]      0x00000000->0x000000d8 at 0x0000039c: .reg/0 HAS_CONTENTS
   [2]      0x00000000->0x000000d8 at 0x0000039c: .reg HAS_CONTENTS
   [3]      0x00000000->0x00000080 at 0x0000052c: .note.linuxcore.siginfo/0 HAS_CONTENTS
   [4]      0x00000000->0x00000080 at 0x0000052c: .note.linuxcore.siginfo HAS_CONTENTS
   [5]      0x00000000->0x00000140 at 0x000005c0: .auxv HAS_CONTENTS
   [6]      0x00000000->0x000000a4 at 0x00000714: .note.linuxcore.file/0 HAS_CONTENTS
   [7]      0x00000000->0x000000a4 at 0x00000714: .note.linuxcore.file HAS_CONTENTS
   [8]      0x00000000->0x00000200 at 0x000007cc: .reg2/0 HAS_CONTENTS
   [9]      0x00000000->0x00000200 at 0x000007cc: .reg2 HAS_CONTENTS
   [10]     0x00000000->0x00000440 at 0x000009e0: .reg-xstate/0 HAS_CONTENTS
   [11]     0x00000000->0x00000440 at 0x000009e0: .reg-xstate HAS_CONTENTS
   [12]     0x00000000->0x000000d8 at 0x00000ea4: .reg/0 HAS_CONTENTS
   [13]     0x00000000->0x00000200 at 0x00000f98: .reg2/0 HAS_CONTENTS
   [14]     0x00000000->0x00000440 at 0x000011ac: .reg-xstate/0 HAS_CONTENTS
   [15]     0x00400000->0x00401000 at 0x00002000: load1 ALLOC LOAD READONLY HAS_CONTENTS
   [16]     0x00401000->0x004b9000 at 0x00003000: load2 ALLOC READONLY CODE
   [17]     0x004b9000->0x004e5000 at 0x00003000: load3 ALLOC READONLY
   [18]     0x004e6000->0x004ec000 at 0x00003000: load4 ALLOC LOAD HAS_CONTENTS
   [19]     0x004ec000->0x004f2000 at 0x00009000: load5 ALLOC LOAD HAS_CONTENTS
   [20]     0x012a8000->0x012cb000 at 0x0000f000: load6 ALLOC LOAD HAS_CONTENTS
   [21]     0x7fda77736000->0x7fda77737000 at 0x00032000: load7 ALLOC READONLY
   [22]     0x7fda77737000->0x7fda77f37000 at 0x00032000: load8 ALLOC LOAD HAS_CONTENTS
   [23]     0x7ffd55f65000->0x7ffd55f86000 at 0x00832000: load9 ALLOC LOAD HAS_CONTENTS
   [24]     0x7ffd55fc3000->0x7ffd55fc7000 at 0x00853000: load10 ALLOC LOAD READONLY HAS_CONTENTS
   [25]     0x7ffd55fc7000->0x7ffd55fc9000 at 0x00857000: load11 ALLOC LOAD READONLY CODE HAS_CONTENTS
   [26]     0xffffffffff600000->0xffffffffff601000 at 0x00859000: load12 ALLOC LOAD READONLY CODE HAS_CONTENTS
  (gdb)

Notice when the core file is first loaded we see two lines like:

  [New process 1]

And GDB reports:

  The current thread has terminated

Which isn't what we'd expect from a core file -- the core file should
only contain threads that are live at the point of the crash, one of
which should be the current thread.  The above message is reported
because GDB has deleted what we think is the current thread!

And in the 'info threads' output we are only seeing a single thread,
again, this is because GDB has deleted one of the threads.

Finally, the 'maintenance info sections' output shows the cause of all
our problems, two sections named .reg/0.  When GDB sees the first of
these it creates a new thread.  But, when we see the second .reg/0 GDB
tries to create another new thread, but this thread has the same
ptid_t as the first thread, so GDB deletes the first thread and
creates the second thread in its place.

Because both these threads are created with an lwpid of 0 GDB reports
these are 'New process NN' rather than 'New LWP NN' which is what we
would normally expect.

The previous commit includes a little more of the history of GDB
support in this area, but these problems were discussed on the mailing
list a while ago in this thread:

  https://inbox.sourceware.org/gdb-patches/AANLkTi=zuEDw6qiZ1jRatkdwHO99xF2Qu+WZ7i0EQjef@mail.gmail.com/

In this commit I propose a solution to these problems.

What I propose is that GDB should spot when we have .reg/0 sections
and, when these are found, should rename these sections using some
unique non-zero lwpid.

Note in the above output we also have sections like .reg2/0 and
.reg-xstate/0, these are additional register sets, this commit also
renumbers these sections inline with their .reg section.

The user is warned that some section renumbering has been performed.

GDB takes care to ensure that the new numbers assigned are unique and
don't clash with any of the pid's that might already be in use --
remember, in a real vmcore file, 0 is used to indicate an idle core,
non-idle cores will have the pid of whichever process was running on
that core, so we don't want GDB to assign an lwpid that clashes with
an actual pid that is in use in the core file.

After this commit here's the updated GDB session output:

  $ ./gdb/gdb --data-directory ./gdb/data-directory/ -q
  (gdb) core-file /tmp/x86_64-pid0-core.core
  warning: found threads with pid 0, assigned replacement Target Ids: LWP 1, LWP 2
  [New LWP 1]
  [New LWP 2]
  Failed to read a valid object file image from memory.
  Core was generated by `./segv-mt'.
  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  0x00000000004017c2 in ?? ()
  [Current thread is 1 (LWP 1)]
  (gdb) info threads
    Id   Target Id         Frame
  * 1    LWP 1             0x00000000004017c2 in ?? ()
    2    LWP 2             0x000000000040dda5 in ?? ()
  (gdb) maintenance info sections
  Core file: `/tmp/x86_64-pid0-core.core', file type elf64-x86-64.
   [0]      0x00000000->0x000012d4 at 0x00000318: note0 READONLY HAS_CONTENTS
   [1]      0x00000000->0x000000d8 at 0x0000039c: .reg/1 HAS_CONTENTS
   [2]      0x00000000->0x000000d8 at 0x0000039c: .reg HAS_CONTENTS
   [3]      0x00000000->0x00000080 at 0x0000052c: .note.linuxcore.siginfo/1 HAS_CONTENTS
   [4]      0x00000000->0x00000080 at 0x0000052c: .note.linuxcore.siginfo HAS_CONTENTS
   [5]      0x00000000->0x00000140 at 0x000005c0: .auxv HAS_CONTENTS
   [6]      0x00000000->0x000000a4 at 0x00000714: .note.linuxcore.file/1 HAS_CONTENTS
   [7]      0x00000000->0x000000a4 at 0x00000714: .note.linuxcore.file HAS_CONTENTS
   [8]      0x00000000->0x00000200 at 0x000007cc: .reg2/1 HAS_CONTENTS
   [9]      0x00000000->0x00000200 at 0x000007cc: .reg2 HAS_CONTENTS
   [10]     0x00000000->0x00000440 at 0x000009e0: .reg-xstate/1 HAS_CONTENTS
   [11]     0x00000000->0x00000440 at 0x000009e0: .reg-xstate HAS_CONTENTS
   [12]     0x00000000->0x000000d8 at 0x00000ea4: .reg/2 HAS_CONTENTS
   [13]     0x00000000->0x00000200 at 0x00000f98: .reg2/2 HAS_CONTENTS
   [14]     0x00000000->0x00000440 at 0x000011ac: .reg-xstate/2 HAS_CONTENTS
   [15]     0x00400000->0x00401000 at 0x00002000: load1 ALLOC LOAD READONLY HAS_CONTENTS
   [16]     0x00401000->0x004b9000 at 0x00003000: load2 ALLOC READONLY CODE
   [17]     0x004b9000->0x004e5000 at 0x00003000: load3 ALLOC READONLY
   [18]     0x004e6000->0x004ec000 at 0x00003000: load4 ALLOC LOAD HAS_CONTENTS
   [19]     0x004ec000->0x004f2000 at 0x00009000: load5 ALLOC LOAD HAS_CONTENTS
   [20]     0x012a8000->0x012cb000 at 0x0000f000: load6 ALLOC LOAD HAS_CONTENTS
   [21]     0x7fda77736000->0x7fda77737000 at 0x00032000: load7 ALLOC READONLY
   [22]     0x7fda77737000->0x7fda77f37000 at 0x00032000: load8 ALLOC LOAD HAS_CONTENTS
   [23]     0x7ffd55f65000->0x7ffd55f86000 at 0x00832000: load9 ALLOC LOAD HAS_CONTENTS
   [24]     0x7ffd55fc3000->0x7ffd55fc7000 at 0x00853000: load10 ALLOC LOAD READONLY HAS_CONTENTS
   [25]     0x7ffd55fc7000->0x7ffd55fc9000 at 0x00857000: load11 ALLOC LOAD READONLY CODE HAS_CONTENTS
   [26]     0xffffffffff600000->0xffffffffff601000 at 0x00859000: load12 ALLOC LOAD READONLY CODE HAS_CONTENTS
  (gdb)

Notice the new warning which is issued when the core file is being
loaded.  The threads are announced as '[New LWP NN]', and we see two
threads in the 'info threads' output.  The 'maintenance info sections'
output shows the result of the section renaming.

The gdb.arch/core-file-pid0.exp test has been update to check for the
improved GDB output.
2023-06-05 10:05:07 +01:00
Andrew Burgess
6cb39b064b gdb/testsuite: add test for core file with a 0 pid
This patch contains a test for this commit:

  commit c820c52a91
  Date:   Fri Aug 6 19:45:58 2010 +0000

              * thread.c (add_thread_silent): Use null_ptid instead of
              minus_one_ptid while getting rid of stale inferior_ptid.

This is another test that has been carried in the Fedora GDB tree for
some time, and I thought that it would be worth merging to master.  I
don't believe there is any test like this currently in the testsuite.

The original issue was reported in this thread:

  https://inbox.sourceware.org/gdb-patches/AANLkTi=zuEDw6qiZ1jRatkdwHO99xF2Qu+WZ7i0EQjef@mail.gmail.com/

The problem was that when GDB was used to open a vmcore (core file)
image generated by the Linux kernel GDB would (sometimes) crash with
an assertion failure:

  thread.c:884: internal-error: switch_to_thread: Assertion `inf != NULL' failed.

To understand what's going on we need some background; a vmcore file
represents each processor core in the same way that a standard
application core file represents threads.  Thus, we might say, a
vmcore file represents cores as threads.

When writing a vmcore file, the kernel will store the pid of the
process currently running on that core as the thread's lwpid.

However, if a core is idle, with no process currently running on it,
then the lwpid for that thread is stored as 0 in the vmcore file.  If
multiple cores are idle then multiple threads will have a lwpid of 0.

Back in 2010, the original issue reported tried to change the kernel's
behaviour in this thread:

  https://lkml.org/lkml/2010/8/3/75

This change was rejected by the kernel team, the current
behaviour (lwpid of 0) was considered correct.  I've checked the
source of a recent kernel.  The code mentioned in the lkml.org posting
has moved, it's now in the function crash_save_cpu in the file
kernel/kexec_core.c, but the general behaviour is unchanged, an idle
core will have an lwpid of 0, so I think GDB still needs to be able to
handle this case.

When GDB loads a vmcore file (which is handled just like any other
core file) the sections are processed in core_open to generate the
threads for the core file.  The processing is done by calling
add_to_thread_list, a function which looks for sections named .reg/NN
where NN is the lwpid of the thread, GDB then builds a ptid_t for the
new thread and calls add_thread.

Remember, in our case the lwpid is 0.  Now for the first thread this
is fine, if a little weird, 0 isn't usually a valid lwpid, but that's
OK, GDB creates a thread with lwpid of 0 and carries on.

When we find the next thread (core) with lwpid of 0, we attempt to
create another thread with an lwpid of 0.  This of course clashes with
the previously created thread, they have the same ptid_t, so GDB tries
to delete the first thread.

And it was within this thread delete code that we triggered a bug
which would then cause GDB to assert -- when deleting we tried to
switch to a thread with minus_one_ptid, this resulted in a call to
find_inferior_pid (passing in minus_one_ptid's pid, which is -1), the
find_inferior_pid call fails and returns NULL, which then triggered an
assert in switch_to_thread.

The actual details of the why the assert triggered are really not
important.  What's important (I think) is that a vmcore file might
have this interesting lwpid of 0 characteristic, which isn't something
we see in "normal" application core files, and it is this that I think
we should be testing.

Now, you might be thinking: isn't deleting the first thread the wrong
thing to do?  If the vmcore file has two threads that represent two
cores, and both have an lwpid of 0 (indicating both cores are idle),
then surely GDB should still represent this as two threads?  You're
not wrong.  This was mentioned by Pedro in the original GDB mailing
list thread here:

  https://inbox.sourceware.org/gdb-patches/201008061057.03037.pedro@codesourcery.com/

This is indeed a problem, and this problem is still present in GDB
today.  I plan to try and address this in a later commit, however,
this first commit is about getting a test in place to confirm that GDB
at a minimum doesn't crash when loading such a vmcore file.

And so, finally, what's in this commit?

This commit contains a new test.  The test doesn't actually contain a
vmcore file.  Instead I've created a standard application core file
that contains two threads, and then manually edited the core file to
set the lwpid of each thread to 0.

To further reduce the size of the core file (as it will be stored in
git), I've zeroed all of the LOAD-able segments in the core file.
This test really doesn't care about that part of the core file, we
only really care about loading the register's, this is enough to
confirm that the GDB doesn't crash.

Obviously as the core file is pre-generated, this test is architecture
specific.  There are already a few tests in gdb.arch/ that include
pre-generate core files.  Just as those existing tests do, I've
compressed the core file with bzip2, which reduces it to just 750
bytes.  I have structured the test so that if/when this patch is
merged I can add some additional core files for other architectures,
however, these are not included in this commit.

The test simply expands the core file, and then loads it into GDB.
One interesting thing to note is that GDB reports the core file
loading like this:

  (gdb) core-file ./gdb/testsuite/outputs/gdb.arch/core-file-pid0/core-file-pid0.x86-64.core
  [New process 1]
  [New process 1]
  Failed to read a valid object file image from memory.
  Core was generated by `./segv-mt'.
  Program terminated with signal SIGSEGV, Segmentation fault.
  The current thread has terminated
  (gdb)

There's two interesting things here: first, the repeated "New process
1" message.  This is caused because linux_core_pid_to_str reports
anything with an lwpid of 0 as a process, rather than an LWP.  And
second, the "The current thread has terminated" message.  This is
because the first thread in the core file is the current thread, but
when GDB loads the second thread (which also has lwpid 0) this causes
the first thread to be deleted, as a result GDB thinks that the
current (first) thread has terminated.

As I said previously, both of these problems are a result of the lwpid
0 aliasing, which is not being fixed in this commit -- this commit is
just confirming that GDB doesn't crash when loading this core file.
2023-06-05 09:53:10 +01:00
Andrew Burgess
a99db75d46 gdb: split inferior and thread setup when opening a core file
I noticed that in corelow.c, when a core file is opened, both the
thread and inferior setup is done in add_to_thread_list.  In this
patch I propose hoisting the inferior setup out of add_to_thread_list
into core_target_open.

The only thing about this change that gave me cause for concern is
that in add_to_thread_list, we only setup the inferior after finding
the first section with a name like ".reg/NN".  If we find no such
section then the inferior will never be setup.

Is this important?

Well, I don't think so.  Back in core_target_open, if there is no
current thread (which there will not be if no ".reg/NN" section was
found), then we look for a thread in the current inferior.  If there
are no threads (which there will not be if no ".reg/NN" is found),
then we once again setup the current inferior.

What I think this means, is that, in all cases, the current inferior
will end up being setup.  By moving the inferior setup code earlier in
core_target_open and making it non-conditional, we can remove the
later code that sets up the inferior, we now know this will always
have been done.

There should be no user visible changes after this commit.
2023-06-04 14:35:35 +01:00
Felix Willgerodt
e9683acf5e python, btrace: Fix some small formatting issues.
Reviewed-By: Tom Tromey <tom@tromey.com>
2023-05-31 09:27:17 +02:00
Tom de Vries
9d66d71cdd [gdb/tui] Fix fingerprint for cmd-only layout
I added a cmd-only layout:
...
(gdb) tui new-layout cmd cmd 1
...
and set it:
...
(gdb) layout cmd
...
which gave me the expect result: only the cmd window in the screen.

However, after going back to layout src:
...
(gdb) layout src
...
I got a source window with only one line in it, and the cmd window taking most
of the screen.

I traced this back to tui_set_layout, where for both the old and the new
layout the fingerprint of the cmd window in the layout is taken.  If the
fingerprint is the same, an effort will be done to preserve the command
window size.

The fingerprint is "VC" for both the old (cmd) and new (src) layouts, which
explains the behaviour.

I think this is essentially a bug in the finger print calculation, and it
should be "C" for the cmd layout.

Fix this by not adding a V or H in the fingerprint if the list size is one.

Tested on x86_64-linux.

Reviewed-By: Tom Tromey <tom@tromey.com>
2023-05-31 07:39:31 +02:00
GDB Administrator
2fee907cfd Automatic date update in version.in 2023-05-31 00:00:47 +00:00
Andrew Burgess
901682e4a4 gdb: add support for %V to printf command
This commit adds a new format for the printf and dprintf commands:
'%V'.  This new format takes any GDB expression and formats it as a
string, just as GDB would for a 'print' command, e.g.:

  (gdb) print a1
  $a = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
  (gdb) printf "%V\n", a1
  {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
  (gdb)

It is also possible to pass the same options to %V as you might pass
to the print command, e.g.:

  (gdb) print -elements 3 -- a1
  $4 = {2, 4, 6...}
  (gdb) printf "%V[-elements 3]\n", a1
  {2, 4, 6...}
  (gdb)

This new feature would effectively replace an existing feature of GDB,
the $_as_string builtin convenience function.  However, the
$_as_string function has a few problems which this new feature solves:

1. $_as_string doesn't currently work when the inferior is not
running, e.g:

  (gdb) printf "%s", $_as_string(a1)
  You can't do that without a process to debug.
  (gdb)

The reason for this is that $_as_string returns a value object with
string type.  When we try to print this we call value_as_address,
which ends up trying to push the string into the inferior's address
space.

Clearly we could solve this problem, the string data exists in GDB, so
there's no reason why we have to push it into the inferior, but this
is an existing problem that would need solving.

2. $_as_string suffers from the fact that C degrades arrays to
pointers, e.g.:

  (gdb) printf "%s\n", $_as_string(a1)
  0x404260 <a1>
  (gdb)

The implementation of $_as_string is passed a gdb.Value object that is
a pointer, it doesn't understand that it's actually an array.  Solving
this would be harder than issue #1 I think.  The whole array to
pointer transformation is part of our expression evaluation.  And in
most cases this is exactly what we want.  It's not clear to me how
we'd (easily) tell GDB that we didn't want this reduction in _some_
cases.  But I'm sure this is solvable if we really wanted to.

3. $_as_string is a gdb.Function sub-class, and as such is passed
gdb.Value objects.  There's no super convenient way to pass formatting
options to $_as_string.  By this I mean that the new %V feature
supports print formatting options.  Ideally, we might want to add this
feature to $_as_string, we might imagine it working something like:

  (gdb) printf "%s\n", $_as_string(a1,
                                   elements = 3,
                                   array_indexes = True)

where the first item is the value to print, while the remaining
options are the print formatting options.  However, this relies on
Python calling syntax, which isn't something that convenience
functions handle.  We could possibly rely on strictly positional
arguments, like:

  (gdb) printf "%s\n", $_as_string(a1, 3, 1)

But that's clearly terrible as there's far more print formatting
options, and if you needed to set the 9th option you'd need to fill in
all the previous options.

And right now, the only way to pass these options to a gdb.Function is
to have GDB first convert them all into gdb.Value objects, which is
really overkill for what we want.

The new %V format solves all these problems: the string is computed
and printed entirely on the GDB side, we are able to print arrays as
actual arrays rather than pointers, and we can pass named format
arguments.

Finally, the $_as_string is sold in the manual as allowing users to
print the string representation of flag enums, so given:

  enum flags
    {
      FLAG_A = (1 << 0),
      FLAG_B = (1 << 1),
      FLAG_C = (1 << 1)
    };

  enum flags ff = FLAG_B;

We can:

  (gdb) printf "%s\n", $_as_string(ff)
  FLAG_B

This works just fine with %V too:

  (gdb) printf "%V\n", ff
  FLAG_B

So all functionality of $_as_string is replaced by %V.  I'm not
proposing to remove $_as_string, there might be users currently
depending on it, but I am proposing that we don't push $_as_string in
the documentation.

As %V is a feature of printf, GDB's dprintf breakpoints naturally gain
access to this feature too.  dprintf breakpoints can be operated in
three different styles 'gdb' (use GDB's printf), 'call' (call a
function in the inferior), or 'agent' (perform the dprintf on the
remote).

The use of '%V' will work just fine when dprintf-style is 'gdb'.

When dprintf-style is 'call' the format string and arguments are
passed to an inferior function (printf by default).  In this case GDB
doesn't prevent use of '%V', but the documentation makes it clear that
support for '%V' will depend on the inferior function being called.

I chose this approach because the current implementation doesn't place
any restrictions on the format string when operating in 'call' style.
That is, the user might already be calling a function that supports
custom print format specifiers (maybe including '%V') so, I claim, it
would be wrong to block use of '%V' in this case.  The documentation
does make it clear that users shouldn't expect this to "just work"
though.

When dprintf-style is 'agent' then GDB does no support the use of
'%V' (right now).  This is handled at the point when GDB tries to
process the format string and send the dprintf command to the remote,
here's an example:

  Reading symbols from /tmp/hello.x...
  (gdb) dprintf call_me, "%V", a1
  Dprintf 1 at 0x401152: file /tmp/hello.c, line 8.
  (gdb) set sysroot /
  (gdb) target remote | gdbserver --once - /tmp/hello.x
  Remote debugging using | gdbserver --once - /tmp/hello.x
  stdin/stdout redirected
  Process /tmp/hello.x created; pid = 3088822
  Remote debugging using stdio
  Reading symbols from /lib64/ld-linux-x86-64.so.2...
  (No debugging symbols found in /lib64/ld-linux-x86-64.so.2)
  0x00007ffff7fd3110 in _start () from /lib64/ld-linux-x86-64.so.2
  (gdb) set dprintf-style agent
  (gdb) c
  Continuing.
  Unrecognized format specifier 'V' in printf
  Command aborted.
  (gdb)

This is exactly how GDB would handle any other invalid format
specifier, for example:

  Reading symbols from /tmp/hello.x...
  (gdb) dprintf call_me, "%Q", a1
  Dprintf 1 at 0x401152: file /tmp/hello.c, line 8.
  (gdb) set sysroot /
  (gdb) target remote | gdbserver --once - /tmp/hello.x
  Remote debugging using | gdbserver --once - /tmp/hello.x
  stdin/stdout redirected
  Process /tmp/hello.x created; pid = 3089193
  Remote debugging using stdio
  Reading symbols from /lib64/ld-linux-x86-64.so.2...
  (No debugging symbols found in /lib64/ld-linux-x86-64.so.2)
  0x00007ffff7fd3110 in _start () from /lib64/ld-linux-x86-64.so.2
  (gdb) set dprintf-style agent
  (gdb) c
  Continuing.
  Unrecognized format specifier 'Q' in printf
  Command aborted.
  (gdb)

The error message isn't the greatest, but improving that can be put
off for another day I hope.

Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Acked-By: Simon Marchi <simon.marchi@efficios.com>
2023-05-30 21:49:21 +01:00
Simon Marchi
ec517d1040 gdb: add interp::on_memory_changed method
Same idea as previous patches, but for memory_changed.

Change-Id: Ic19f20c24d8a6431d4a89c5625e8ef4898f76e82
2023-05-30 15:07:26 -04:00
Simon Marchi
3d654fa72d gdb: add interp::on_param_changed method
Same idea as previous patches, but for command_param_changed.

Change-Id: I7c2196343423360da05f016f8ffa871c064092bb
2023-05-30 15:07:26 -04:00
Simon Marchi
19081eb5f1 gdb: add interp::on_breakpoint_modified method
Same idea as previous patches, but for breakpoint_modified.

Change-Id: I4f0a9edea912de431e32451d74224b2022a7c328
2023-05-30 15:07:26 -04:00
Simon Marchi
e4239559f4 gdb: add interp::on_breakpoint_deleted method
Same idea as previous patches, but for breakpoint_deleted.

Change-Id: I59c231ce963491bb1eee1432ee1090138f09e19c
2023-05-30 15:07:26 -04:00
Simon Marchi
e7692320db gdb: add interp::on_breakpoint_created method
Same idea as previous patches, but for breakpoint_created.

Change-Id: I614113c924edc243590018b8fb3bf69cb62215ef
2023-05-30 15:07:26 -04:00
Simon Marchi
c27ec5c09f gdb: add interp::on_tsv_modified method
Same idea as previous patches, but for tsv_modified.

Change-Id: I55454a2386d5450040b3a353909b26f389a43682
2023-05-30 15:07:26 -04:00
Simon Marchi
f0dffaff4f gdb: add interp::on_tsv_deleted method
Same idea as previous patches, but for tsv_deleted.

Change-Id: I71b0502b493da7b6e293bee02aeca98de83d4b75
2023-05-30 15:07:26 -04:00
Simon Marchi
bf506f275a gdb: add interp::on_tsv_created method
Same idea as previous patches, but for tsv_created.

Change-Id: I9c30ecfdbd78ca015d613f43a0c0aef6c7eb32b5
2023-05-30 15:07:26 -04:00
Simon Marchi
0bc845fc98 gdb: add interp::on_traceframe_changed method
Same idea as previous patches, but for traceframe_changed.

Change-Id: Ia473f07d70d57b30aca0094d0e0585d7e0d95637
2023-05-30 15:07:26 -04:00
Simon Marchi
7603ea6afa gdb: add interp::on_about_to_proceed method
Same idea as previous patches, but for about_to_proceed.  We only need
(and want, as far as the mi_interp implementation is concerned) to
notify the interpreter that caused the proceed.

Change-Id: Id259bca10dbc3d43d46607ff7b95243a9cbe2f89
2023-05-30 15:07:26 -04:00
Simon Marchi
d711fe3b0f gdb: add interp::on_solib_unloaded method
Same idea as previous patches, but for solib_unloaded.

Change-Id: Iad847de93f0b38b5c90679a173d3beeaed7af6c5
2023-05-30 15:07:26 -04:00
Simon Marchi
f648548100 gdb: add interp::on_solib_loaded method
Same idea as previous patches, but for solib_loaded

Change-Id: I85edb0a4b377f4b2c39ffccf31cb75f38bae0f55
2023-05-30 15:07:26 -04:00
Simon Marchi
52d98df742 gdb: add interp::on_target_resumed method
Same idea as previous patches, but for target_resumed.

Change-Id: I66fa28d1d41a1f3c4fb0d6a470137d493eac3c8c
2023-05-30 15:07:26 -04:00
Simon Marchi
44fbffc69d gdb: add interp::on_record_changed method
Same idea as previous patches, but for record_changed

Change-Id: I5eeeacd703af8401c315060514c94e8e6439cc40
2023-05-30 15:07:26 -04:00
Simon Marchi
2646bfa763 gdb: add interp::on_inferior_removed method
Same idea as previous patches, but for inferior_removed.

Change-Id: I7971840bbbdcfabf77e2ded7584830c9dfdd10d0
2023-05-30 15:07:26 -04:00
Simon Marchi
d38086cce9 gdb: add interp::on_inferior_disappeared method
Same idea as previous patches, but for inferior_disappeared.

For symmetry with on_inferior_appeared, I named this one
on_inferior_disappeared, despite the observer being called
inferior_exit.  This is called when detaching an inferior, so I think
that calling it "disappeared" is a bit less misleading (the observer
should probably be renamed later).

Change-Id: I372101586bc9454997953c1e540a2a6685f53ef6
2023-05-30 15:07:26 -04:00
Simon Marchi
0c613e170e gdb: add interp::on_inferior_appeared method
Same idea as previous patches, but for inferior_appeared.

Change-Id: Ibe4feba34274549a886b1dfb5b3f8d59ae79e1b5
2023-05-30 15:07:26 -04:00
Simon Marchi
023c6d45d7 gdb: add interp::on_inferior_added method
Same idea as previous patches, but for inferior_added.

mi_interp::init avoided using mi_inferior_added, since, as the comment
used to say, it would notify all MI interpreters.  Now, it's easy to
only notify the new interpreter, so it's possible to just call the
on_inferior_added method in mi_interp::init.

Change-Id: I0eddbd5367217d1c982516982089913019ef309f
2023-05-30 15:07:26 -04:00
Simon Marchi
8e7af84345 gdb: add interp::on_thread_exited method
Same idea as previous patches, but for thread_exited.

Change-Id: I4be974cbe58cf635453fef503c2d77c82522cbd9
2023-05-30 15:07:26 -04:00
Simon Marchi
30e7e0a917 gdb: add interp::on_new_thread method
Same idea as previous patches, but for new_thread.

Change-Id: Ib70ae3421b736fd69d86c4e7c708bec349aa256c
2023-05-30 15:07:26 -04:00
Simon Marchi
77cd03e27c gdb: add interp::on_user_selected_context_changed method
Same as previous patches, but for user_selected_context_changed.

Change-Id: I40de15be897671227d4bcf3e747f0fd595f0d5be
2023-05-30 15:07:26 -04:00
Simon Marchi
2736b77153 gdb: add interp::on_command_error method
Same idea as the previous patches, but for command_error.

Change-Id: If6098225dd72fad8be13b3023b35bc8bc48efb9d
2023-05-30 15:07:26 -04:00
Simon Marchi
c3d321de3f gdb: add interp::on_sync_execution_done method
Same as previous patches, but for sync_execution_done.  Except that
here, we only want to notify the interpreter that is executing the
command, not all interpreters.

Change-Id: I729c719447b5c5f29af65dbf6fed9132e2cd308b
2023-05-30 15:07:26 -04:00
Simon Marchi
2e5dbfab56 gdb: add interp::on_no_history method
Same as previous patches, but for no_history.

Change-Id: I06930fe7cb4082138c6c5496c5118fe4951c10da
2023-05-30 15:07:26 -04:00
Simon Marchi
bf64d1d5bf gdb: add interp::on_exited method
Same as previous patch, but for exited.  Remove the exited observable,
since nothing uses it anymore, and we don't have anything coming that
will use it.

Change-Id: I358cbea0159af56752dfee7510d6a86191e722bb
2023-05-30 15:07:26 -04:00
Simon Marchi
d6bd2ef5f4 gdb: add interp::on_signal_exited method
Same as previous patch, but for signal_exited.  Remove the signal_exited
observable, since nothing uses it anymore, and we don't have anything
coming that will use it.

Change-Id: I0dca1eab76338bf27be755786e3dad3241698b10
2023-05-30 15:07:26 -04:00
Simon Marchi
8782926771 gdb: add interp::on_normal_stop method
Same idea as the previous patch, but for the normal_stop event.

Change-Id: I4fc8ca8a51c63829dea390a2b6ce30b77f9fb863
2023-05-30 15:07:26 -04:00
Simon Marchi
3f75a984d2 gdb: add interp::on_signal_received method
Instead of having the interpreter code registering observers for the
signal_received observable, add a "signal_received" virtual method to
struct interp.  Add a interps_notify_signal_received function that loops
over all UIs and calls the signal_received method on the interpreter.
Finally, add a notify_signal_received function that calls
interps_notify_signal_received and then notifies the observers.  Replace
all existing notifications to the signal_received observers with calls
to notify_signal_received.

Before this patch, the CLI and MI code both register a signal_received
observer.  These observer go over all UIs, and, for those that have a
interpreter of the right kind, print the stop notifiation.

After this patch, we have just one "loop over all UIs", inside
interps_notify_signal_received.  Since the interp::on_signal_received
method gets called once for each interpreter, the implementations only
need to deal with the current interpreter (the "this" pointer).

The motivation for this patch comes from a future patch, that makes the
amdgpu code register an observer to print a warning after the CLI's
signal stop message.  Since the amdgpu and the CLI code both use
observers, the order of the two messages is not stable, unless we define
the priority using the observer dependency system.  However, the
approach of using virtual methods on the interpreters seems like a good
change anyway, I think it's more straightforward and simple to
understand than the current solution that uses observers.  We are sure
that the amdgpu message gets printed after the CLI message, since
observers are notified after interpreters.

Keep the signal_received, even if nothing uses if, because we will be
using it in the upcoming amdgpu patch implementing the warning described
above.

Change-Id: I4d8614bb8f6e0717f4bfc2a59abded3702f23ac4
2023-05-30 15:07:26 -04:00
Tom de Vries
37d9880d65 [gdb] Mention --with/without-system-readline for --configuration
Simon reported that the new test-case gdb.tui/pr30056.exp fails with system
readline.

This is because the test-case requires a fix in readline that's present in our
in-repo copy of readline, but most likely not in any system readline yet.

Fix this by:
- mentioning --with-system-readline or --without-system-readline in the
  configuration string.
- adding a new proc with_system_readline that makes this information available
  in the testsuite.
- using this in test-case gdb.tui/pr30056.exp to declare it unsupported for
  --with-system-readline.

Tested on x86_64-linux.

Reported-By: Simon Marchi <simon.marchi@efficios.com>
Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-05-30 17:49:31 +02:00
Nick Clifton
35f8b0595f Slight wording improvement for the -Ur documentation 2023-05-30 16:12:18 +01:00
Nick Clifton
10d6a2b1a0 Improve header information displayed with objdump -P for PE binaries.
* od-pe.c (targ_info): New array.
  (get_target_specific_info): New function.
  (decode_machine_number): Retire.  Use get_target_specific_info instead.
  (is_pe_object_magic): Likewise.
  (dump_pe_file_header): Display more information.
  Rework layout to be similar to that from 'objdump -p'.
  Add code to handle larger than normnal AOUT headers.
2023-05-30 15:43:24 +01:00
mengqinggang
2bd766d624 LoongArch: ld: Add support for linker relaxation.
Add ld relax support and testsuits.

ld/ChangeLog:

	* emultempl/loongarchelf.em: Regenerated.
	* testsuite/ld-elf/compressed1d.d: Xfail loongarch*-*.
	* testsuite/ld-elf/pr26936.d: Likewise.
	* testsuite/ld-loongarch-elf/disas-jirl.d: Regenerated.
	* testsuite/ld-loongarch-elf/disas-jirl-32.d: Regenerated.
	* testsuite/ld-loongarch-elf/jmp_op.d: Likewise.
	* testsuite/ld-loongarch-elf/macro_op.d: Likewise.
	* testsuite/ld-loongarch-elf/macro_op_32.d: Likewise.
	* testsuite/ld-loongarch-elf/relax-align.dd: New test.
	* testsuite/ld-loongarch-elf/relax-align.s: New test.
	* testsuite/ld-loongarch-elf/relax.exp: New test.
	* testsuite/ld-loongarch-elf/relax.s: New test.
	* testsuite/ld-loongarch-elf/uleb128.dd: New test.
	* testsuite/ld-loongarch-elf/uleb128.s: New test.
2023-05-30 19:56:48 +08:00
mengqinggang
56576f4a72 LoongArch: gas: Add support for linker relaxation.
Add gas -mrelax and -mno-relax option.
Add R_LARCH_RELAX reloc for instrction if it can be relaxed.
ADD R_LARCH_ALIGN reloc for align pseudo instruction because relax.
Add ADD/SUB reloc pair for debug and exception data to calculate symbol
substraction because relax.

gas/ChangeLog:

	* config/tc-loongarch.c:
	(struct loongarch_cl_insn): New macro_id member.
	(enum options): New OPTION_RELAX and OPTION_NO_RELAX.
	(struct option): New mrelax and mno-relax.
	(md_parse_option): Likewise.
	(get_internal_label):
	(loongarch_args_parser_can_match_arg_helper): Generate relax reloc.
	(move_insn): Set fx_frag and fx_where if exist.
	(append_fixp_and_insn): Call frag_wane and frag_new for linker relax
	relocs.
	(loongarch_assemble_INSNs): New loongarch_cl_insn pointer parameter.
	(md_assemble): Fix function call.
	(fix_reloc_insn): Likewise.
	(md_apply_fix): Generate ADD/SUB reloc pair for debug and exception
	data.
	(loongarch_fix_adjustable): Delete.
	(md_convert_frag): Generate new fix.
	(loongarch_pre_output_hook): New function.
	(loongarch_make_nops): Likewise.
	(loongarch_frag_align_code): Likewise.
	(loongarch_insert_uleb128_fixes): Likewise.
	(loongarch_md_finish): Likewise.
	* config/tc-loongarch.h
	(md_allow_local_subtract): New macro define.
	(loongarch_frag_align_code): New declare.
	(md_do_align): Likewise.
	(loongarch_fix_adjustable): Delete.
	(tc_fix_adjustable): New macro define.
	(TC_FORCE_RELOCATION_SUB_SAME): Likewise.
	(TC_LINKRELAX_FIXUP): Likewise.
	(TC_FORCE_RELOCATION_LOCAL): Likewise.
	(DWARF2_USE_FIXED_ADVANCE_PC): Likewise.
	(MD_APPLY_SYM_VALUE): Likewise.
	(tc_symbol_new_hook): New extern.
	(NOP_OPCODE): Delete.
	(loongarch_pre_output_hook): New macro define.
	(md_pre_output_hook): Likewise.
	(md_finish): Likewise.
	(loongarch_md_finish): New extern.
	* testsuite/gas/all/align.d: Mark as unsupported on LoongArch.
	* testsuite/gas/all/gas.exp: Xfail loongarch*-*.
	* testsuite/gas/all/relax.d: Likewise.
	* testsuite/gas/elf/dwarf-5-irp.d: Likewise.
	* testsuite/gas/elf/dwarf-5-loc0.d: Likewise.
	* testsuite/gas/elf/dwarf-5-macro-include.d: Likewise.
	* testsuite/gas/elf/dwarf-5-macro.d: Likewise.
	* testsuite/gas/elf/dwarf2-11.d: Likewise.
	* testsuite/gas/elf/dwarf2-15.d: Likewise.
	* testsuite/gas/elf/dwarf2-16.d: Likewise.
	* testsuite/gas/elf/dwarf2-17.d: Likewise.
	* testsuite/gas/elf/dwarf2-18.d: Likewise.
	* testsuite/gas/elf/dwarf2-19.d: Likewise.
	* testsuite/gas/elf/dwarf2-5.d: Likewise.
	* testsuite/gas/elf/ehopt0.d: Likewise.
	* testsuite/gas/elf/elf.exp: Likewise.
	* testsuite/gas/elf/section11.d: Likewise.
	* testsuite/gas/lns/lns.exp: Likewise.
	* testsuite/gas/loongarch/jmp_op.d: Regenerated.
	* testsuite/gas/loongarch/li.d: Likewise.
	* testsuite/gas/loongarch/macro_op.d: Likewise.
	* testsuite/gas/loongarch/macro_op_32.d: Likewise.
	* testsuite/gas/loongarch/macro_op_large_abs.d: Likewise.
	* testsuite/gas/loongarch/macro_op_large_pc.d: Likewise.
	* testsuite/gas/loongarch/relax_align.d: New test.
	* testsuite/gas/loongarch/relax_align.s: New test.
	* testsuite/gas/loongarch/uleb128.d: New test.
	* testsuite/gas/loongarch/uleb128.s: New test.
2023-05-30 19:56:46 +08:00
mengqinggang
7624446235 LoongArch: binutils: Add support for linker relaxation.
Add support for relocs related to relax to readelf.

binutils/ChangeLog:

	* readelf.c (target_specific_reloc_handling): Handle ULEB128 reloc.
	(is_32bit_inplace_add_reloc): Handle new reloc.
	(is_32bit_inplace_sub_reloc): Likewise.
	(is_64bit_inplace_add_reloc): Likewise.
	(is_64bit_inplace_sub_reloc): Likewise.
	(is_16bit_inplace_add_reloc): Likewise.
	(is_16bit_inplace_sub_reloc): Likewise.
	(is_8bit_inplace_add_reloc): Likewise.
	(is_8bit_inplace_sub_reloc): Likewise.
	(is_6bit_inplace_sub_reloc): Likewise.
	(is_6bit_inplace_add_reloc): New function.
	(apply_relocations): Handle new reloc.
	* testsuite/binutils-all/readelf.exp: Add -mno-relax option
	for LoongArch.
2023-05-30 19:56:45 +08:00
mengqinggang
7ad9de1188 LoongArch: opcodes: Add support for linker relaxation.
Set gas default to enable relax.

opcodes/ChangeLog:

	* loongarch-opc.c (struct loongarch_ASEs_option): New member relax
	with the default value 1.
2023-05-30 19:56:43 +08:00
mengqinggang
1b6fccd28d LoongArch: bfd: Add support for linker relaxation.
Add relax support and related relocs in bfd.

bfd/ChangeLog:

	* bfd-in2.h: Add relocs related to relax.
	* elfnn-loongarch.c (struct loongarch_elf_link_hash_table): New integer
	pointer (data_segment_phase) to monitor the data segment phase.
	(loongarch_elf_check_relocs): Swap B21/B26 reloc sequence.
	(loongarch_elf_adjust_dynamic_symbol): Fix code format.
	(loongarch_reloc_rewrite_imm_insn): Fix function call.
	(perform_relocation): Handle new relocs related to relax.
	(RELOCATE_CALC_PC32_HI20): Fix code format.
	(RELOCATE_CALC_PC64_HI32): Likewise.
	(loongarch_elf_relocate_section): Handle new relocs related to relax.
	(loongarch_relax_delete_bytes): New function.
	(loongarch_relax_pcala_addi): Likewise.
	(loongarch_relax_pcala_ld): Likewise.
	(bfd_elfNN_loongarch_set_data_segment_info): Likewise.
	(loongarch_relax_align): Likewise.
	(loongarch_elf_relax_section): Likewise.
	(bfd_elfNN_bfd_relax_section): New macro define.
	* elfxx-loongarch.c (reloc_bits): New bfd point parameter.
	(reloc_bits_b16): Likewise.
	(reloc_bits_b21): Likewise.
	(reloc_bits_b26): Likewise.
	(loongarch_adjust_reloc_bitsfield): Likewise.
	(reloc_bits_pcrel20_s2): New function.
	(loongarch_elf_add_sub_reloc): Likewise.
	(loongarch_elf_add_sub_reloc_uleb128): Likewise.
	(loongarch_write_unsigned_leb128): New function.
	* elfxx-loongarch.h (loongarch_adjust_reloc_bitsfield): New bfd point
	parameter.
	(bfd_elf32_loongarch_set_data_segment_info): New declare.
	(bfd_elf64_loongarch_set_data_segment_info): Likewise.
	(loongarch_write_unsigned_leb128): Likewise.
	* libbfd.h: Add relocs related to relax.
	* reloc.c: Add relocs related to relax.
2023-05-30 19:56:41 +08:00
mengqinggang
57a930e3bf LoongArch: include: Add support for linker relaxation.
Add relocs and gas LARCH_opts.relax option.

include/ChangeLog:

	* elf/loongarch.h: Add relocs.
	* opcode/loongarch.h: Add LARCH_opts.relax and macro LARCH_NOP.
2023-05-30 19:56:40 +08:00
Nick Clifton
7b9e7db083 Add support for an ARMMAGIC value of 0xa00 to the PE dumper. 2023-05-30 11:42:16 +01:00
Alan Modra
796029320e arm-pe objdump -P
arm-pe looks to be a very old PE implementation, incompatible with
current arm-wince-pe.  arm-pe has different relocations and uses
ARMMAGIC which has this comment: "I just made this up".  Well, OK, I
don't know the history but it was probably before Microsoft "just made
up" their constants for ARM windows CE.

This patch supports objdump -P for arm-pe, and another magic constant
that may appear in object files.  (I don't think binutils generates
files using ARMV7PEMAGIC aka IMAGE_FILE_MACHINE_ARMNT.)

	* od-pe.c (is_pe_object_magic): Handle IMAGE_FILE_MACHINE_ARMNT
	and ARMMAGIC.
2023-05-30 12:59:55 +09:30
Alan Modra
20c52e689a Define IMAGE_FILE_MACHINE_ARMNT
Same value as ARMV7PEMAGIC.
https://learn.microsoft.com/en-us/windows/win32/sysinfo/image-file-machine-constants

	* coff/pe.h (IMAGE_FILE_MACHINE_ARMNT): Define.
2023-05-30 12:51:57 +09:30
Alan Modra
0b32bdabdc Don't define COFF_MAGIC
This macro was unused apart from aout/encap.h, which has been deleted.

	* config/tc-arm.h (COFF_MAGIC): Don't define.
	* config/tc-sh.h (COFF_MAGIC): Don't define.
	* config/tc-z80.h (COFF_MAGIC): Don't define.
	* config/tc-z8k.h (COFF_MAGIC): Don't define.
2023-05-30 12:51:39 +09:30
Alan Modra
e193947c3b Delete include/aout/encap.h
This file is unused and as the header comment says, obsolete.
2023-05-30 12:51:22 +09:30
Alan Modra
9fa2b95974 Regen binutils POTFILES.in
for od-pe.c
2023-05-30 12:51:07 +09:30