Commit Graph

550 Commits

Author SHA1 Message Date
Kévin Le Gouguec
752e03a7c1 gdb: Fix Windows build after #include shuffle
Without this patch, the build chokes on:

    ../../src/gdb/windows-nat.c:384:21: error: field 'm_debug_event_pending' has incomplete type 'std::atomic<bool>'
      384 |   std::atomic<bool> m_debug_event_pending { false };
          |                     ^~~~~~~~~~~~~~~~~~~~~
    In file included from […gcc tree…]/include/c++/13.2.1/bits/shared_ptr_atomic.h:33,
                     from […gcc tree…]/include/c++/13.2.1/memory:81,
                     from ../../src/gdb/../gdbsupport/gdb_unique_ptr.h:23,
                     from ../../src/gdb/../gdbsupport/common-utils.h:26,
                     from ../../src/gdb/../gdbsupport/common-defs.h:199,
                     from ./../../src/gdb/defs.h:26,
                     from <command-line>:
    […gcc tree…]/include/c++/13.2.1/bits/atomic_base.h:174:12: note: declaration of 'struct std::atomic<bool>'
      174 |     struct atomic;
          |            ^~~~~~
    make.exe[2]: *** [Makefile:1947: windows-nat.o] Error 1

Presumably windows-nat.c relied on objfiles.h including <atomic>,
which was undone in 2024-05-16 "gdb: remove unused includes in
objfiles.{c,h}" (f617661c11).
2024-05-20 18:47:33 +02:00
Pedro Alves
43ca4ec299 gdb/Cygwin: Fix attach pid error message
On Cygwin, with "attach PID":

 - GDB first tries to interpret PID as a Windows native PID, and tries
   to attach to that.

 - if the attach fails, GDB then tries to interpret the PID as a
   Cygwin PID, and attach to that.

If converting the user-provided PID from a Cygwin PID to a Windows PID
fails, you get this:

 (gdb) attach 12345
 Can't attach to process 0 (error 2: The system cannot find the file specified.)

Note "process 0".

With the fix in this commit, we'll now get:

 (gdb) attach 12345
 Can't attach to process 12345 (error 2: The system cannot find the file specified.)

I noticed this while looking at gdb.log after running
gdb.base/attach.exp on Cygwin.

Change-Id: I05b9dc1f3a634a822ea49bb5c61719f5e62c8514
Approved-By: Luis Machado <luis.machado@arm.com>
2024-04-29 16:20:46 +01:00
Pedro Alves
e0139e5b03 Windows: Fix run/attach hang after bad run/attach
On Cygwin, gdb.base/attach.exp exposes that an "attach" after a
previously failed "attach" hangs:

 (gdb) PASS: gdb.base/attach.exp: do_attach_failure_tests: attach to digits-starting nonsense is prohibited
 attach 0
 Can't attach to process 0 (error 2: The system cannot find the file specified.)
 (gdb) PASS: gdb.base/attach.exp: do_attach_failure_tests: attach to nonexistent process is prohibited
 attach 10644
 FAIL: gdb.base/attach.exp: do_attach_failure_tests: first attach (timeout)

The problem is that windows_nat_target::attach always returns success
even if the attach fails.  When we return success, the helper thread
begins waiting for events (which will never come), and thus the next
attach deadlocks on the do_synchronously call within
windows_nat_target::attach.

"run" has the same problem, which is exposed by the new
gdb.base/run-fail-twice.exp testcase added in a following patch:

 (gdb) run
 Starting program: .../gdb.base/run-fail-twice/run-fail-twice.nox
 Error creating process .../gdb.base/run-fail-twice/run-fail-twice.nox, (error 6: The handle is invalid.)
 (gdb) PASS: gdb.base/run-fail-twice.exp: test: bad run 1
 run
 Starting program: .../gdb.base/run-fail-twice/run-fail-twice.nox
 FAIL: gdb.base/run-fail-twice.exp: test: bad run 2 (timeout)

The problem here is the same, except that this time it is
windows_nat_target::create_inferior that returns the incorrect result.

This commit fixes both the "attach" and "run" paths, and the latter
both the Cygwin and MinGW paths.  The tests mentioned above now pass
on Cygwin.  Confirmed the fixes manually for MinGW GDB.

Change-Id: I15ec9fa279aff269d4982b00f4ea7c25ae917239
Approved-By: Tom Tromey <tom@tromey.com>
2024-04-26 21:22:46 +01:00
Simon Marchi
5b9707eb87 gdb: remove gdbcmd.h
Most files including gdbcmd.h currently rely on it to access things
actually declared in cli/cli-cmds.h (setlist, showlist, etc).  To make
things easy, replace all includes of gdbcmd.h with includes of
cli/cli-cmds.h.  This might lead to some unused includes of
cli/cli-cmds.h, but it's harmless, and much faster than going through
the 170 or so files by hand.

Change-Id: I11f884d4d616c12c05f395c98bbc2892950fb00f
Approved-By: Tom Tromey <tom@tromey.com>
2024-04-25 12:59:02 -04:00
Pedro Alves
56f4dea207 gdb/Windows: Fix detach while running
While testing a WIP Cygwin GDB that supports non-stop, I noticed that
gdb.threads/attach-non-stop.exp exposes that this:

 (gdb) attach PID&
 ...
 (gdb) detach

... hangs.

And it turns out that it hangs in all-stop as well.  This commits
fixes that.

After "attach &", the target is set running, we've called
ContinueDebugEvent and the process_thread thread is waiting for
WaitForDebugEvent events.  It is the equivalent of "attach; c&".

In windows_nat_target::detach, the first thing we do is
unconditionally call windows_continue (for ContinueDebugEvent), which
blocks in do_synchronously, until the process_thread sees an event out
of WaitForDebugEvent.  Unless the inferior happens to run into a
breakpoint, etc., then this hangs indefinitely.

If we've already called ContinueDebugEvent earlier, then we shouldn't
be calling it again in ::detach.

Still in windows_nat_target::detach, we have an interesting issue that
ends up being the bulk of the patch -- only the process_thread thread
can call DebugActiveProcessStop, but if it is blocked in
WaitForDebugEvent, we need to somehow force it to break out of it.
The only way to do that, is to force the inferior to do something that
causes WaitForDebugEvent to return some event.

This patch uses CreateRemoteThread to do it, which results in
WaitForDebugEvent reporting CREATE_THREAD_DEBUG_EVENT.  We then
terminate the injected thread before it has a chance to run any
userspace code.

Note that Win32 functions like DebugBreakProcess and
GenerateConsoleCtrlEvent would also inject a new thread in the
inferior.  I first used DebugBreakProcess, but that is actually more
complicated to use, because we'd have to be sure to consume the
breakpoint event before detaching, otherwise the inferior would likely
die due a breakpoint exception being raised with no debugger around to
intercept it.

See the new break_out_process_thread method.

So the fix has two parts:

 - Keep track of whether we've called ContinueDebugEvent and the
   process_thread thread is waiting for events, or whether
   WaitForDebugEvent already returned an event.

 - In windows_nat_target::detach, if the process_thread thread is
   waiting for events, unblock out of its WaitForDebugEvent, before
   proceeding with the actual detach.

New test included.  Passes cleanly on GNU/Linux native and gdbserver,
and also passes cleanly on Cygwin and MinGW, with the fix.  Before the
fix, it would hang and fail with a timeout.

Tested-By: Hannes Domani <ssbssa@yahoo.de>
Reviewed-By: Tom Tromey <tom@tromey.com>
Change-Id: Ifb91c58c08af1a9bcbafecedc93dfce001040905
2024-04-17 18:57:01 +01:00
Simon Marchi
18d2988e5d gdb, gdbserver, gdbsupport: remove includes of early headers
Now that defs.h, server.h and common-defs.h are included via the
`-include` option, it is no longer necessary for source files to include
them.  Remove all the inclusions of these files I could find.  Update
the generation scripts where relevant.

Change-Id: Ia026cff269c1b7ae7386dd3619bc9bb6a5332837
Approved-By: Pedro Alves <pedro@palves.net>
2024-03-26 21:13:22 -04:00
Pedro Alves
ccf3148e31 Fix windows_nat_target::fake_create_process ptid
While working on Windows non-stop mode, I managed to introduce a bug
that led to fake_create_process being called.  That then resulted in
GDB crashes later on, because fake_create_process added a thread with
an incorrect ptid for this target.  It is putting dwThreadId in the
tid field of the ptid instead of on the lwp field.  This is fixed by
this patch.

Change-Id: Iaee5d2deaa57c501f7e6909f8ac242af9b183215
2024-03-25 19:29:53 +00:00
Pedro Alves
e9315f148d windows-nat: Use gdb_realpath
Use gdb_realpath instead of realpath in windows-nat.c:windows_make_so,
so that we don't have to manually call free.

Approved-By: John Baldwin <jhb@FreeBSD.org>
Change-Id: Id3cda7e177ac984c9a5f7c23f354e72bd561edff
2024-03-22 19:46:59 +00:00
Pedro Alves
30512efab1 windows-nat: Remove SO_NAME_MAX_PATH_SIZE limit
There is no need to limit shared library path sizes to
SO_NAME_MAX_PATH_SIZE nowadays.  windows_solib::name and
windows_solib::original_name are std::strings nowadays, and so are
solib::so_name and solib::so_original_name in the core solib code.

This commit reworks the code to remove that limit.  This also fixes a
leak where we were not releasing 'rname' in the realpath branch if the
'rname' string was larger than SO_NAME_MAX_PATH_SIZE.

Note: I tested the cygwin_conv_path with a manual hack to force that
path, and then stepping through the code.  You only get to that path
if Windows doesn't report an absolute path for ntdll.dll, and on my
machine (running Windows 10), it always does.

Approved-By: John Baldwin <jhb@FreeBSD.org>
Change-Id: I79e9862d5a7646eebfef7ab5b05b96318a7ca0c5
2024-03-22 19:46:59 +00:00
Pedro Alves
092ff48583 Simplify windows-nat.c:windows_make_so #ifdefery
There are two separate #ifndef __CYGWIN__/#else/#endif sections in the
windows_make_so function with 3 lines of shared code separating them.
I find this makes the code harder to understand than necessary.
AFAICS, there is no reason those three shared lines need to be after
the first #ifdef block.  There is no early return, nor are 'load_addr'
nor 'name' modified.

This commit moves that shared code to the top of the function, and
then combines the two #ifndef sections.

Approved-By: John Baldwin <jhb@FreeBSD.org>
Change-Id: If2678b52836b1c3134a5e9f9fdaee74448d8b7bc
2024-03-22 19:46:59 +00:00
Jon Turney
e433bca484 Drop special way of getting inferior context after a Cygwin signal
Simplify Cygwin signal handling by dropping the special way of getting
inferior context after a Cygwin signal.

I think the reason this existed was because previously we were not
able to unwind through the alternate stack used by _sigfe frames, so
without the hint of the "user" code IP, the backtrace from a signal
was unusable.

Now we can unwind through _sigfe frames, drop all this complexity.

(Restoring this specially obtained context to the inferior (as the
code currently does) skips over the actual signal delivery and
handling.  Cygwin has carried for a long time a patch which clears the
ContextFlags in the signal context, so we never attempt to restore it
to the inferior, but that interfers with gdb's ability to modify that
context e.g. if it decides it wants to turn on FLAG_TRACE_BIT.)

Change-Id: I214edd5a99fd17c1a31ad18138d4a6cc420225e3
2024-02-23 16:16:18 +00:00
Simon Marchi
85e8a786f4 gdb: add program_space parameter to disable_breakpoints_in_shlibs
Make the current_program_space reference bubble up one level.

Change-Id: Ide917aa306bff1872d961244901d79f65d2da62e
Approved-By: Andrew Burgess <aburgess@redhat.com>
2024-02-09 11:09:55 -05:00
Tom Tromey
b36a26343a Use reference result of emplace_back
Starting with C++17, emplace_back returns a reference to the new
object.  This patch changes code that uses emplace_back followed by a
call to back() to simply use this reference instead.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
2024-02-04 15:37:55 -07:00
Tom Tromey
a197d5f7eb Really fix Windows gdbserver segment registers
My earlier attempt to mask the segment registers in gdbserver for
Windows introduced some bugs.  That patch is here:

https://sourceware.org/pipermail/gdb-patches/2023-December/205306.html

The problem turned out to be that these fields in the Windows
'CONTEXT' type are just 16 bits, so while masking the values on read
is fine, writing the truncated values back then causes zeros to be
written to some subsequent field.

This patch cleans this up by arranging never to write too much data to
a field.

I also noticed that two register numbers here were never updated for
the 64-bit port.  This patch fixes this as well, and renames the "FCS"
register to follow gdb.

Finally, this patch applies the same treatment to windows-nat.c.

Reviewed-by: John Baldwin <jhb@FreeBSD.org>
2024-01-30 10:18:38 -07:00
Andrew Burgess
1d506c26d9 Update copyright year range in header of all files managed by GDB
This commit is the result of the following actions:

  - Running gdb/copyright.py to update all of the copyright headers to
    include 2024,

  - Manually updating a few files the copyright.py script told me to
    update, these files had copyright headers embedded within the
    file,

  - Regenerating gdbsupport/Makefile.in to refresh it's copyright
    date,

  - Using grep to find other files that still mentioned 2023.  If
    these files were updated last year from 2022 to 2023 then I've
    updated them this year to 2024.

I'm sure I've probably missed some dates.  Feel free to fix them up as
you spot them.
2024-01-12 15:49:57 +00:00
Tom Tromey
d182e39881 Use C++17 [[fallthrough]] attribute
This changes gdb to use the C++17 [[fallthrough]] attribute rather
than special comments.

This was mostly done by script, but I neglected a few spellings and so
also fixed it up by hand.

I suspect this fixes the bug mentioned below, by switching to a
standard approach that, presumably, clang supports.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=23159
Approved-By: John Baldwin <jhb@FreeBSD.org>
Approved-By: Luis Machado <luis.machado@arm.com>
Approved-By: Pedro Alves <pedro@palves.net>
2023-11-29 14:29:43 -07:00
Tom Tromey
602971b386 Introduce throw_winerror_with_name
This introduces throw_winerror_with_name, a Windows analog of
perror_with_name, and changes various places in gdb to call it.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30770
2023-11-27 12:55:14 -07:00
Lancelot Six
6b09f1342c gdb: Replace gdb::optional with std::optional
Since GDB now requires C++17, we don't need the internally maintained
gdb::optional implementation.  This patch does the following replacing:
  - gdb::optional -> std::optional
  - gdb::in_place -> std::in_place
  - #include "gdbsupport/gdb_optional.h" -> #include <optional>

This change has mostly been done automatically.  One exception is
gdbsupport/thread-pool.* which did not use the gdb:: prefix as it
already lives in the gdb namespace.

Change-Id: I19a92fa03e89637bab136c72e34fd351524f65e9
Approved-By: Tom Tromey <tom@tromey.com>
Approved-By: Pedro Alves <pedro@palves.net>
2023-11-21 11:52:35 +00:00
Simon Marchi
9c742269ec gdb: remove get_current_regcache
Remove get_current_regcache, inlining the call to get_thread_regcache in
callers.  When possible, pass the right thread_info object known from
the local context.  Otherwise, fall back to passing `inferior_thread ()`.

This makes the reference to global context bubble up one level, a small
step towards the long term goal of reducing the number of references to
global context (or rather, moving those references as close as possible
to the top of the call tree).

No behavior change expected.

Change-Id: Ifa6980c88825d803ea586546b6b4c633c33be8d6
2023-11-17 20:01:37 +00:00
Simon Marchi
d6ac292e5f gdb: remove two uses of obstack
Remove uses of obstack in the code generating the libraries XML for
Windows and AIX.  Use std::string instead.  I'm not able to test this
change, unfortunately.

Change-Id: I28480913337e3fe8d6c31e551626931e6b1367ef
Approved-By: Tom Tromey <tom@tromey.com>
2023-11-16 21:26:45 -05:00
Simon Marchi
99d9c3b92c gdb: remove target_gdbarch
This function is just a wrapper around the current inferior's gdbarch.
I find that having that wrapper just obscures where the arch is coming
from, and that it's often used as "I don't know which arch to use so
I'll use this magical target_gdbarch function that gets me an arch" when
the arch should in fact come from something in the context (a thread,
objfile, symbol, etc).  I think that removing it and inlining
`current_inferior ()->arch ()` everywhere will make it a bit clearer
where that arch comes from and will trigger people into reflecting
whether this is the right place to get the arch or not.

Change-Id: I79f14b4e4934c88f91ca3a3155f5fc3ea2fadf6b
Reviewed-By: John Baldwin <jhb@FreeBSD.org>
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-10-10 10:44:35 -04:00
Andrew Burgess
3d38b301bb gdb: remove print_sys_errmsg
This started with me running into this comment in symfile.c:

  /* FIXME, should use print_sys_errmsg but it's not filtered.  */
  gdb_printf (_("`%ps' has disappeared; keeping its symbols.\n"),
              styled_string (file_name_style.style (), filename));

In this particular case I think I disagree with the comment; I think
the output should be a warning rather than just a message printed to
gdb_stdout, I think when the executable, or some other objfile that is
currently being debugged, disappears from disk, this is likely an
unexpected situation, and worth warning the user about.

So, in theory, I could just call print_sys_errmsg and remove the
comment, but that would mean loosing the filename styling in the
output... so in the end I remove the comment and updated the code to
call warning.

But that got me looking at print_sys_errmsg and how it's used.

Currently the function takes a string and an errno, and prints, to
stderr, the string followed by the result of calling strerror on the
errno.

In some places the string passed to print_sys_errmsg is just a
filename, and this is used when something goes wrong.  In these cases,
I think calling warning rather than gdb_printf to gdb_stderr, would be
better, and in fact, in a couple of places we manually print a
"warning" prefix, and then call print_sys_errmsg.  And so, for these
users I have added a new function warning_filename_and_errno, which
takes a filename, which is printed with styling, and an errno, which
is passed through strerror and the resulting string printed.  This new
function calls warning to print its output.  I then updated some of
the print_sys_errmsg users to use this new function.

Some other users of print_sys_errmsg are also emitting what is clearly
a warning, however, the string being passed in is more than just a
filename, so the new warning_filename_and_errno function can't be
used, it would style the whole string.  For these users I have
switched to calling warning directly, this allows me to style the
warning message correctly.

Finally, in inflow.c there is one last call to print_sys_errmsg, in
this case I just inlined the definition of print_sys_errmsg.  This is
a really weird case, as after printing this message GDB just does a
hard exit.  This is pretty old code, dating back to the initial GDB
import, I guess it should be updated to call error() maybe, but I'm
reluctant to make this change as part of this commit, just in case
there's some reason why we can't throw an error at this point.

With that done there are now no users of print_sys_errmsg, and so the
old function can be removed.

While I was doing all of the above I added some additional filename
styling in soure.c, this is in an else block where the if contained
the print_sys_errmsg call, so these felt related.

And finally, while I was updating the uses of print_sys_errmsg in
procfs.c, I noticed that we used a static errmsg buffer to format some
error strings.  As the above changes got rid of one of the users of
errmsg I also removed the other two users, and the static buffer.

There were a couple of tests that depended on the existing output
message format that needed updating.  In one case we gained an extra
'warning: ' prefix, and in the other 'Warning: ' becomes 'warning: ',
I think in both cases the new output is an improvement.

Approved-By: Tom Tromey <tom@tromey.com>
2023-10-05 12:21:46 +01:00
Tom Tromey
ef0f16ccf8 Remove explanatory comments from includes
I noticed a comment by an include and remembered that I think these
don't really provide much value -- sometimes they are just editorial,
and sometimes they are obsolete.  I think it's better to just remove
them.  Tested by rebuilding.

Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-09-20 11:45:16 -06:00
Pedro Alves
9d7d58e726 gdb: centralize "[Thread ...exited]" notifications
Currently, each target backend is responsible for printing "[Thread
...exited]" before deleting a thread.  This leads to unnecessary
differences between targets, like e.g. with the remote target, we
never print such messages, even though we do print "[New Thread ...]".

E.g., debugging the gdb.threads/attach-many-short-lived-threads.exp
with gdbserver, letting it run for a bit, and then pressing Ctrl-C, we
currently see:

 (gdb) c
 Continuing.
 ^C[New Thread 3850398.3887449]
 [New Thread 3850398.3887500]
 [New Thread 3850398.3887551]
 [New Thread 3850398.3887602]
 [New Thread 3850398.3887653]
 ...

 Thread 1 "attach-many-sho" received signal SIGINT, Interrupt.
 0x00007ffff7e6a23f in __GI___clock_nanosleep (clock_id=clock_id@entry=0, flags=flags@entry=0, req=req@entry=0x7fffffffda80, rem=rem@entry=0x7fffffffda80)
     at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:78
 78      in ../sysdeps/unix/sysv/linux/clock_nanosleep.c
 (gdb)

Above, we only see "New Thread" notifications, even though threads
were deleted.

After this patch, we'll see:

 (gdb) c
 Continuing.
 ^C[Thread 3558643.3577053 exited]
 [Thread 3558643.3577104 exited]
 [Thread 3558643.3577155 exited]
 [Thread 3558643.3579603 exited]
 ...
 [New Thread 3558643.3597415]
 [New Thread 3558643.3600015]
 [New Thread 3558643.3599965]
 ...

 Thread 1 "attach-many-sho" received signal SIGINT, Interrupt.
 0x00007ffff7e6a23f in __GI___clock_nanosleep (clock_id=clock_id@entry=0, flags=flags@entry=0, req=req@entry=0x7fffffffda80, rem=rem@entry=0x7fffffffda80)
     at ../sysdeps/unix/sysv/linux/clock_nanosleep.c:78
 78      in ../sysdeps/unix/sysv/linux/clock_nanosleep.c
 (gdb) q

This commit fixes this by moving the thread exit printing to common
code instead, triggered from within delete_thread (or rather,
set_thread_exited).

There's one wrinkle, though.  While most targest want to print:

 [Thread ... exited]

the Windows target wants to print:

 [Thread ... exited with code <exit_code>]

... and sometimes wants to suppress the notification for the main
thread.  To address that, this commits adds a delete_thread_with_code
function, only used by that target (so far).

This fix was originally posted as part of a larger series:

  https://inbox.sourceware.org/gdb-patches/20221212203101.1034916-1-pedro@palves.net/

But didn't really need to be part of that series.  In order to get
this fix merged sooner, I (Andrew Burgess) have rebased this commit
outside of the original series.  Any bugs introduced while splitting
this patch out and rebasing, are entirely my own.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30129
Co-Authored-By: Andrew Burgess <aburgess@redhat.com>
2023-08-23 09:57:38 +01:00
Simon Marchi
9213a6d79a gdb: make find_thread_ptid a process_stratum_target method
Make find_thread_ptid (the overload that takes a process_stratum_target)
a method of process_stratum_target.

Change-Id: Ib190a925a83c6b93e9c585dc7c6ab65efbdd8629
Reviewed-By: Tom Tromey <tom@tromey.com>
2023-04-04 21:05:31 -04:00
Pedro Alves
fb6d30e013 Tweak "Using the running image of ..." output
Currently, "info files" and "info program" on a few native targets
show:

 (gdb) info files
 Symbols from "/home/pedro/gdb/tests/threads".
 Native process:
	 Using the running image of child Thread 0x7ffff7d89740 (LWP 1097968).
					  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 ...

 (gdb) info program
	 Using the running image of child Thread 0x7ffff7d89740 (LWP 1097968).
					  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 Program stopped at 0x555555555278.
 ...


This patch changes them to:

 (gdb) info files
 Symbols from "/home/pedro/gdb/tests/threads".
 Native process:
	 Using the running image of child process 1097968.
	                                  ^^^^^^^^^^^^^^^
 ...

 (gdb) info program
	 Using the running image of child process 1097968.
	                                  ^^^^^^^^^^^^^^^
 Program stopped at 0x555555555278.
 ...


... which I think makes a lot more sense in this context.  The "info
program" manual entry even says:

  "Display information about the status of your program: whether it is
   running or not, what process it is, and why it stopped."
                        ^^^^^^^^^^^^^

This change affects ptrace targets, procfs targets, and Windows.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
Change-Id: I6aab061ff494a84ba3398cf98fd49efd7a6ec1ca
2023-02-27 19:12:28 +00:00
Joel Brobecker
213516ef31 Update copyright year range in header of all files managed by GDB
This commit is the result of running the gdb/copyright.py script,
which automated the update of the copyright year range for all
source files managed by the GDB project to be updated to include
year 2023.
2023-01-01 17:01:16 +04:00
Tom Tromey
c88afe9cf5 Fix control-c handling on Windows
As Hannes pointed out, the Windows target-async patches broke C-c
handling there.  Looking into this, I found a few oddities, fixed
here.

First, windows_nat_target::interrupt calls GenerateConsoleCtrlEvent.
I think this event can be ignored by the inferior, so it's not a great
way to interrupt.  Instead, using DebugBreakProcess (or a more
complicated thing for Wow64) seems better.

Second, windows_nat_target did not implement the pass_ctrlc method.
Implementing this lets us remove the special code to call
SetConsoleCtrlHandler and instead integrate into gdb's approach to C-c
handling.  I believe that this should also fix the race that's
described in the comment that's being removed.

Initially, I thought a simpler version of this patch would work.
However, I think what happens is that some other library (I'm not sure
what) calls SetConsoleCtrlHandler while gdb is running, and this
intercepts and handles C-c -- so that the gdb SIGINT handler is not
called.  C-break continues to work, presumably because whatever
handler is installed ignores it.

This patch works around this issue by ensuring that the gdb handler
always comes first.
2022-12-13 12:51:53 -07:00
Tom Tromey
4cb80f0e5b Remove call to registers_changed from windows-nat.c
I noticed that windows_nat_target::interrupt calls registers_changed.
However, I don't think there's any reason to do this, because this
will happen automatically when the inferior stop is processed.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
2022-12-01 11:40:12 -07:00
Tom Tromey
5f8363d924 Remove the_windows_nat_target global
I belatedly realized that the "the_windows_nat_target" global isn't
really necessary.  It's only used in one place, where 'this' would be
simpler and clearer.  This patch removes the global entirely.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
2022-12-01 11:36:18 -07:00
Hannes Domani
e1df90510a Remove no longer used jump label
The out label is unused since wait_for_debug_event is in a different thread.
2022-11-28 20:46:01 +01:00
Hannes Domani
0d146c1c5f Actually set m_is_async to current async mode
Looks like this was missed in the async mode implementation.
2022-11-28 20:46:01 +01:00
Hannes Domani
23d04cffa8 Don't use auto for lambda parameter
Older gcc versions (here 4.9.2) can't handle auto for a lambda parameter:

../../gdb/windows-nat.c: In member function 'void windows_nat_target::delete_thread(ptid_t, DWORD, bool)':
../../gdb/windows-nat.c:629:12: error: use of 'auto' in lambda parameter declaration only available with -std=c++1y or -std=gnu++1y [-Werror]
        [=] (auto &th)
            ^
2022-11-28 20:46:01 +01:00
Hannes Domani
1d39fec4ae Fix calling convention of thread entry point
For i686 the CreateThread entry point function needs the WINAPI (stdcall)
calling convention:

../../gdb/windows-nat.c: In constructor 'windows_nat_target::windows_nat_target()':
../../gdb/windows-nat.c:450:56: error: invalid user-defined conversion from 'windows_nat_target::windows_nat_target()::<lambda(LPVOID)>' to 'LPTHREAD_START_ROUTINE' {aka 'long unsigned int (__attribute__((stdcall)) *)(void*)'} [-fpermissive]
  450 |   HANDLE bg_thread = CreateThread (nullptr, 64 * 1024, fn, this, 0, nullptr);
      |                                                        ^~
../../gdb/windows-nat.c:444:13: note: candidate is: 'constexpr windows_nat_target::windows_nat_target()::<lambda(LPVOID)>::operator DWORD (*)(LPVOID)() const' (near match)
  444 |   auto fn = [] (LPVOID self) -> DWORD
      |             ^
../../gdb/windows-nat.c:444:13: note:   no known conversion from 'DWORD (*)(LPVOID)' {aka 'long unsigned int (*)(void*)'} to 'LPTHREAD_START_ROUTINE' {aka 'long unsigned int (__attribute__((stdcall)) *)(void*)'}

Since it's not possible to change the calling convention of a lambda, I've
moved it to a separate function.
2022-11-28 20:46:01 +01:00
Tom Tromey
c83b95d88f Fix static initialization order problem in windows-nat.c
This patch fixes a static initialization order problem in
windows-nat.c that was pointed out by Jon Turney.  The underlying
problem is that the windows_nat_target constructor relies on
serial_logfile already being constructed, but this is not enforced by
C++ rules.  This patch fixes the problem by initializing the global
windows_nat_target later.
2022-11-17 11:44:30 -07:00
Jon Turney
90f902daf5
Drop apparently unneeded include of winsock2.h
Commit d08bae3d ("Implement target async for Windows") unconditionally
includes winsock2.h.  We don't want to do that on Cygwin, since
including both winsock2.h and sys/select.h causes incompatible
redefinition problems.

Since that include is apparently unneeded, just drop it.

Fixes: d08bae3d
2022-11-13 14:44:43 +00:00
Jon Turney
26f228db71
Fix Cygwin build after 20489cca
Update code under __CYGWIN__ which accesses inferior process information
which is now stored in windows_process_info rather than globals.
2022-11-02 14:36:16 +00:00
Pedro Alves
f34652de0b internal_error: remove need to pass __FILE__/__LINE__
Currently, every internal_error call must be passed __FILE__/__LINE__
explicitly, like:

  internal_error (__FILE__, __LINE__, "foo %d", var);

The need to pass in explicit __FILE__/__LINE__ is there probably
because the function predates widespread and portable variadic macros
availability.  We can use variadic macros nowadays, and in fact, we
already use them in several places, including the related
gdb_assert_not_reached.

So this patch renames the internal_error function to something else,
and then reimplements internal_error as a variadic macro that expands
__FILE__/__LINE__ itself.

The result is that we now should call internal_error like so:

  internal_error ("foo %d", var);

Likewise for internal_warning.

The patch adjusts all calls sites.  99% of the adjustments were done
with a perl/sed script.

The non-mechanical changes are in gdbsupport/errors.h,
gdbsupport/gdb_assert.h, and gdb/gdbarch.py.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
Change-Id: Ia6f372c11550ca876829e8fd85048f4502bdcf06
2022-10-19 15:32:36 +01:00
Tom Tromey
d08bae3d66 Implement target async for Windows
This implements target async for Windows.  The basic idea is to have
the worker thread block in WaitForDebugEvent, then notify the event
loop when an event is seen.  In a few situations, this blocking
behavior is undesirable, so the functions passed to do_synchronously
are changed to return a boolean indicating which behavior is needed.
2022-08-22 12:12:00 -06:00
Tom Tromey
4cb763d64d Move some Windows operations to worker thread
On Windows, certain debugging APIs can only be called from the thread
that started (or attached) to the inferior.  Also, there is no way on
Windows to wait for a debug event in addition to other events.
Therefore, in order to implement target async for Windows, gdb will
have to call some functions in a worker thread.

This patch implements the worker thread and moves the necessary
operations there.  Target async isn't yet implemented, so this patch
does not cause any visible changes.
2022-08-22 12:09:24 -06:00
Tom Tromey
02d04eac24 Use strwinerror in gdb/windows-nat.c
When working on windows-nat.c, it's useful to see an error message in
addition to the error number given by GetLastError.  This patch moves
strwinerror from gdbserver to gdbsupport, and then updates
windows-nat.c to use it.  A couple of minor changes to strwinerror
(constify the return type and use the ARRAY_SIZE macro) are also
included.
2022-08-16 08:04:37 -06:00
Tom Tromey
eea2d83593 Remove dead code from windows_nat_target::detach
windows_nat_target::detach has a variable 'detached' that is only set
after a call to 'error'.  However, this can't happen because 'error'
throws an exception.

This patch removes the dead code.
2022-07-25 07:29:58 -06:00
Tom Tromey
36da255e7c Fix typo in windows-nat.c
I noticed a typo in a printf in windows-nat.c.  This fixes it.
2022-07-22 10:58:11 -06:00
Andrew Burgess
08106042d9 gdb: move the type cast into gdbarch_tdep
I built GDB for all targets on a x86-64/GNU-Linux system, and
then (accidentally) passed GDB a RISC-V binary, and asked GDB to "run"
the binary on the native target.  I got this error:

  (gdb) show architecture
  The target architecture is set to "auto" (currently "i386").
  (gdb) file /tmp/hello.rv32.exe
  Reading symbols from /tmp/hello.rv32.exe...
  (gdb) show architecture
  The target architecture is set to "auto" (currently "riscv:rv32").
  (gdb) run
  Starting program: /tmp/hello.rv32.exe
  ../../src/gdb/i387-tdep.c:596: internal-error: i387_supply_fxsave: Assertion `tdep->st0_regnum >= I386_ST0_REGNUM' failed.

What's going on here is this; initially the architecture is i386, this
is based on the default architecture, which is set based on the native
target.  After loading the RISC-V executable the architecture of the
current inferior is updated based on the architecture of the
executable.

When we "run", GDB does a fork & exec, with the inferior being
controlled through ptrace.  GDB sees an initial stop from the inferior
as soon as the inferior comes to life.  In response to this stop GDB
ends up calling save_stop_reason (linux-nat.c), which ends up trying
to read register from the inferior, to do this we end up calling
target_ops::fetch_registers, which, for the x86-64 native target,
calls amd64_linux_nat_target::fetch_registers.

After this I eventually end up in i387_supply_fxsave, different x86
based targets will end in different functions to fetch registers, but
it doesn't really matter which function we end up in, the problem is
this line, which is repeated in many places:

  i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (arch);

The problem here is that the ARCH in this line comes from the current
inferior, which, as we discussed above, will be a RISC-V gdbarch, the
tdep field will actually be of type riscv_gdbarch_tdep, not
i386_gdbarch_tdep.  After this cast we are relying on undefined
behaviour, in my case I happen to trigger an assert, but this might
not always be the case.

The thing I tried that exposed this problem was of course, trying to
start an executable of the wrong architecture on a native target.  I
don't think that the correct solution for this problem is to detect,
at the point of cast, that the gdbarch_tdep object is of the wrong
type, but, I did wonder, is there a way that we could protect
ourselves from incorrectly casting the gdbarch_tdep object?

I think that there is something we can do here, and this commit is the
first step in that direction, though no actual check is added by this
commit.

This commit can be split into two parts:

 (1) In gdbarch.h and arch-utils.c.  In these files I have modified
 gdbarch_tdep (the function) so that it now takes a template argument,
 like this:

    template<typename TDepType>
    static inline TDepType *
    gdbarch_tdep (struct gdbarch *gdbarch)
    {
      struct gdbarch_tdep *tdep = gdbarch_tdep_1 (gdbarch);
      return static_cast<TDepType *> (tdep);
    }

  After this change we are no better protected, but the cast is now
  done within the gdbarch_tdep function rather than at the call sites,
  this leads to the second, much larger change in this commit,

  (2) Everywhere gdbarch_tdep is called, we make changes like this:

    -  i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (arch);
    +  i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);

There should be no functional change after this commit.

In the next commit I will build on this change to add an assertion in
gdbarch_tdep that checks we are casting to the correct type.
2022-07-21 15:19:42 +01:00
Jon Turney
9dad432e10
Trivial fixes to Cygwin build after 8fea1a81
* Remove a stray semicolon
* Restore dropped nullptr program argument in use of create_process() under CYGWIN
2022-06-12 17:01:32 +01:00
Jon Turney
09d5a3fffc
Simplify __USEWIDE
Prior to c6ca3dab dropping support for Cygwin 1.5, __USEWIDE was not
defined for Cygwin 1.5.  After that, it's always defined if __CYGWIN__
is, so remove __USEWIDE conditionals inside __CYGWIN__ conditionals.
2022-06-12 15:57:53 +01:00
Jon Turney
7d941aa3aa
Simplify cygwin_buf_t
Prior to c6ca3dab dropping support for Cygwin 1.5, cygwin_buf_t was
defined as char for Cygwin 1.5.  After that, it's always wchar_t, so
just use that.
2022-06-12 15:56:55 +01:00
Tom Tromey
20489cca90 Use subclasses of windows_process_info
This changes windows_process_info to use virtual methods for its
callbacks, and then changes the two clients of this code to subclass
this class to implement the methods.

I considered using CRTP here, but that would require making the new
structures visible to the compilation of of nat/windows-nat.c.  This
seemed like a bit of a pain, so I didn't do it.

This change then lets us change all the per-inferior globals to be
members of the new subclass.  Note that there can still only be a
single inferior -- currently there's a single global of the new type.
This is just another step toward possibly implementing multi-inferior
for Windows.

It's possible this could be cleaned up further... ideally I'd like to
move more of the data into the base class.  However, because gdb
supports Cygwin and gdbserver does not, and because I don't have a way
to build or test Cygwin, larger refactorings are difficult.
2022-06-07 11:44:53 -06:00
Tom Tromey
5517650206 Turn some windows-nat.c static functions into methods
This patch turns some windows-nat.c static functions into methods on
windows_nat_target.  This avoids having to reference the
windows_nat_target singleton in some more spots -- a minor code
cleanup.
2022-06-07 11:44:51 -06:00
Tom Tromey
bcb9251f02 Allow ASLR to be disabled on Windows
On Windows, it is possible to disable ASLR when creating a process.
This patch adds code to do this, and hooks it up to gdb's existing
disable-randomization feature.  Because the Windows documentation
cautions that this isn't available on all versions of Windows, the
CreateProcess wrapper function is updated to make the attempt, and
then fall back to the current approach if it fails.
2022-06-07 09:59:41 -06:00