From patchwork Tue Dec 3 10:52:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 102302 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 677723858CD9 for ; Tue, 3 Dec 2024 10:56:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 677723858CD9 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=AHL2/+Zn X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id F378C3858D35 for ; Tue, 3 Dec 2024 10:52:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F378C3858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F378C3858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1733223152; cv=none; b=D2HozODKNn++c46pr+KzoCboYbC0zcnJW9g4U223FWnIv2LH3oaqjEj0h+dLkcCBwoSPkvaJPHX68VaWlQ5rTl79wev+breLbYjbci7vO/2QVEeIaxjxFx4pBJ4jK4Hq2LtFHwKBENHoSzZ9oBWhriSj4iRwgpSzsMJgnepHs4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1733223152; c=relaxed/simple; bh=9IpcZnzS5o8h926zSojFFS94aXovCE9iHgSaAmaqLAI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=FL6V6TEtoM9YtnJa0Di3Pnr6OeAg/YshSObvq0oaz9mY/OSOjppm/GGYdRzIDhU0WxLGwO/ZM/i7QupqmjW6BUXD2Tvw03Q0ysChE3XIhgYRcnFrumKR7rYYgvUZoyH65aJ8DvdmJfE9NUeFoASv5SSc1TZx5PX6LpqIxwCkZ2E= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F378C3858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1733223151; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5Vp67ibIJzLYFRBEG99SY5debicw3JebLYrj1OeDSBc=; b=AHL2/+ZngxrSFXoYYzAesMzlKTymaufWKRFWz+pVO7zhZVzwOu/pHFNn2SVEtnZK/nMTNs ILTU52fpaFcGSeRqW1KYp0nlLJkjP4AopUOYdkhE4DZQqhlAsB6aeV/M0bCLCHHnyP/4Wd hsHyT1DjYOlH6kMY1oLtZrWTD573raY= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-400-M03owkg1OGm2BTpPZzIQoQ-1; Tue, 03 Dec 2024 05:52:30 -0500 X-MC-Unique: M03owkg1OGm2BTpPZzIQoQ-1 X-Mimecast-MFC-AGG-ID: M03owkg1OGm2BTpPZzIQoQ Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4349eeeb841so39373135e9.3 for ; Tue, 03 Dec 2024 02:52:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733223148; x=1733827948; 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=5Vp67ibIJzLYFRBEG99SY5debicw3JebLYrj1OeDSBc=; b=hrZ37ItJWtCZE5317ux++LkhcYk9m6pq2sMzp3VVUPgNTy8t2HxtqyrCR8F9BL6UjB EidRfMt12syW05sL7wUmhvb4e97B1WYlvUrAJDbxew26KuPnpB8cer18jMTQoajX4600 6o19IXqQNf1tJzbCebMaDv0N1oePFtmAs3mh1lnMoxxEqV9L9fjKIjRUwCKyPQ++B3pk M10FpC1iegEcJ1+lR5envxnFSRUNO4FMdiBCyAoRy3N9IGJN+m52InLlY+VhvAws7eRO Q+ArWcJKiompSa9Tlx7QFFyCGll5cehNgfjMYH61ObAq22O7L0GuFCtmEhO2zw/b7Pof KXTQ== X-Gm-Message-State: AOJu0Yx7oNlkD5PSlkhVp9qQPEM7SDumsRFL9hBKoEEx2CW3xuBzuGZG 74B9FEtAc7cYGMivDDm/C5YEyskDoVLF4IL/NXJCJfkFt+Kc1VX0Ih/91PYmTE8yricfXry9zMM tzu6Dnj0T4U1w4IY/EXS7ozwII4niAiPOxEC+LPAD96GA70Y8buwPFk9uKUzsuIocEcDpN1I9p9 LIpayE9EcH8czBAo0W5mww6VJ199RjYb44VkkeGC/rihY= X-Gm-Gg: ASbGncvSHgImrPU6ZBV0wBXEQkGTOIe97PAVp7L3ymIgug/vhVXntE4hcfdO9QUXqPF 6f6ip5aUc5Gv2iiHQxt2qdStXpcDNjDAkmiTgCvXQZCkenaW2A8zUcdcdJXXW2JfyAGBSYxzWRK 4QHDWEYxpf/tubvQIqXVM9RZCpAsmVIHh1ZlXNS021kXi+jqjT7qWnnn6frugTRkYT/3hCpHwcu IRuF2nx7C3QOMUrpz/aNONmCKUzxvyG59S2utPOQO0Sa8Tkw8rEU+IZTqUbxvl6Sm3xpyKVj9Vj 6g== X-Received: by 2002:a05:6000:698:b0:385:dea3:6059 with SMTP id ffacd0b85a97d-385fd429a01mr1618240f8f.49.1733223148022; Tue, 03 Dec 2024 02:52:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IFwfjWs9xkbaqixmRvq2TJIWyzCKR4EecXXuqVkjNPm0okBymLwmNZL1u2UUA/n6nPK2dq1kw== X-Received: by 2002:a05:6000:698:b0:385:dea3:6059 with SMTP id ffacd0b85a97d-385fd429a01mr1618214f8f.49.1733223147508; Tue, 03 Dec 2024 02:52:27 -0800 (PST) Received: from localhost (197.209.200.146.dyn.plus.net. [146.200.209.197]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-385e19104a0sm10602347f8f.32.2024.12.03.02.52.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Dec 2024 02:52:27 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv4 2/5] gdb: parse and set the inferior environment from core files Date: Tue, 3 Dec 2024 10:52:17 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: AFRCz-RVh4Bcs1iTC0lMlHhWg63iL-dZTgNI_U8mqIo_1733223149 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URIBL_BLOCKED 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 Extend the core file context parsing mechanism added in the previous commit to also store the environment parsed from the core file. This environment can then be injected into the inferior object. The benefit of this is that when examining a core file in GDB, the 'show environment' command will now show the environment extracted from a core file. Consider this example: $ env -i GDB_TEST_VAR=FOO ./gen-core Segmentation fault (core dumped) $ gdb -c ./core.1669829 ... [New LWP 1669829] Core was generated by `./gen-core'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x0000000000401111 in ?? () (gdb) show environment GDB_TEST_VAR=foo (gdb) There's a new test for this functionality. --- gdb/arch-utils.c | 26 ++++++++ gdb/arch-utils.h | 13 +++- gdb/corelow.c | 3 + gdb/linux-tdep.c | 6 +- .../gdb.base/corefile-exec-context.exp | 63 +++++++++++++++++++ 5 files changed, 106 insertions(+), 5 deletions(-) diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c index a2ed2a2c011..3cc6d9912d5 100644 --- a/gdb/arch-utils.c +++ b/gdb/arch-utils.c @@ -1499,6 +1499,32 @@ gdbarch_initialized_p (gdbarch *arch) return arch->initialized_p; } +/* See arch-utils.h. */ + +gdb_environ +core_file_exec_context::environment () const +{ + gdb_environ e; + + for (const auto &entry : m_environment) + { + char *eq = strchr (entry.get (), '='); + + /* If there's no '=' character, then skip this entry. */ + if (eq == nullptr) + continue; + + const char *value = eq + 1; + const char *var = entry.get (); + + *eq = '\0'; + e.set (var, value); + *eq = '='; + } + + return e; +} + void _initialize_gdbarch_utils (); void _initialize_gdbarch_utils () diff --git a/gdb/arch-utils.h b/gdb/arch-utils.h index 8d9f1625bdd..1c33bfb4704 100644 --- a/gdb/arch-utils.h +++ b/gdb/arch-utils.h @@ -21,6 +21,7 @@ #define ARCH_UTILS_H #include "gdbarch.h" +#include "gdbsupport/environ.h" class frame_info_ptr; struct minimal_symbol; @@ -88,9 +89,11 @@ struct core_file_exec_context found but not ARGV then use the no-argument constructor to create an empty context object. */ core_file_exec_context (gdb::unique_xmalloc_ptr exec_name, - std::vector> argv) + std::vector> argv, + std::vector> envp) : m_exec_name (std::move (exec_name)), - m_arguments (std::move (argv)) + m_arguments (std::move (argv)), + m_environment (std::move (envp)) { gdb_assert (m_exec_name != nullptr); } @@ -115,6 +118,9 @@ struct core_file_exec_context const std::vector> &args () const { return m_arguments; } + /* Return the environment variables from this context. */ + gdb_environ environment () const; + private: /* The executable filename as reported in the core file. Can be nullptr @@ -124,6 +130,9 @@ struct core_file_exec_context /* List of arguments. Doesn't include argv[0] which is the executable name, for this look at m_exec_name field. */ std::vector> m_arguments; + + /* List of environment strings. */ + std::vector> m_environment; }; /* Default implementation of gdbarch_displaced_hw_singlestep. */ diff --git a/gdb/corelow.c b/gdb/corelow.c index 0c799a670ee..e17058b1871 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -1069,6 +1069,9 @@ core_target_open (const char *arg, int from_tty) argv.push_back (a.get ()); gdb::array_view view (argv.data (), argv.size ()); current_inferior ()->set_args (view); + + /* And now copy the environment. */ + current_inferior ()->environment = ctx.environment (); } else { diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c index 8d506fe3ce1..6d90e89e3c4 100644 --- a/gdb/linux-tdep.c +++ b/gdb/linux-tdep.c @@ -2074,8 +2074,7 @@ linux_corefile_parse_exec_context_1 (struct gdbarch *gdbarch, bfd *cbfd) be pointing at the first environment string. */ ptr += ptr_bytes; - /* Parse the environment strings. Nothing is done with this yet, but - will be in a later commit. */ + /* Parse the environment strings. */ std::vector> environment; while ((v = deref (ptr)) != 0) { @@ -2092,7 +2091,8 @@ linux_corefile_parse_exec_context_1 (struct gdbarch *gdbarch, bfd *cbfd) return {}; return core_file_exec_context (std::move (execfn), - std::move (arguments)); + std::move (arguments), + std::move (environment)); } /* Parse and return execution context details from core file CBFD. */ diff --git a/gdb/testsuite/gdb.base/corefile-exec-context.exp b/gdb/testsuite/gdb.base/corefile-exec-context.exp index b18a8104779..ac97754fe71 100644 --- a/gdb/testsuite/gdb.base/corefile-exec-context.exp +++ b/gdb/testsuite/gdb.base/corefile-exec-context.exp @@ -100,3 +100,66 @@ gdb_test_multiple "core-file $corefile_2" "load core file with args" { # Also, the argument list should be available through 'show args'. gdb_test "show args" \ "Argument list to give program being debugged when it is started is \"$args\"\\." + +# Find the name of an environment variable that is not set. +set env_var_base "GDB_TEST_ENV_VAR_" +set env_var_name "" + +for { set i 0 } { $i < 10 } { incr i } { + set tmp_name ${env_var_base}${i} + if { ! [info exists ::env($tmp_name)] } { + set env_var_name $tmp_name + break + } +} + +if { $env_var_name eq "" } { + unsupported "couldn't find suitable environment variable name" + return -1 +} + +# Generate a core file with this environment variable set. +set env_var_value "TEST VALUE" +save_vars { ::env($env_var_name) } { + setenv $env_var_name $env_var_value + + set corefile [core_find $binfile {} $args] + if {$corefile == ""} { + untested "unable to create corefile" + return 0 + } +} +set corefile_3 "$binfile.2.core" +remote_exec build "mv $corefile $corefile_3" + +# Restart, load the core file, and check the environment variable +# shows up. +clean_restart $binfile + +# Check for environment variable VAR_NAME in the environment, its +# value should be VAR_VALUE. +proc check_for_env_var { var_name var_value } { + set saw_var false + gdb_test_multiple "show environment" "" { + -re "^$var_name=$var_value\r\n" { + set saw_var true + exp_continue + } + -re "^\[^\r\n\]*\r\n" { + exp_continue + } + -re "^$::gdb_prompt $" { + } + } + return $saw_var +} + +gdb_assert { ![check_for_env_var $env_var_name $env_var_value] } \ + "environment variable is not set before core file load" + +gdb_test "core-file $corefile_3" \ + "Core was generated by `[string_to_regexp $binfile] $args'\\.\r\n.*" \ + "load core file for environment test" + +gdb_assert { [check_for_env_var $env_var_name $env_var_value] } \ + "environment variable is set after core file load"