public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Jon Turney <jon.turney@dronecode.org.uk>
To: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH 2/2] Implement target async for Windows
Date: Fri, 4 Nov 2022 14:59:30 +0000	[thread overview]
Message-ID: <f37940ce-c898-85e8-8143-9c4df1c7d84c@dronecode.org.uk> (raw)
In-Reply-To: <20220803130822.735057-3-tromey@adacore.com>

[-- Attachment #1: Type: text/plain, Size: 2917 bytes --]

On 03/08/2022 14:08, Tom Tromey via Gdb-patches wrote:
> 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.
> ---
>   gdb/windows-nat.c | 123 ++++++++++++++++++++++++++++++++++++++++------
>   1 file changed, 108 insertions(+), 15 deletions(-)
> 
> diff --git a/gdb/windows-nat.c b/gdb/windows-nat.c
> index 80cdedce7b9..9c277e9a93d 100644
> --- a/gdb/windows-nat.c
> +++ b/gdb/windows-nat.c
[...]
>   
>   static windows_nat_target the_windows_nat_target;
> @@ -366,7 +400,8 @@ check (BOOL ok, const char *file, int line)
>   
>   windows_nat_target::windows_nat_target ()
>     : m_pushed_event (CreateEvent (nullptr, false, false, nullptr)),
> -    m_response_event (CreateEvent (nullptr, false, false, nullptr))
> +    m_response_event (CreateEvent (nullptr, false, false, nullptr)),
> +    m_wait_event (make_serial_event ())
>   {
>     auto fn = [] (LPVOID self) -> DWORD
>       {
This introduces a crash on gdb startup for me.

> Thread 1 "gdb" received signal SIGSEGV, Segmentation fault.
> ops=ops@entry=0x100abfca0 <serial_event_ops>, open_name=open_name@entry=0x0) at ../../gdb/serial.c:249
> 249       if (!serial_logfile.empty ())
> (top-gdb) p serial_logfile
> $1 = <error reading variable: Cannot access memory at address 0xffffffffffffffe8>
> (top-gdb) bt
> #0  0x000000010071e755 in serial_open_ops_1 (ops=ops@entry=0x100abfca0 <serial_event_ops>, open_name=open_name@entry=0x0) at ../../gdb/serial.c:249
> #1  0x000000010071ec77 in serial_open_ops (ops=ops@entry=0x100abfca0 <serial_event_ops>) at ../../gdb/serial.c:267
> #2  0x000000010071d0ac in make_serial_event () at ../../gdb/ser-event.c:164
> #3  0x0000000100810f9e in windows_nat_target::windows_nat_target (this=0x100cdf2e0 <the_windows_nat_target>) at ../../gdb/windows-nat.c:406
> #4  0x00000001009d77f0 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at ../../gdb/windows-nat.c:390
> #5  _GLOBAL__sub_I__ZN18windows_nat_targetC2Ev () at ../../gdb/windows-nat.c:3311
> #6  0x00007fff152a6582 in do_global_ctors (force=0, in_pfunc=0x1009d8700 <___CTOR_LIST__>) at ../../../../src/winsup/cygwin/dcrt0.cc:78
> #7  __main () at ../../../../src/winsup/cygwin/dcrt0.cc:1084
> #8  0x00000001009cd7d0 in main (argc=1, argv=0x7ffffcc50) at ../../gdb/gdb.c:25

This looks like an problem with static object construction ordering: 
the serial_logfile object needs to be constructed before before 
the_windows_nat_target object, but nothing enforces that, currently.

Attached is a patch I wrote to verify the issue, and applying it fixes 
the crash for me, but I'm sure there is a more elegant solution out there...

[-- Attachment #2: 0001-Avoid-crash-due-to-undefined-ordering-of-static-obje.patch --]
[-- Type: text/plain, Size: 3392 bytes --]

From 11d56dbbd8034eaa1f128f9b4a5fdcbf16cdbb9e Mon Sep 17 00:00:00 2001
From: Jon Turney <jon.turney@dronecode.org.uk>
Date: Fri, 4 Nov 2022 14:14:41 +0000
Subject: [PATCH] Avoid crash due to undefined ordering of static object
 construction

This looks like an issue with static object construction ordering:
the serial_logfile object needs to be constructed before before
the_windows_nat_target object, but nothing enforces that.

Thread 1 "gdb" received signal SIGSEGV, Segmentation fault.
ops=ops@entry=0x100abfca0 <serial_event_ops>, open_name=open_name@entry=0x0) at ../../gdb/serial.c:249
249       if (!serial_logfile.empty ())
> (top-gdb) p serial_logfile
> $1 = <error reading variable: Cannot access memory at address 0xffffffffffffffe8>
(top-gdb) bt
0  0x000000010071e755 in serial_open_ops_1 (ops=ops@entry=0x100abfca0 <serial_event_ops>, open_name=open_name@entry=0x0) at ../../gdb/serial.c:249
1  0x000000010071ec77 in serial_open_ops (ops=ops@entry=0x100abfca0 <serial_event_ops>) at ../../gdb/serial.c:267
2  0x000000010071d0ac in make_serial_event () at ../../gdb/ser-event.c:164
3  0x0000000100810f9e in windows_nat_target::windows_nat_target (this=0x100cdf2e0 <the_windows_nat_target>) at ../../gdb/windows-nat.c:406
4  0x00000001009d77f0 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at ../../gdb/windows-nat.c:390
5  _GLOBAL__sub_I__ZN18windows_nat_targetC2Ev () at ../../gdb/windows-nat.c:3311
6  0x00007fff152a6582 in do_global_ctors (force=0, in_pfunc=0x1009d8700 <___CTOR_LIST__>) at ../../../../src/winsup/cygwin/dcrt0.cc:78
7  __main () at ../../../../src/winsup/cygwin/dcrt0.cc:1084
8  0x00000001009cd7d0 in main (argc=1, argv=0x7ffffcc50) at ../../gdb/gdb.c:25
---
 gdb/serial.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/gdb/serial.c b/gdb/serial.c
index b304807c597..1cc0d2a9b5d 100644
--- a/gdb/serial.c
+++ b/gdb/serial.c
@@ -38,7 +38,13 @@ static struct serial *scb_base;
 /* Non-NULL gives filename which contains a recording of the remote session,
    suitable for playback by gdbserver.  */
 
-static std::string serial_logfile;
+static inline
+std::string &serial_logfile()
+{
+  static std::string serial_logfile_;
+  return serial_logfile_;
+}
+
 static struct ui_file *serial_logfp = NULL;
 
 static const struct serial_ops *serial_interface_lookup (const char *);
@@ -246,12 +252,12 @@ serial_open_ops_1 (const struct serial_ops *ops, const char *open_name)
   scb->next = scb_base;
   scb_base = scb;
 
-  if (!serial_logfile.empty ())
+  if (!serial_logfile().empty ())
     {
       stdio_file_up file (new stdio_file ());
 
-      if (!file->open (serial_logfile.c_str (), "w"))
-	perror_with_name (serial_logfile.c_str ());
+      if (!file->open (serial_logfile().c_str (), "w"))
+	perror_with_name (serial_logfile().c_str ());
 
       serial_logfp = file.release ();
     }
@@ -688,7 +694,7 @@ Show parity for remote serial I/O."), NULL,
 			NULL, /* FIXME: i18n: */
 			&serial_set_cmdlist, &serial_show_cmdlist);
 
-  add_setshow_filename_cmd ("remotelogfile", no_class, &serial_logfile, _("\
+  add_setshow_filename_cmd ("remotelogfile", no_class, &serial_logfile(), _("\
 Set filename for remote session recording."), _("\
 Show filename for remote session recording."), _("\
 This file is used to record the remote session for future playback\n\
-- 
2.38.1


  parent reply	other threads:[~2022-11-04 14:59 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-03 13:08 [PATCH 0/2] " Tom Tromey
2022-08-03 13:08 ` [PATCH 1/2] Move some Windows operations to worker thread Tom Tromey
2022-08-03 13:33   ` Eli Zaretskii
2022-08-03 18:47     ` Tom Tromey
2022-08-03 19:16       ` Eli Zaretskii
2022-08-03 13:08 ` [PATCH 2/2] Implement target async for Windows Tom Tromey
2022-11-04 14:28   ` Jon Turney
2022-11-08 18:38     ` Tom Tromey
2022-11-13 14:45       ` Jon Turney
2022-11-04 14:59   ` Jon Turney [this message]
2022-11-08 18:52     ` Tom Tromey
2022-11-17 14:33       ` Jon Turney
2022-11-17 18:38         ` Tom Tromey
2022-11-17 18:43           ` Tom Tromey
2022-08-03 13:34 ` [PATCH 0/2] " Eli Zaretskii
2022-08-03 18:54   ` Tom Tromey
2022-08-03 19:17     ` Eli Zaretskii
2022-08-22 18:03 ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f37940ce-c898-85e8-8143-9c4df1c7d84c@dronecode.org.uk \
    --to=jon.turney@dronecode.org.uk \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).