From patchwork Tue Feb 20 16:56:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 86043 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 647E03858C78 for ; Tue, 20 Feb 2024 16:56:46 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) by sourceware.org (Postfix) with ESMTPS id 080F23858D33 for ; Tue, 20 Feb 2024 16:56:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 080F23858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 080F23858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708448176; cv=none; b=M2Hr/K67gdVM48MHh4AxNqPhzYdmglylv9TsgKB+NWkAY814WeF9ofU8EbEamxdB16+MGhIRgX9gECPNFus9DsAgQ5ZrBwf6HlJrGWWP4CktnkfrsUvSD/p2vJzKFQX4qhUR5TtXigTk/ztdlzEmOejmTXkbTI8mFEv8OZrI4GY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708448176; c=relaxed/simple; bh=0Hdk4i6i0ue4vcmc3iUK8M6cR1JeY8aRXISP2wMO9og=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=nZ5Aomh6LOBSzMKM8GjvtKZcEa9vUq5tlTC8zEURCXXKBzCMHk9aJcqlQhggH36QlkT1fwvpk9P2NC8bjj4MIlViWnydOWZknly8HRT9cROcSj5HILSFM2yU38X+Sk4z5uUzxgNCgqFB2AJ6ByQuI/cRYzJoab/JdJh38DAvmgo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-33d51cd89dbso1464967f8f.0 for ; Tue, 20 Feb 2024 08:56:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708448171; x=1709052971; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mYCP40m+0eFhnUjdEQrWTbXiSvzDVTM0830+BjjxE5Y=; b=d6TxwPaU7pLK7QNG/a0aqaQUpnfOGHIqMM+BULiwbD3RFS32tHsdgVewjp6vfizz+A pomEyJIwRuP5uYfF1VzV0ZTnOeee9/Rq8B3v4FkXBm8Har5XjIqxP18FgFscbw8UHmMU wXRHMhJaK0YRE2kzk60kR098DsT6NaH31UYyJF4Y3bVia/R0s1Z4yiRGyBCGUceCfwsT IsVOtAIjpjfNVxCV1eniscr3Sa8qMh1dtPPsnKnePq5qIqnEn9MOf1TjoRiPpAg/Do7t fu/AZQyz56tmfbffk0ravNP3RQg53xrmq0PNHpriC9hlFUOk2QZ8NQdrdIpy/zr7pP0S KmiA== X-Gm-Message-State: AOJu0YycgJgN+9AR3IutG1YAf8iBX9ojQjKttZURaKoOzL3ltuOuaOSd hGd6jqT7BXWlN8yBNPTMKKtKkf/TmBcSRsGiESrFrvacKVAY+F9aLwMB+yRVrlA= X-Google-Smtp-Source: AGHT+IGVb/60FjjdB0dnmjzDYYP4HJQ3DP9Z2R68A5UfnRJpkw1rI+R+O+tc1D7+rXzQDvlO7hE2Zg== X-Received: by 2002:a5d:64c2:0:b0:33d:3098:c1f with SMTP id f2-20020a5d64c2000000b0033d30980c1fmr6614484wri.44.1708448170379; Tue, 20 Feb 2024 08:56:10 -0800 (PST) Received: from localhost ([2001:8a0:f918:ab00:69e9:8530:3f47:315c]) by smtp.gmail.com with UTF8SMTPSA id m12-20020a5d4a0c000000b0033b60bad2fcsm13972347wrq.113.2024.02.20.08.56.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 20 Feb 2024 08:56:09 -0800 (PST) From: Pedro Alves To: gdb-patches@sourceware.org Cc: Pedro Alves Subject: [PATCH 1/2] Teach gdb how to unwind cygwin _sigbe and sigdelayed frames Date: Tue, 20 Feb 2024 16:56:03 +0000 Message-ID: <20240220165605.563516-2-pedro@palves.net> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240220165605.563516-1-pedro@palves.net> References: <20240220165605.563516-1-pedro@palves.net> MIME-Version: 1.0 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org From: Jon Turney The majority of functions in the cygwin DLL are wrapped by routines which use an an alternate stack to return via a signal handler if a signal occured while inside the function. (See [1],[2]) At present, these frames cannot be correctly unwound by gdb. There doesn't seem to currently be a way to correctly describe these frames using DWARF CFI. So instead, write a custom unwinder for _sigbe and sigdelayed frames, which gets the return address from the alternate stack. The offset of tls::stackptr from TIB.stacktop is determined by analyzing the code in _sigbe or sigdelayed. This can backtrace from _sigbe and from a sighandler through sigdelayed. Implemented for amd64 and i386 Issues: 1. We should detect if we are in the wrapper after the return address has been popped off the alternate stack, and if so, fetch the return address from the register it's been popped into. 2. If there are multiple _sigbe or sigdelayed stack frames to be unwound, this only unwinds the first one correctly, because we don't unwind the value of the alternate stack pointer itself. This is no worse than currently, when we can't even unwind one of these frame correctly, but isn't quite correct. I guess this could be handled by defining a pseudo-register to track its value as we unwind the stack. [1] https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/gendef [2] https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=winsup/cygwin/how-signals-work.txt Co-Authored-By: Pedro Alves Change-Id: I4a0d02c1b85d0aadaab2de3abd584eb4bda5b5cc --- gdb/amd64-windows-tdep.c | 26 ++++++ gdb/i386-windows-tdep.c | 20 ++++ gdb/windows-tdep.c | 194 +++++++++++++++++++++++++++++++++++++++ gdb/windows-tdep.h | 20 ++++ 4 files changed, 260 insertions(+) diff --git a/gdb/amd64-windows-tdep.c b/gdb/amd64-windows-tdep.c index 7c5169fd98c..af17886c080 100644 --- a/gdb/amd64-windows-tdep.c +++ b/gdb/amd64-windows-tdep.c @@ -1334,11 +1334,37 @@ amd64_windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) set_gdbarch_long_bit (gdbarch, 32); } +/* Sigwrapper unwinder instruction patterns for AMD64. */ + +static const gdb_byte amd64_sigbe_bytes[] = { + 0x49, 0xc7, 0xc3, 0xf8, 0xff, 0xff, 0xff, /* movq $-8,%r11 */ + 0x4d, 0x0f, 0xc1, 0x9a, /* xaddq %r11,$tls::stackptr(%r10) */ + /* 4 bytes for tls::stackptr operand. */ +}; + +static const gdb_byte amd64_sigdelayed_bytes[] = { + 0x49, 0xc7, 0xc3, 0xf8, 0xff, 0xff, 0xff, /* movq $-8,%r11 */ + 0x4d, 0x0f, 0xc1, 0x9c, 0x24, /* xaddq %r11,$tls::stackptr(%r12) */ + /* 4 bytes for tls::stackptr operand. */ +}; + +static const gdb::array_view amd64_sig_patterns[] { + { amd64_sigbe_bytes }, + { amd64_sigdelayed_bytes }, +}; + +/* The sigwrapper unwinder on AMD64. */ + +static const cygwin_sigwrapper_frame_unwind + amd64_cygwin_sigwrapper_frame_unwind (amd64_sig_patterns); + /* gdbarch initialization for Cygwin on AMD64. */ static void amd64_cygwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) { + frame_unwind_append_unwinder (gdbarch, &amd64_cygwin_sigwrapper_frame_unwind); + amd64_windows_init_abi_common (info, gdbarch); cygwin_init_abi (info, gdbarch); } diff --git a/gdb/i386-windows-tdep.c b/gdb/i386-windows-tdep.c index cd5c580a3e8..e8ed043be8a 100644 --- a/gdb/i386-windows-tdep.c +++ b/gdb/i386-windows-tdep.c @@ -27,6 +27,7 @@ #include "xml-support.h" #include "gdbcore.h" #include "inferior.h" +#include "frame-unwind.h" /* Core file support. */ @@ -169,11 +170,30 @@ i386_windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) set_gdbarch_push_dummy_call (gdbarch, i386_windows_push_dummy_call); } +/* Sigwrapper unwinder instruction patterns for i386. */ + +static const gdb_byte i386_sigbe_bytes[] = { + 0xb8, 0xfc, 0xff, 0xff, 0xff, /* movl $-4,%eax */ + 0x0f, 0xc1, 0x83, /* xadd %eax,$tls::stackptr(%ebx) */ + /* 4 bytes for tls::stackptr operand. */ +}; + +static const gdb::array_view i386_sig_patterns[] { + { i386_sigbe_bytes }, +}; + +/* The sigwrapper unwinder on i386. */ + +static const cygwin_sigwrapper_frame_unwind + i386_cygwin_sigwrapper_frame_unwind (i386_sig_patterns); + /* gdbarch initialization for Cygwin on i386. */ static void i386_cygwin_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) { + frame_unwind_append_unwinder (gdbarch, &i386_cygwin_sigwrapper_frame_unwind); + i386_windows_init_abi_common (info, gdbarch); cygwin_init_abi (info, gdbarch); } diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c index 19a42a49a98..361ccdd0dcb 100644 --- a/gdb/windows-tdep.c +++ b/gdb/windows-tdep.c @@ -32,6 +32,7 @@ #include "gdb_bfd.h" #include "solib.h" #include "solib-target.h" +#include "frame-unwind.h" #include "gdbcore.h" #include "coff/internal.h" #include "libcoff.h" @@ -1212,3 +1213,196 @@ even if their meaning is unknown."), isn't another convenience variable of the same name. */ create_internalvar_type_lazy ("_tlb", &tlb_funcs, NULL); } + +/* Frame cache data for the cygwin sigwrapper unwinder. */ + +struct cygwin_sigwrapper_frame_cache +{ + CORE_ADDR prev_pc; + int tlsoffset; +}; + +/* Return true if the instructions at PC match the instructions bytes + in PATTERN. Returns false otherwise. */ + +static bool +insns_match_pattern (CORE_ADDR pc, + const gdb::array_view pattern) +{ + for (size_t i = 0; i < pattern.size (); i++) + { + gdb_byte buf; + if (target_read_code (pc + i, &buf, 1) != 0) + return false; + if (buf != pattern[i]) + return false; + } + return true; +} + +/* Helper for cygwin_sigwrapper_frame_cache. Search for one of the + patterns in PATTERNS_LIST within [START, END). If found, record + the tls offset found after the matched pattern in the instruction + stream, in *TLSOFFSET. */ + +static void +cygwin_sigwrapper_frame_analyze + (struct gdbarch *gdbarch, + CORE_ADDR start, CORE_ADDR end, + gdb::array_view> patterns_list, + int *tlsoffset) +{ + enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); + + *tlsoffset = 0; + + for (CORE_ADDR addr = start; addr < end; addr++) + { + for (auto patterns : patterns_list) + { + if (insns_match_pattern (addr, patterns)) + { + /* The instruction sequence is followed by 4 bytes for + tls::stackptr. */ + gdb_byte tls_stackptr[4]; + if (target_read_code (addr + patterns.size (), tls_stackptr, 4) == 0) + { + *tlsoffset = extract_signed_integer (tls_stackptr, 4, byte_order); + + frame_debug_printf ("matched pattern at %s, sigstackptroffset=%x", + paddress (gdbarch, addr), + *tlsoffset); + break; + } + } + } + } + + /* XXX: Perhaps we should also note the address of the xaddq + instruction which pops the RA from the sigstack. If PC is after + that, we should look in the appropriate register to get the RA, + not on the sigstack. */ +} + +/* Fill THIS_CACHE using the cygwin sigwrapper unwinding data for + THIS_FRAME. */ + +static cygwin_sigwrapper_frame_cache * +cygwin_sigwrapper_frame_cache (frame_info_ptr this_frame, void **this_cache) +{ + struct gdbarch *gdbarch = get_frame_arch (this_frame); + enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); + auto *cache = (struct cygwin_sigwrapper_frame_cache *) *this_cache; + const int len = gdbarch_addr_bit (gdbarch) / 8; + + /* Get address of top of stack from thread information block. */ + CORE_ADDR thread_local_base; + target_get_tib_address (inferior_ptid, &thread_local_base); + + CORE_ADDR stacktop + = read_memory_unsigned_integer (thread_local_base + len, len, byte_order); + + frame_debug_printf ("TEB.stacktop=%s", paddress (gdbarch, stacktop)); + + /* Find cygtls, relative to stacktop, and read signalstackptr from + cygtls. */ + CORE_ADDR signalstackptr + = read_memory_unsigned_integer (stacktop + cache->tlsoffset, + len, byte_order); + + frame_debug_printf ("sigsp=%s", paddress (gdbarch, signalstackptr)); + + /* Read return address from signal stack. */ + cache->prev_pc + = read_memory_unsigned_integer (signalstackptr - len, len, byte_order); + + frame_debug_printf ("ra=%s", paddress (gdbarch, cache->prev_pc)); + + return cache; +} + +static struct value * +cygwin_sigwrapper_frame_prev_register (const frame_info_ptr &this_frame, + void **this_cache, + int regnum) +{ + struct gdbarch *gdbarch = get_frame_arch (this_frame); + struct cygwin_sigwrapper_frame_cache *cache + = cygwin_sigwrapper_frame_cache (this_frame, this_cache); + + frame_debug_printf ("%s for pc=%s", + gdbarch_register_name (gdbarch, regnum), + paddress (gdbarch, cache->prev_pc)); + + if (regnum == gdbarch_pc_regnum (gdbarch)) + return frame_unwind_got_address (this_frame, regnum, cache->prev_pc); + + return frame_unwind_got_register (this_frame, regnum, regnum); +} + +static void +cygwin_sigwrapper_frame_this_id (const frame_info_ptr &this_frame, + void **this_cache, + struct frame_id *this_id) +{ + *this_id = frame_id_build_unavailable_stack (get_frame_func (this_frame)); +} + +static int +cygwin_sigwrapper_frame_sniffer (const struct frame_unwind *self_, + const frame_info_ptr &this_frame, + void **this_cache) +{ + const auto *self = (const struct cygwin_sigwrapper_frame_unwind *) self_; + struct gdbarch *gdbarch = get_frame_arch (this_frame); + + CORE_ADDR pc = get_frame_pc (this_frame); + const char *name; + CORE_ADDR start, end; + find_pc_partial_function (pc, &name, &start, &end); + + if (name == nullptr) + return 0; + + if (strcmp (name, "_sigbe") != 0 + && strcmp (name, "__sigbe") != 0 + && strcmp (name, "sigdelayed") != 0 + && strcmp (name, "_sigdelayed") != 0) + return 0; + + frame_debug_printf ("name=%s, start=%s, end=%s", + name, + paddress (gdbarch, start), + paddress (gdbarch, end)); + + int tlsoffset; + cygwin_sigwrapper_frame_analyze (gdbarch, start, end, self->patterns_list, + &tlsoffset); + if (tlsoffset == 0) + return 0; + + frame_debug_printf ("sigstackptroffset=%x", tlsoffset); + + auto *cache = FRAME_OBSTACK_ZALLOC (struct cygwin_sigwrapper_frame_cache); + cache->tlsoffset = tlsoffset; + + *this_cache = cache; + cygwin_sigwrapper_frame_cache (this_frame, this_cache); + + return 1; +} + +/* Cygwin sigwapper unwinder. */ + +cygwin_sigwrapper_frame_unwind::cygwin_sigwrapper_frame_unwind + (gdb::array_view> patterns_list) + : frame_unwind (), + patterns_list (patterns_list) +{ + name = "cygwin sigwrapper"; + type = NORMAL_FRAME; + stop_reason = default_frame_unwind_stop_reason; + this_id = cygwin_sigwrapper_frame_this_id; + prev_register = cygwin_sigwrapper_frame_prev_register; + sniffer = cygwin_sigwrapper_frame_sniffer; +} diff --git a/gdb/windows-tdep.h b/gdb/windows-tdep.h index e786a1d3eb9..f122f7aaa61 100644 --- a/gdb/windows-tdep.h +++ b/gdb/windows-tdep.h @@ -18,6 +18,8 @@ #ifndef WINDOWS_TDEP_H #define WINDOWS_TDEP_H +#include "frame-unwind.h" + struct gdbarch; extern struct cmd_list_element *info_w32_cmdlist; @@ -55,4 +57,22 @@ extern void cygwin_init_abi (struct gdbarch_info info, extern bool is_linked_with_cygwin_dll (bfd *abfd); +/* Cygwin sigwapper unwinder. Unwinds signal frames over + sigbe/sigdelayed. */ + +struct cygwin_sigwrapper_frame_unwind : public frame_unwind +{ + explicit cygwin_sigwrapper_frame_unwind + (gdb::array_view> patterns_list); + + /* Architecture-specific list of instruction patterns to match. + It's a list of patterns instead of single pattern because some + architectures want to match more than one function + (sigbe/sigdelayed & friends). Each potential instruction + sequence is assumed to be followed by 4 bytes for tls::stackptr. + If any pattern in the list matches, then the frame is assumed to + be a sigwrapper frame. */ + gdb::array_view> patterns_list; +}; + #endif