From patchwork Fri Mar 31 03:44:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 67139 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 641F73858025 for ; Fri, 31 Mar 2023 03:45:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 641F73858025 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680234323; bh=6hOUks+PY139xOuZ6hs8NiNxdF1K9PGzSaM7Gazwgws=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=vDAy8H8goAy+UojAR0Etqv8jBkQGvp8KEBohZyNx+PLt42RvdGq35CJ37gue2namw MGrnv2p9NSsGNvKJ5LzKQCkXMK8RqG3+RsSwjcBNfawIjG+jFS5FWU75yY61DEy1a6 cJ8LxOwC/MgLWn8NMONKWRUcFV7q9LxDleAszLEI= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-ot1-x329.google.com (mail-ot1-x329.google.com [IPv6:2607:f8b0:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id A824D3858D38 for ; Fri, 31 Mar 2023 03:44:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A824D3858D38 Received: by mail-ot1-x329.google.com with SMTP id 61-20020a9d02c3000000b0069fe8de3139so11217450otl.1 for ; Thu, 30 Mar 2023 20:44:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680234297; x=1682826297; 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=6hOUks+PY139xOuZ6hs8NiNxdF1K9PGzSaM7Gazwgws=; b=yxJDK2SIbVjYBhCoRuMH7xP3EgKuLYuQ4VZ4dhwiJvOGmkbeJP3X6HR2KCuYyAa0yW IVGYOmYBXITegEqFLKn3AhuMnHCdkPTevap6XGauJEUOe/UkyhaUC5Y7bQt07AmRAFhT o6TDh6HXTZ5ctw0LjkxMKZyFfd7cBEzzVVpHtG49N/nqBGKt9WPrpIzj8QjmfBaPayP9 a7yvCxeXG6GImq94BDanWsloU7M75/0k+vc6RTXK4edeytSwdloHCKqKh8jgmrVQY6s9 EkyNmDulclh8HOWgxQTTr+FEt29j5bYOgTV4wo56bbb66qfKGbVulqf+R8FlvM4Pv87M MtvA== X-Gm-Message-State: AO0yUKUIdyznEC1pYZgLJlLGeHe1odqbF/IUxTo8hEpVQM57CGPRxH+X dz7k4DVZ3xgB6fOEl8C27RUDifTiQdEojhn/hsI= X-Google-Smtp-Source: AK7set9uEmuhd7ZaAbZEti2L1ssY8y49NapI4UIXuoA4wq6LOKsCdC2151r7HwEisAqgdTRQ+p0EYw== X-Received: by 2002:a9d:7dc9:0:b0:69f:d2:4ff9 with SMTP id k9-20020a9d7dc9000000b0069f00d24ff9mr13080780otn.34.1680234296986; Thu, 30 Mar 2023 20:44:56 -0700 (PDT) Received: from localhost ([177.124.15.55]) by smtp.gmail.com with ESMTPSA id l6-20020a0568301d6600b0069fa6ca584bsm691664oti.40.2023.03.30.20.44.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 20:44:56 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Thiago Jung Bauermann , Pedro Alves Subject: [PATCH 1/5] gdbserver: Use current_process in handle_qxfer_auxv Date: Fri, 31 Mar 2023 03:44:28 +0000 Message-Id: <20230331034432.3037148-2-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230331034432.3037148-1-thiago.bauermann@linaro.org> References: <20230331034432.3037148-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Thiago Jung Bauermann via Gdb-patches From: Thiago Jung Bauermann Reply-To: Thiago Jung Bauermann Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Commit 43e5fbd8b788 ("gdbserver: Add PID parameter to linux_get_auxv and linux_get_hwcap") changed the functions for reading auxv from using current_thread to accepting a PID argument. Pedro then suggested: "Since we're removing the thread dependency, I think that check should be replaced by current_process() == NULL instead". This patch implements his suggestion. Suggested-by: Pedro Alves --- gdbserver/server.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/gdbserver/server.cc b/gdbserver/server.cc index 949849b63a2d..b6786baafadd 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -1445,11 +1445,10 @@ handle_qxfer_auxv (const char *annex, if (!the_target->supports_read_auxv () || writebuf != NULL) return -2; - if (annex[0] != '\0' || current_thread == NULL) + if (annex[0] != '\0' || current_process () == nullptr) return -1; - return the_target->read_auxv (current_thread->id.pid (), offset, readbuf, - len); + return the_target->read_auxv (current_process ()->pid, offset, readbuf, len); } /* Handle qXfer:exec-file:read. */ From patchwork Fri Mar 31 03:44:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 67140 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 568103858CDB for ; Fri, 31 Mar 2023 03:45:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 568103858CDB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680234352; bh=P6o8q8XzCAYSqf85kVdH5Rz1kNJ3aViJj45pHmrC0hA=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=tXyKNQFL8UnXZ/1b5IM1QNulcQpUfVpaZWWTv7ToXEcYHUTC+jKbiCa8kIFW4uBio 52kaMLOnG3hAIJJx/3GiH/acvNvErtFWlbh8cYkjF0PmtZzleoFdxiIS7VgTQjHqR8 WhSbW2NJaOTxY0sHc/dhaCj8fCWBKSr7un+WxWRs= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id 232B53857C55 for ; Fri, 31 Mar 2023 03:45:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 232B53857C55 Received: by mail-oi1-x22f.google.com with SMTP id r16so15733703oij.5 for ; Thu, 30 Mar 2023 20:45:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680234300; x=1682826300; 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=P6o8q8XzCAYSqf85kVdH5Rz1kNJ3aViJj45pHmrC0hA=; b=i9xaPTCRJV9NXnL6lXT1yo9c286FGm4WqHpAFcA9mTmZ9/Jta95PlAglkUFz1l02G5 mkVO3bpOjjICfBp2RA51dX1Zx0rrYkn9Vg6O6kuOcnV8UTT3SunwDQVBp9N1ttRCJ8TZ s9itAGE9GWputGRqAZMe37fRQ5YmiMGq+kHQPL1k8ZRfBMTqJi20XF2FDlzxdHgI6TLK hOEps9JpRwM6OOp63tenDy/njS+rPjj14t2MHINZI+xyn04gdG9vttjvrdfHVcF7yFeN rtWdRC6l2dRfkXuQxCRH+4IEkthsvEnSMioNr4PD8iWZQFsXkfheF4c5YgqpMAkMHYV5 KYuA== X-Gm-Message-State: AO0yUKVLab5/x3+E/ryaFXNjGJsTmqcHZrnqwl8eUDwH8rPDEMajSvxH Vc+MMykkh/Mr3ukT8cgKwGZccVffBvJJDMO9syM= X-Google-Smtp-Source: AK7set8yx3pAY9F8gnuPAEcJRARW6mZMdJNu6V3t+hp9ferSGYbiNYKnHk/dTVrVdo21lsGLFwNr5A== X-Received: by 2002:a54:410b:0:b0:360:fd4c:11ab with SMTP id l11-20020a54410b000000b00360fd4c11abmr11205729oic.1.1680234300234; Thu, 30 Mar 2023 20:45:00 -0700 (PDT) Received: from localhost ([177.124.15.55]) by smtp.gmail.com with ESMTPSA id y2-20020a4ad642000000b0051134f333d3sm415555oos.16.2023.03.30.20.44.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 20:44:59 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Thiago Jung Bauermann , Pedro Alves Subject: [PATCH 2/5] gdbserver: Use the PID of the current process instead of the current thread Date: Fri, 31 Mar 2023 03:44:29 +0000 Message-Id: <20230331034432.3037148-3-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230331034432.3037148-1-thiago.bauermann@linaro.org> References: <20230331034432.3037148-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Thiago Jung Bauermann via Gdb-patches From: Thiago Jung Bauermann Reply-To: Thiago Jung Bauermann Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Commit 43e5fbd8b788 ("gdbserver: Add PID parameter to linux_get_auxv and linux_get_hwcap") used current_thread->id.pid () to get the current process' PID. Pedro then mentioned that using current_process ()->pid is more to the point. He added that "sometimes there's a current process, but not a current thread, so when we don't actually need a thread, current_process() is better". So this patch goes through the places that use current_thread to get the current process' PID and changes them to use current_process () instead. Suggested-by: Pedro Alves --- gdbserver/linux-aarch64-low.cc | 10 +++++----- gdbserver/linux-arm-low.cc | 6 +++--- gdbserver/linux-ppc-low.cc | 6 +++--- gdbserver/mem-break.cc | 2 +- gdbserver/regcache.cc | 2 +- gdbserver/server.cc | 2 +- gdbserver/tracepoint.cc | 2 +- gdbserver/win32-i386-low.cc | 4 ++-- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/gdbserver/linux-aarch64-low.cc b/gdbserver/linux-aarch64-low.cc index 2ed6e95562c5..0c524c036d89 100644 --- a/gdbserver/linux-aarch64-low.cc +++ b/gdbserver/linux-aarch64-low.cc @@ -436,7 +436,7 @@ aarch64_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr, int ret; enum target_hw_bp_type targ_type; struct aarch64_debug_reg_state *state - = aarch64_get_debug_reg_state (pid_of (current_thread)); + = aarch64_get_debug_reg_state (current_process ()->pid); if (show_debug_regs) fprintf (stderr, "insert_point on entry (addr=0x%08lx, len=%d)\n", @@ -487,7 +487,7 @@ aarch64_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr, int ret; enum target_hw_bp_type targ_type; struct aarch64_debug_reg_state *state - = aarch64_get_debug_reg_state (pid_of (current_thread)); + = aarch64_get_debug_reg_state (current_process ()->pid); if (show_debug_regs) fprintf (stderr, "remove_point on entry (addr=0x%08lx, len=%d)\n", @@ -575,7 +575,7 @@ aarch64_target::low_stopped_data_address () = aarch64_remove_non_address_bits ((CORE_ADDR) siginfo.si_addr); /* Check if the address matches any watched address. */ - state = aarch64_get_debug_reg_state (pid_of (current_thread)); + state = aarch64_get_debug_reg_state (current_process ()->pid); for (i = aarch64_num_wp_regs - 1; i >= 0; --i) { const unsigned int offset @@ -846,7 +846,7 @@ aarch64_target::low_arch_setup () if (is_elf64) { struct aarch64_features features; - int pid = current_thread->id.pid (); + int pid = current_process ()->pid; features.vq = aarch64_sve_get_vq (tid); /* A-profile PAC is 64-bit only. */ @@ -3323,7 +3323,7 @@ aarch64_target::supports_memory_tagging () #endif } - return (linux_get_hwcap2 (current_thread->id.pid (), 8) & HWCAP2_MTE) != 0; + return (linux_get_hwcap2 (current_process ()->pid, 8) & HWCAP2_MTE) != 0; } bool diff --git a/gdbserver/linux-arm-low.cc b/gdbserver/linux-arm-low.cc index 5975b44af0ae..76d15baa8512 100644 --- a/gdbserver/linux-arm-low.cc +++ b/gdbserver/linux-arm-low.cc @@ -636,7 +636,7 @@ arm_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr, pts[i] = p; /* Only update the threads of the current process. */ - for_each_thread (current_thread->id.pid (), [&] (thread_info *thread) + for_each_thread (current_process ()->pid, [&] (thread_info *thread) { update_registers_callback (thread, watch, i); }); @@ -681,7 +681,7 @@ arm_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr, pts[i].control = arm_hwbp_control_disable (pts[i].control); /* Only update the threads of the current process. */ - for_each_thread (current_thread->id.pid (), [&] (thread_info *thread) + for_each_thread (current_process ()->pid, [&] (thread_info *thread) { update_registers_callback (thread, watch, i); }); @@ -958,7 +958,7 @@ get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self) static const struct target_desc * arm_read_description (void) { - unsigned long arm_hwcap = linux_get_hwcap (current_thread->id.pid (), 4); + unsigned long arm_hwcap = linux_get_hwcap (current_process ()->pid, 4); if (arm_hwcap & HWCAP_IWMMXT) return arm_linux_read_description (ARM_FP_TYPE_IWMMXT); diff --git a/gdbserver/linux-ppc-low.cc b/gdbserver/linux-ppc-low.cc index f8dd770b8eb3..fe44ba47992b 100644 --- a/gdbserver/linux-ppc-low.cc +++ b/gdbserver/linux-ppc-low.cc @@ -894,8 +894,8 @@ ppc_target::low_arch_setup () /* The value of current_process ()->tdesc needs to be set for this call. */ - ppc_hwcap = linux_get_hwcap (current_thread->id.pid (), features.wordsize); - ppc_hwcap2 = linux_get_hwcap2 (current_thread->id.pid (), features.wordsize); + ppc_hwcap = linux_get_hwcap (current_process ()->pid, features.wordsize); + ppc_hwcap2 = linux_get_hwcap2 (current_process ()->pid, features.wordsize); features.isa205 = ppc_linux_has_isa205 (ppc_hwcap); @@ -1097,7 +1097,7 @@ is_elfv2_inferior (void) const struct target_desc *tdesc = current_process ()->tdesc; int wordsize = register_size (tdesc, 0); - if (!linux_get_auxv (current_thread->id.pid (), wordsize, AT_PHDR, &phdr)) + if (!linux_get_auxv (current_process ()->pid, wordsize, AT_PHDR, &phdr)) return def_res; /* Assume ELF header is at the beginning of the page where program headers diff --git a/gdbserver/mem-break.cc b/gdbserver/mem-break.cc index c669842228d0..4cd7a3de093f 100644 --- a/gdbserver/mem-break.cc +++ b/gdbserver/mem-break.cc @@ -1392,7 +1392,7 @@ set_single_step_breakpoint (CORE_ADDR stop_at, ptid_t ptid) { struct single_step_breakpoint *bp; - gdb_assert (current_ptid.pid () == ptid.pid ()); + gdb_assert (current_process ()->pid == ptid.pid ()); bp = (struct single_step_breakpoint *) set_breakpoint_type_at (single_step_breakpoint, stop_at, NULL); diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc index 0b1141662ac6..15dda96965f1 100644 --- a/gdbserver/regcache.cc +++ b/gdbserver/regcache.cc @@ -106,7 +106,7 @@ void regcache_invalidate (void) { /* Only update the threads of the current process. */ - int pid = current_thread->id.pid (); + int pid = current_process ()->pid; regcache_invalidate_pid (pid); } diff --git a/gdbserver/server.cc b/gdbserver/server.cc index b6786baafadd..b6c23f280f86 100644 --- a/gdbserver/server.cc +++ b/gdbserver/server.cc @@ -1469,7 +1469,7 @@ handle_qxfer_exec_file (const char *annex, if (current_thread == NULL) return -1; - pid = pid_of (current_thread); + pid = current_process ()->pid; } else { diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc index 3f60989e4c7f..590210260bf5 100644 --- a/gdbserver/tracepoint.cc +++ b/gdbserver/tracepoint.cc @@ -6813,7 +6813,7 @@ static int run_inferior_command (char *cmd, int len) { int err = -1; - int pid = current_ptid.pid (); + int pid = current_process ()->pid; trace_debug ("run_inferior_command: running: %s", cmd); diff --git a/gdbserver/win32-i386-low.cc b/gdbserver/win32-i386-low.cc index f78e01206785..af96b464681b 100644 --- a/gdbserver/win32-i386-low.cc +++ b/gdbserver/win32-i386-low.cc @@ -57,7 +57,7 @@ x86_dr_low_set_addr (int regnum, CORE_ADDR addr) gdb_assert (DR_FIRSTADDR <= regnum && regnum <= DR_LASTADDR); /* Only update the threads of this process. */ - for_each_thread (current_thread->id.pid (), update_debug_registers); + for_each_thread (current_process ()->pid, update_debug_registers); } /* Update the inferior's DR7 debug control register from STATE. */ @@ -66,7 +66,7 @@ static void x86_dr_low_set_control (unsigned long control) { /* Only update the threads of this process. */ - for_each_thread (current_thread->id.pid (), update_debug_registers); + for_each_thread (current_process ()->pid, update_debug_registers); } /* Return the current value of a DR register of the current thread's From patchwork Fri Mar 31 03:44:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 67142 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 69A193858C53 for ; Fri, 31 Mar 2023 03:46:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69A193858C53 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680234380; bh=hJzD9p/orwam37ktrNcGd8AEKISD10vKiLPjnGpL6wE=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=DWhD5lbYmLute0DlpXJ4biXGegiQxsBbOZuZpvqesUAkUL2QXokAv9PbIFWwriqEt H9m0ZFUbNCxaxRdaWds4J3At0eCbudC+cJf0ElTFjnrKSHyztEmUEzkgCbNSlNsL4+ YtheglxpU7LY28VJRa09ohjVEfzOc18yP+Cf455I= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-oa1-x35.google.com (mail-oa1-x35.google.com [IPv6:2001:4860:4864:20::35]) by sourceware.org (Postfix) with ESMTPS id 7EF5F38582BD for ; Fri, 31 Mar 2023 03:45:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7EF5F38582BD Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-17ab3a48158so21984666fac.1 for ; Thu, 30 Mar 2023 20:45:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680234303; x=1682826303; 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=hJzD9p/orwam37ktrNcGd8AEKISD10vKiLPjnGpL6wE=; b=LMeymn2ibtsB1AdtS7Huvw3+46hcY3uNFAGhAW30aBXRh+qjh/HHyTvr26ucIl/kQO MORmkANociSYxzFmL8Hu5FQvXxrWGp1Bmm/oF28DA4NSdi0hGPVJ4uLjd8WAW03daBPL jvr9R2wVkCCYUlYMuzq/+0kuKrUIw5Ozs6iF0mLnd9O6AluGtZgn8N+1qCz/2hKeWS+p UYvkYyNScSINt389OhWvh0hWFlTAJPySx1QQRryTzMHEo93JSt4MlXsYxGrk0XM4LxSg MAUCcr/LKIbvwUmPdnI4IS5YxetreV6cVCk2O/ZzZgsBCoTySIVprEvxIHCQ1UXFTT8I 7+8w== X-Gm-Message-State: AO0yUKWKjXmQRt9zdnh+zNEzm+mkvFFeCvtSSp3pTJgSBhk8HZs4kxSw KiVWBWBShhbHJhVvxDLb/41evWjcZHWscjArAPg= X-Google-Smtp-Source: AKy350bCk6EFFgCfBVusK2DNCIVZRBpzKjL4hbjsZ8K3LY3ra7+WLdb/91ulXnnRPDN7+EVPIZbrog== X-Received: by 2002:a05:6870:64ac:b0:176:72e0:d45d with SMTP id cz44-20020a05687064ac00b0017672e0d45dmr16616562oab.16.1680234303722; Thu, 30 Mar 2023 20:45:03 -0700 (PDT) Received: from localhost ([177.124.15.55]) by smtp.gmail.com with ESMTPSA id sc15-20020a056871220f00b001729072f759sm612948oab.0.2023.03.30.20.45.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 20:45:03 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Thiago Jung Bauermann , Pedro Alves Subject: [PATCH 3/5] gdbserver/linux: Read auxv from any thread of the process Date: Fri, 31 Mar 2023 03:44:30 +0000 Message-Id: <20230331034432.3037148-4-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230331034432.3037148-1-thiago.bauermann@linaro.org> References: <20230331034432.3037148-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Thiago Jung Bauermann via Gdb-patches From: Thiago Jung Bauermann Reply-To: Thiago Jung Bauermann Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" If the initial thread of the process exits, reading the process' auxiliary vector via /proc/PID/auxv fails in one of two ways: 1. If gdbserver is root, then opening the file succeeds but reading from it returns 0 bytes. 2. If gdbserver isn't root, then opening the file fails with EACCES. This race isn't easy to run into because one of the first things that GDB does when connecting to gdbserver is to read the inferior's auxiliary vector and store it in the auxv cache. All further queries of the auxiliary vector will be served from there, unless one of the cache-clearing events ("inferior_exit", "inferior_appeared", "executable_changed") occurs. To fix the race condition iterate through tasks in /proc/PID/task/ and try to read their auxv file, returning the first one that succeeds. Suggested-by: Pedro Alves --- gdb/nat/linux-procfs.c | 67 ++++++++++++++++++++++++++++++++++++++++++ gdb/nat/linux-procfs.h | 7 +++++ gdbserver/linux-low.cc | 21 +++---------- 3 files changed, 78 insertions(+), 17 deletions(-) diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c index 9c2d1beb91fa..d53d3ea98bed 100644 --- a/gdb/nat/linux-procfs.c +++ b/gdb/nat/linux-procfs.c @@ -329,6 +329,73 @@ linux_proc_attach_tgid_threads (pid_t pid, /* See linux-procfs.h. */ +bool +linux_proc_read_auxv (pid_t pid, gdb_byte *readbuf, off_t offset, size_t len, + ssize_t &xfered_len) +{ + if (linux_proc_get_tgid (pid) != pid) + return false; + + std::string path_tasks = string_printf ("/proc/%ld/task", (long) pid); + gdb_dir_up dir (opendir (path_tasks.c_str ())); + if (dir == nullptr) + { + warning (_("Could not open /proc/%ld/task."), (long) pid); + return false; + } + + /* In a multi-threaded process, any given thread (including the initial one) + can exit at any time. Because of this, iterate through the threads trying + to read their auxv file and return the first one that succeeds. */ + struct dirent *dp; + while ((dp = readdir (dir.get ())) != nullptr) + { + /* Fetch one lwp. */ + unsigned long lwp = strtoul (dp->d_name, nullptr, 10); + if (lwp == 0) + continue; + + std::string path_auxv = string_printf ("/proc/%lu/auxv", lwp); + scoped_fd fd = gdb_open_cloexec (path_auxv, O_RDONLY, 0); + if (fd.get () < 0) + { + /* If the leader thread of the process exited and we aren't root, then + trying to read its auxv file results in EACCES error. */ + if (errno == EACCES) + continue; + + /* Unexpected failure. Give up. */ + return false; + } + + ssize_t l; + if (offset != 0 && lseek (fd.get (), offset, SEEK_SET) != offset) + l = -1; + else + l = read (fd.get (), readbuf, len); + + /* Unexpected failure. Give up. */ + if (l < 0) + return false; + /* The read call returns 0 if the leader thread of the process exited and + we are root, or if any thread exited after we opened its auxv file but + before we had a chance to read it. This only applies during the first + read into the file though (i.e., offset == 0). */ + else if (l != 0 || offset != 0) + { + xfered_len = l; + return true; + } + } + + /* If we get here, either all open call failed with EACCES (meaning the + threads are gone), or all read calls returned 0 (meaning either EOF or that + the threads are gone). */ + return true; +} + +/* See linux-procfs.h. */ + int linux_proc_task_list_dir_exists (pid_t pid) { diff --git a/gdb/nat/linux-procfs.h b/gdb/nat/linux-procfs.h index 639d8efaf0bf..bcdcedcdc2a9 100644 --- a/gdb/nat/linux-procfs.h +++ b/gdb/nat/linux-procfs.h @@ -80,6 +80,13 @@ extern int linux_proc_task_list_dir_exists (pid_t pid); extern const char *linux_proc_pid_to_exec_file (int pid); +/* Read the auxiliary vector for the PID process into READBUF which has LEN bytes, + starting at OFFSET. Returns true if successful, with XFERED_LEN indicating how many + bytes were read, and false on error. */ + +extern bool linux_proc_read_auxv (pid_t pid, gdb_byte *readbuf, off_t offset, + size_t len, ssize_t &xfered_len); + /* Display possible problems on this system. Display them only once per GDB execution. */ diff --git a/gdbserver/linux-low.cc b/gdbserver/linux-low.cc index e6a39202a98a..d42eb3a49ad7 100644 --- a/gdbserver/linux-low.cc +++ b/gdbserver/linux-low.cc @@ -5486,24 +5486,11 @@ int linux_process_target::read_auxv (int pid, CORE_ADDR offset, unsigned char *myaddr, unsigned int len) { - char filename[PATH_MAX]; - int fd, n; - - xsnprintf (filename, sizeof filename, "/proc/%d/auxv", pid); - - fd = open (filename, O_RDONLY); - if (fd < 0) - return -1; - - if (offset != (CORE_ADDR) 0 - && lseek (fd, (off_t) offset, SEEK_SET) != (off_t) offset) - n = -1; - else - n = read (fd, myaddr, len); - - close (fd); + ssize_t xfered_len; + bool rc = linux_proc_read_auxv ((pid_t) pid, (gdb_byte *) myaddr, + (off_t) offset, (size_t) len, xfered_len); - return n; + return rc ? (int) xfered_len : -1; } int From patchwork Fri Mar 31 03:44:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 67141 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 87568385781F for ; Fri, 31 Mar 2023 03:45:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87568385781F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680234354; bh=J28skYPVmzjAWA1dGyEtay8SXd2J00VvU+kkpBxUsqE=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Gm7kHL3XZRPCcdJYCHsiveeP16Y1RxSKGKa656zvhGkT+JQnX7Z6QSpcSgmsw35zX ZP+lNzOAg5k2ij9pwDZE2ze1pakRgHm9L62FvNjbD4VP2sLOFz5JyrPW6eh8yIYkk1 jAytWZJRD9D32rM/LwtMm76K2LQH2zXHKj4Ga1h8= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-oa1-x31.google.com (mail-oa1-x31.google.com [IPv6:2001:4860:4864:20::31]) by sourceware.org (Postfix) with ESMTPS id DACC73857438 for ; Fri, 31 Mar 2023 03:45:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DACC73857438 Received: by mail-oa1-x31.google.com with SMTP id 586e51a60fabf-17683b570b8so21918892fac.13 for ; Thu, 30 Mar 2023 20:45:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680234307; x=1682826307; 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=J28skYPVmzjAWA1dGyEtay8SXd2J00VvU+kkpBxUsqE=; b=HqNQx2L103Qvuhy80lljDfIzVrZVVsc33ssaUTQkVRfdtJc+I2H6qpgP9Yxaps/7o+ CI2nbAq0nSUeCE4ZmEu38gICjoc58Et4N/ujOzu/vQD3AlPXQMWvb7wpY1qSXJT2JcZj ylNFfED2qYpG/1fR+AAtIyP2etgmcsH0TjkdRXoJ1Xshb6pL0d6+FkyhctZcM6X8NlG/ +0Ylw55ZxqI8OJMzFShwn4MKE8a0y605Pd0s6ekk9NfsKFiyEYiBLBkCz9qat2sJpyZz C4kX6Pzyt9xAslp0hgGo9hKcl/fV/+fqjvSkn26NUaya5s3kJL+Op/X2xsQTBnFvNud0 iC+A== X-Gm-Message-State: AO0yUKXUYM2zfxtX434gnhixsUhvWXz0Wc7dsEZJ2tU8CjOkjjVEiGDE K1dnm3dH6NgculsY7BqE/0vDtdFLGMUP3IbikLw= X-Google-Smtp-Source: AKy350bbUdIM+yBBwt+Ir/X4BeWh040Y4BUPPSyFrIuv0pOYWSgo6BA18fZOwTT0JCuN+QsZkMJuRA== X-Received: by 2002:a05:6870:8917:b0:16a:b45a:e2d9 with SMTP id i23-20020a056870891700b0016ab45ae2d9mr15715577oao.7.1680234307294; Thu, 30 Mar 2023 20:45:07 -0700 (PDT) Received: from localhost ([177.124.15.55]) by smtp.gmail.com with ESMTPSA id zd8-20020a056871278800b0017eccc3fed9sm556304oab.47.2023.03.30.20.45.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 20:45:07 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Thiago Jung Bauermann , Pedro Alves Subject: [PATCH 4/5] gdb/linux-nat: Read auxv from any thread of the process Date: Fri, 31 Mar 2023 03:44:31 +0000 Message-Id: <20230331034432.3037148-5-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230331034432.3037148-1-thiago.bauermann@linaro.org> References: <20230331034432.3037148-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Thiago Jung Bauermann via Gdb-patches From: Thiago Jung Bauermann Reply-To: Thiago Jung Bauermann Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" If the initial thread of the process exits, reading the process' auxiliary vector via /proc/PID/auxv fails in one of two ways: 1. If GDB is root, then opening the file succeeds but reading from it returns 0 bytes. 2. If gdbserver isn't root, then opening the file fails with EACCES. This race isn't easy to run into because one of the first things that GDB does when starting an inferior is to read its auxiliary vector and store it in the auxv cache. All further queries of the auxiliary vector will be served from there, unless one of the cache-clearing events ("inferior_exit", "inferior_appeared", "executable_changed") occurs. To fix the race, use linux_proc_read_auxv instead of the generic procfs implementation. Suggested-by: Pedro Alves --- gdb/linux-nat.c | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index fd80fd975c14..938ae1c9b8c6 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -3667,6 +3667,33 @@ linux_xfer_siginfo (ptid_t ptid, enum target_object object, return TARGET_XFER_OK; } +/* Implement the to_xfer_partial target_ops method for TARGET_OBJECT_AUXV. This + function handles access via /proc/LWP/auxv, which allows handling possible + races in multi-threaded inferiors. */ + +static enum target_xfer_status +linux_nat_xfer_auxv (gdb_byte *readbuf, const gdb_byte *writebuf, + ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) +{ + /* Linux doesn't support writing to the auxv file. */ + if (readbuf == nullptr || writebuf != nullptr) + return TARGET_XFER_E_IO; + + ssize_t xfered; + bool rc = linux_proc_read_auxv (inferior_ptid.pid (), readbuf, (off_t) offset, + (size_t) len, xfered); + + if (!rc) + return TARGET_XFER_E_IO; + else if (xfered == 0) + return TARGET_XFER_EOF; + else + { + *xfered_len = (ULONGEST) xfered; + return TARGET_XFER_OK; + } +} + static enum target_xfer_status linux_nat_xfer_osdata (enum target_object object, const char *annex, gdb_byte *readbuf, @@ -3695,8 +3722,17 @@ linux_nat_target::xfer_partial (enum target_object object, return TARGET_XFER_EOF; if (object == TARGET_OBJECT_AUXV) - return memory_xfer_auxv (this, object, annex, readbuf, writebuf, - offset, len, xfered_len); + { + /* For attached inferiors, use memory_xfer_auxv's ld.so support which + works with virtual executables being executed by Valgrind's + memcheck. */ + if (current_inferior ()->attach_flag) + return memory_xfer_auxv (this, object, annex, readbuf, writebuf, + offset, len, xfered_len); + else + return linux_nat_xfer_auxv (readbuf, writebuf, offset, len, + xfered_len); + } if (object == TARGET_OBJECT_OSDATA) return linux_nat_xfer_osdata (object, annex, readbuf, writebuf, From patchwork Fri Mar 31 03:44:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 67143 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 6C8083857026 for ; Fri, 31 Mar 2023 03:46:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C8083857026 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1680234382; bh=Ac7/Oh+Oakk03EvK8CRN5RENmU65G5HTzB6gp91/uSk=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Jf3hHQsHOe/W7Uidq9EjQpTIdyEqhRY14Oi4UjFNFmVULyhYDqfYRSClew17W3PNB OFs/sh+Ea0nUEeuAc5+/PTVtyexye5icZPeLqP1LMPAYQ1V4WMxrc0TI+RrxYVxHT3 G6/rZ65GcsrqbEbjp0NFciCYFDJHCW9ECOGfAY3w= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-oa1-x36.google.com (mail-oa1-x36.google.com [IPv6:2001:4860:4864:20::36]) by sourceware.org (Postfix) with ESMTPS id EE8BF3857026 for ; Fri, 31 Mar 2023 03:45:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE8BF3857026 Received: by mail-oa1-x36.google.com with SMTP id 586e51a60fabf-177b78067ffso21959664fac.7 for ; Thu, 30 Mar 2023 20:45:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680234310; x=1682826310; 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=Ac7/Oh+Oakk03EvK8CRN5RENmU65G5HTzB6gp91/uSk=; b=jUdocrmXRp3yAQ1LFnAwrNZT/X5wvhYcwJwMC7jOMNYODjdV+oF756g1mbkphRM2GY 9Qgbv7hXaufGOZffKWfdnVnPWOuevU6Xaw1dRD3sh2A2B0Y0UuLAo2DCwSWbE1txfDiu q4IGJWDp9Gdye5Q8R3oR3ReT/T44xZ+q6btsYd+IrZ9SrOSQ0m1x66WzuyKvN9lisvOL s5v3Iga0/KhssPrXiV9FkK7pPH758U85Se8gEEfgC8HIk63I+j2WFNQPc+Lh0bajF9B2 uXbLc3UHP97UtquYaC6D+sp5rfhEYnm5HZQ8y9B2XWGbt2X6a5+rkxp25Bq7O9bfLvQN yKLg== X-Gm-Message-State: AO0yUKVBa+QiZchesDvP1DGSWLEnsbwOMeUwc+62dyeiLhWKzUPi04t8 JF6usV+t1gvC/7cM85ew2sm/peCShhOOWQn4JOw= X-Google-Smtp-Source: AKy350a2N3csG+0v3o2AFHOhZimMmbCLyWcP5lEiDFLx44UJkTrAsiHjUSh7F57+9SZapVpISWsuaA== X-Received: by 2002:a05:6870:b48b:b0:17a:b378:8e1d with SMTP id y11-20020a056870b48b00b0017ab3788e1dmr16263158oap.0.1680234310172; Thu, 30 Mar 2023 20:45:10 -0700 (PDT) Received: from localhost ([177.124.15.55]) by smtp.gmail.com with ESMTPSA id k21-20020a0568301bf500b006a17bffbc61sm685644otb.38.2023.03.30.20.45.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 20:45:09 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Thiago Jung Bauermann Subject: [PATCH 5/5] gdb/testsuite: Add test that reads auxv in a multi-threaded inferior Date: Fri, 31 Mar 2023 03:44:32 +0000 Message-Id: <20230331034432.3037148-6-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230331034432.3037148-1-thiago.bauermann@linaro.org> References: <20230331034432.3037148-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Thiago Jung Bauermann via Gdb-patches From: Thiago Jung Bauermann Reply-To: Thiago Jung Bauermann Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This test exercises reading the auxiliary vector in a program where the main thread exits before other threads. Because GDB's auxv cache makes it difficult to hit the race that this test exercises, add a maintenance command that flushes the cache. Also, move the fetch_auxv procedure from gdb.base/auxv to lib/gdb.exp so that the new testcase can use it. --- gdb/auxv.c | 16 ++++++++ gdb/testsuite/gdb.base/auxv.exp | 56 --------------------------- gdb/testsuite/gdb.threads/auxv.c | 62 ++++++++++++++++++++++++++++++ gdb/testsuite/gdb.threads/auxv.exp | 30 +++++++++++++++ gdb/testsuite/lib/gdb.exp | 62 ++++++++++++++++++++++++++++++ 5 files changed, 170 insertions(+), 56 deletions(-) create mode 100644 gdb/testsuite/gdb.threads/auxv.c create mode 100644 gdb/testsuite/gdb.threads/auxv.exp diff --git a/gdb/auxv.c b/gdb/auxv.c index 812b28075548..a39d8afd2990 100644 --- a/gdb/auxv.c +++ b/gdb/auxv.c @@ -27,6 +27,7 @@ #include "observable.h" #include "gdbsupport/filestuff.h" #include "objfiles.h" +#include "cli/cli-cmds.h" #include "auxv.h" #include "elf/common.h" @@ -602,6 +603,17 @@ info_auxv_command (const char *cmd, int from_tty) } } +/* Implement 'maint flush auxv' command. */ + +static void +auxv_flush_command (const char *command, int from_tty) +{ + /* Force-flush the auxv cache. */ + invalidate_auxv_cache(); + if (from_tty) + gdb_printf (_ ("Auxiliary vector cache flushed.\n")); +} + void _initialize_auxv (); void _initialize_auxv () @@ -610,6 +622,10 @@ _initialize_auxv () _("Display the inferior's auxiliary vector.\n\ This is information provided by the operating system at program startup.")); + add_cmd ("auxv-cache", class_maintenance, auxv_flush_command, + _ ("Force gdb to flush its auxiliary vector cache."), + &maintenanceflushlist); + /* Observers used to invalidate the auxv cache when needed. */ gdb::observers::inferior_exit.attach (invalidate_auxv_cache_inf, "auxv"); gdb::observers::inferior_appeared.attach (invalidate_auxv_cache_inf, "auxv"); diff --git a/gdb/testsuite/gdb.base/auxv.exp b/gdb/testsuite/gdb.base/auxv.exp index 89242b6f4321..7f58bd318fbc 100644 --- a/gdb/testsuite/gdb.base/auxv.exp +++ b/gdb/testsuite/gdb.base/auxv.exp @@ -59,62 +59,6 @@ set print_core_line [gdb_get_line_number "ABORT;"] gdb_test "tbreak $print_core_line" gdb_test continue ".*ABORT;.*" -proc fetch_auxv {test} { - global gdb_prompt - - set auxv_lines {} - set bad -1 - # Former trailing `\[\r\n\]+' may eat just \r leaving \n in the buffer - # corrupting the next matches. - if {[gdb_test_multiple "info auxv" $test { - -re "info auxv\r\n" { - exp_continue - } - -ex "The program has no auxiliary information now" { - set bad 1 - exp_continue - } - -ex "Auxiliary vector is empty" { - set bad 1 - exp_continue - } - -ex "No auxiliary vector found" { - set bad 1 - exp_continue - } - -re "^\[0-9\]+\[ \t\]+(AT_\[^ \t\]+)\[^\r\n\]+\r\n" { - lappend auxv_lines $expect_out(0,string) - exp_continue - } - -re "^\[0-9\]+\[ \t\]+\\?\\?\\?\[^\r\n\]+\r\n" { - warning "Unrecognized tag value: $expect_out(0,string)" - set bad 1 - lappend auxv_lines $expect_out(0,string) - exp_continue - } - -re "$gdb_prompt $" { - incr bad - } - -re "^\[^\r\n\]+\r\n" { - if {!$bad} { - warning "Unrecognized output: $expect_out(0,string)" - set bad 1 - } - exp_continue - } - }] != 0} { - return {} - } - - if {$bad} { - fail $test - return {} - } - - pass $test - return $auxv_lines -} - set live_data [fetch_auxv "info auxv on live process"] # Now try gcore. diff --git a/gdb/testsuite/gdb.threads/auxv.c b/gdb/testsuite/gdb.threads/auxv.c new file mode 100644 index 000000000000..5ad280145c89 --- /dev/null +++ b/gdb/testsuite/gdb.threads/auxv.c @@ -0,0 +1,62 @@ +/* Copyright 2023 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* This program creates an additional thread and then exits the leader thread. + It uses semaphores so that GDB can stop the program at specific points and + test how it deals with accessing the auxv information of a multi-threaded + program in different moments of the thread life cycle. */ + +#include +#include +#include +#include +#include +#include + +static volatile pthread_t main_thread; + +static void * +subthread (void *arg) +{ + int rc; + + rc = pthread_join (main_thread, NULL); + if (rc != 0) + fprintf (stderr, "Warning: pthread_join failed: %s\n", strerror (rc)); + + return NULL; /* Main thread exited. */ +} + +int +main (int argc, char *argv[]) +{ + int rc; + pthread_t thread_id; + + main_thread = pthread_self (); + + rc = pthread_create (&thread_id, NULL, &subthread, NULL); + if (rc != 0) + { + fprintf (stderr, "Error: pthread_create failed: %s\n", strerror (rc)); + return EXIT_FAILURE; + } + + pthread_exit (NULL); + /* NOTREACHED */ + return EXIT_FAILURE; +} diff --git a/gdb/testsuite/gdb.threads/auxv.exp b/gdb/testsuite/gdb.threads/auxv.exp new file mode 100644 index 000000000000..1ad3564642bd --- /dev/null +++ b/gdb/testsuite/gdb.threads/auxv.exp @@ -0,0 +1,30 @@ +# Test 'info auxv' and related functionality with a multi-threaded inferior. + +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +standard_testfile +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \ + {debug pthreads}] } { + return +} + +if ![runto ${srcfile}:[gdb_get_line_number "Main thread exited."]] { + return +} + +gdb_test -nopass "maintenance flush auxv-cache" "Auxiliary vector cache flushed." + +fetch_auxv "Get auxv after main thread exits" diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 14ce39e8ed72..8dd79ba5008a 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -7821,6 +7821,68 @@ proc get_var_address { var } { return "" } +# Retrieve the auxiliary vector from the inferior. +# +# Issues a pass/fail using TEST as the message, reflecting whether the "info auxv" +# command worked. +# +# Returns the command's output. +proc fetch_auxv {test} { + global gdb_prompt + + set auxv_lines {} + set bad -1 + # Former trailing `\[\r\n\]+' may eat just \r leaving \n in the buffer + # corrupting the next matches. + if {[gdb_test_multiple "info auxv" $test { + -re "info auxv\r\n" { + exp_continue + } + -ex "The program has no auxiliary information now" { + set bad 1 + exp_continue + } + -ex "Auxiliary vector is empty" { + set bad 1 + exp_continue + } + -ex "No auxiliary vector found" { + set bad 1 + exp_continue + } + -re "^\[0-9\]+\[ \t\]+(AT_\[^ \t\]+)\[^\r\n\]+\r\n" { + lappend auxv_lines $expect_out(0,string) + exp_continue + } + -re "^\[0-9\]+\[ \t\]+\\?\\?\\?\[^\r\n\]+\r\n" { + warning "Unrecognized tag value: $expect_out(0,string)" + set bad 1 + lappend auxv_lines $expect_out(0,string) + exp_continue + } + -re "$gdb_prompt $" { + incr bad + } + -re "^\[^\r\n\]+\r\n" { + if {!$bad} { + warning "Unrecognized output: $expect_out(0,string)" + set bad 1 + } + exp_continue + } + }] != 0} { + return {} + } + + if {$bad} { + fail $test + return {} + } + + pass $test + return $auxv_lines +} + # Return the frame number for the currently selected frame proc get_current_frame_number {{test_name ""}} { global gdb_prompt