From patchwork Wed Jan 15 15:54:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 104841 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 8DD9F385276A for ; Wed, 15 Jan 2025 15:55:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8DD9F385276A 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=ULbRHmoX 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.129.124]) by sourceware.org (Postfix) with ESMTP id 6C9783852748 for ; Wed, 15 Jan 2025 15:54:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6C9783852748 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 6C9783852748 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736956483; cv=none; b=bbp8/BBi+L8EAnzGqsHhiycD71seN/JlWio0EGNAlsv95rvfDThCgMzyG/VHLlAH1N1pUrzTwC6CTwxLsV728+C3v47KcCkiqmWoH5s1CD5hijA1oo4dm0h4BtcqX7JSVW4T+G697QLJ2zP4fJpENG7VmwUDS6DrQ4fK1JGX43E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736956483; c=relaxed/simple; bh=3v2+EMmdnjXJQnbwgGCIkDgTmzjNMY0wU/SCgEGcE9A=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=tWDTt9T4mbjm+W+qeEW9rUN8TicOxkd77AwYr45M01PGjXzbmzRcEDLC+kFaWnxuy52w6D3uZhdLc/gvb9cHQ1ByH8zG7MMqP8jSdHPeic2AtJ4mzsbRgzMqhMT/IzZreeZUtrBRd2MORPt3p9EKO5kDplqHl7n8W4oKx+xbFRw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C9783852748 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736956483; 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; bh=gkR+BEgdiL8vy21yuK7xUO5f1F33/WgvngbI/89e7CU=; b=ULbRHmoX/pyJnmFLg9gT5TLJ8oAhzkvLA2GDl2oXfaVLUx3qNtsN9jgHcQ/DOEsqWLPag1 Zzbh71aFAyDedgwKJ0rdKTPwyqJREdNS+1GZzdLjk2ZrXne8VeNuT+U72DJ+eyIuFlvgyr VBD2LikxZ5Sf1vVEf1H/JOERiYzlKVI= 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-17-uXhHfG9QOBu8EGo0cW20kQ-1; Wed, 15 Jan 2025 10:54:41 -0500 X-MC-Unique: uXhHfG9QOBu8EGo0cW20kQ-1 X-Mimecast-MFC-AGG-ID: uXhHfG9QOBu8EGo0cW20kQ Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4361c040ba8so38566275e9.1 for ; Wed, 15 Jan 2025 07:54:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736956480; x=1737561280; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gkR+BEgdiL8vy21yuK7xUO5f1F33/WgvngbI/89e7CU=; b=VUBp5XSid1eOEj2HWcq2yGptEY8i3jZg5oz/Wtq/TsZMFSDMHNvTwsNYpTCOzWYHRZ IFUO5dK4jsCIEtOiUNIROQJ7fC6k0LmDzkDxrBis3NLb83LiOZm9naWatX+Qm2RA86hF m93P2V0+4PjU8wmjp1n3i4l9He0QXZc+b3/zuJ77TBTLpuYyk2sZl+sHzSq7WwVYzxvl SYmA1fjn0khSjiktmOzfQp5j44MsOkQ6v6A61NDbmAN6to38q2V83vQHHXV2tDSU7UyY 8WgoYgx1hW2x6CLaj116ekMyHalGI/zSd4PngTLsUkn0TTv5H4R3izjBB5ez5lbwWR5A h2Jw== X-Gm-Message-State: AOJu0YzcEPQSCmG5lY9Ai1DT1sbYmnR3clGZOCFWLGuaifowQLt1r7PX ROTQK4Xphirb25bVVP+ZaYd8TKjkaRdAwyz9kXkmHESsPfkNmGOGULYibo/XOuMXlrjdwQ6BnKe +iKWzBf1UnSWz1Udf2K/FkLTXEMN7Zi3Ez12fKBVMyp4ken23a2zArQPbvqkimlI6R4P8pq4SwQ 4haeSALFkEPU0Rho8If0j752R+UVWdulSqUOiWabC1WVc= X-Gm-Gg: ASbGnct50rJvC7Wsmvd+2TaHLfb06GPg/ZNu6RukQrqntXmF1XW+HJVigPoec68MSkD fTfRPbO7Ttssb0xsA0aYnUA8Gmth+NsPYqIdceBvHgkz6T5FZlvvRJpMoUhaymOIh53KP+3Tkc3 OFfqpNgtSDxLTlVVuJhbV1Hp68/ZoRn8Ifm6JwkJgUJX9hVb0tYuv0rNxeXXR9Uf2en9wGk2293 i4p9mH9lU/cMutWeiswuFXSU9J2xVlRODeH8DdNDvejf1R21pXxMBtbSmOLzhxyY4jZ0t2UBBii 98gwQw== X-Received: by 2002:a05:600c:4f81:b0:434:f1d5:1453 with SMTP id 5b1f17b1804b1-436e2531ec8mr293230805e9.0.1736956479761; Wed, 15 Jan 2025 07:54:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IGr+ArjdHygZxli4UdZMAifATGkEOqG95dHc5Hnu8uL7O25DQwLzgf+nHO+NGo2xPhDVLvPJg== X-Received: by 2002:a05:600c:4f81:b0:434:f1d5:1453 with SMTP id 5b1f17b1804b1-436e2531ec8mr293230575e9.0.1736956479304; Wed, 15 Jan 2025 07:54:39 -0800 (PST) Received: from localhost (44.226.159.143.dyn.plus.net. [143.159.226.44]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-437c74ab449sm27615815e9.10.2025.01.15.07.54.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 07:54:38 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb: quote inferior arguments, if needed, when opening a core file Date: Wed, 15 Jan 2025 15:54:37 +0000 Message-ID: <1ee9f2ab559de88bc6c02132542ec52c152b62c7.1736956470.git.aburgess@redhat.com> X-Mailer: git-send-email 2.47.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: oJFfHGMrPkITL493s9rndn1SWvE3GwCd8rvzsQMXZrA_1736956480 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.7 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, SPF_HELO_NONE, SPF_NONE, 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.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 This commit fixes an issue with the commit: commit d3d13bf876aae425ae0eff2ab0f1af9f7da0264a Date: Thu Apr 25 09:36:43 2024 +0100 gdb: add gdbarch method to get execution context from core file The above commit improves GDB's ability to display inferior arguments when opening a core file, however, if an argument includes white space, then this is not displayed as well as it should be. For example: (gdb) core-file /tmp/corefile-exec-context.2.core [New LWP 4069711] Reading symbols from /tmp/corefile-exec-context... Core was generated by `/tmp/corefile-exec-context aaaaa bbbbb ccccc ddddd e e e e e'. Program terminated with signal SIGABRT, Aborted. #0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50 50 return ret; (gdb) show args Argument list to give program being debugged when it is started is "aaaaa bbbbb ccccc ddddd e\ e\ e\ e\ e". (gdb) Notice the 'Core was generated by ...' line. In this case it is not clear if the "e e e e e" is a single argument containing white space, or 5 single arguments. But when we 'show args' it is immediately clear that this is a single argument, as the white space is now escaped. This problem was caused by the above commit building the argument string itself, and failing to consider white space escaping. This commit changes things around, first we place the arguments into the inferior, then, to print the 'Core was generated by ...' line, we ask the inferior for the argument string. In this way the quoting is handled just as it is for 'show args'. The initial output is now: (gdb) core-file /tmp/corefile-exec-context.2.core [New LWP 4069711] Reading symbols from /tmp/corefile-exec-context... Core was generated by `/tmp/corefile-exec-context aaaaa bbbbb ccccc ddddd e\ e\ e\ e\ e'. Program terminated with signal SIGABRT, Aborted. #0 0x00007f4f007af625 in raise () from /lib64/libc.so.6 (gdb) Much better. The existing test is extended to cover this case. Reviewed-By: Guinevere Larsen --- gdb/corelow.c | 21 +++++++---------- .../gdb.base/corefile-exec-context.exp | 23 ++++++++++++++++++- 2 files changed, 30 insertions(+), 14 deletions(-) base-commit: ac8f3fc9330da0302ebb491bf2bac8da5e035e35 diff --git a/gdb/corelow.c b/gdb/corelow.c index 9b9bee05bac..ee57a9c4865 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -1169,27 +1169,22 @@ core_target_open (const char *arg, int from_tty) if (ctx.valid ()) { - std::string args; - for (const auto &a : ctx.args ()) - { - args += ' '; - args += a.get (); - } - - gdb_printf (_("Core was generated by `%ps%s'.\n"), - styled_string (file_name_style.style (), - ctx.execfn ()), - args.c_str ()); - /* Copy the arguments into the inferior. */ std::vector argv; - for (const auto &a : ctx.args ()) + for (const gdb::unique_xmalloc_ptr &a : ctx.args ()) 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 (); + + /* Inform the user of executable and arguments. */ + const std::string &args = current_inferior ()->args (); + gdb_printf (_("Core was generated by `%ps%s%s'.\n"), + styled_string (file_name_style.style (), + ctx.execfn ()), + (args.length () > 0 ? " " : ""), args.c_str ()); } else { diff --git a/gdb/testsuite/gdb.base/corefile-exec-context.exp b/gdb/testsuite/gdb.base/corefile-exec-context.exp index 73e13e60d75..d6d4c0de4be 100644 --- a/gdb/testsuite/gdb.base/corefile-exec-context.exp +++ b/gdb/testsuite/gdb.base/corefile-exec-context.exp @@ -69,7 +69,7 @@ gdb_test_multiple "core-file $corefile_1" "load core file no args" { } # Generate a core file, this time pass some arguments to the inferior. -set args "aaaaa bbbbb ccccc ddddd eeeee" +set args "aaaaa bbbbb ccccc ddddd e\\\\ e\\\\ e\\\\ e\\\\ e" set corefile [core_find $binfile {} $args] if {$corefile == ""} { untested "unable to create corefile" @@ -101,6 +101,27 @@ gdb_test_multiple "core-file $corefile_2" "load core file with args" { gdb_test "show args" \ "Argument list to give program being debugged when it is started is \"$args\"\\." +# Move up to 'main'. Do it this way because we cannot know how many +# frames up 'main' actually is. +gdb_test_multiple "up" "move up to main" { + -re -wrap "#$decimal\\s+\[^\r\n\]+ in main .*" { + pass $gdb_test_name + } + + -re -wrap "#$decimal\\s+\[^\r\n\]+ in .*" { + send_gdb "up\n" + exp_continue + } +} + +# Check that the inferior was started with the expected arguments. +gdb_test "print argc" " = 6" +gdb_test "print argv\[1\]" " = $hex \"aaaaa\"" +gdb_test "print argv\[2\]" " = $hex \"bbbbb\"" +gdb_test "print argv\[3\]" " = $hex \"ccccc\"" +gdb_test "print argv\[4\]" " = $hex \"ddddd\"" +gdb_test "print argv\[5\]" " = $hex \"e e e e e\"" + # Find the name of an environment variable that is not set. set env_var_base "GDB_TEST_ENV_VAR_" set env_var_name ""