From patchwork Tue Jul 11 16:14:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72502 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 C0DAA3857711 for ; Tue, 11 Jul 2023 16:15:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C0DAA3857711 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689092101; bh=iZp3m+sBCkNOVzu20zjujFdRq7FlsIfhI8HANjTguHw=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=jQwOWwzuC1dOL+Uj6vn9pSQ4TxxECTQrizOqxvONTxFuZMb/tY+C6/4kqCKrie616 f0EZYLiP5xLk/BMltv7MHY41+MMNbPZ2zhrfyfttqId7mExyaYMAlPOUkBLg71YPJY +sFndqWOfP63FbCuo9esEjHO3Hg4kQLc2sKJ4+0w= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd35.google.com (mail-io1-xd35.google.com [IPv6:2607:f8b0:4864:20::d35]) by sourceware.org (Postfix) with ESMTPS id 384253858D20 for ; Tue, 11 Jul 2023 16:14:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 384253858D20 Received: by mail-io1-xd35.google.com with SMTP id ca18e2360f4ac-7835ffc53bfso128168139f.1 for ; Tue, 11 Jul 2023 09:14:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689092048; x=1691684048; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iZp3m+sBCkNOVzu20zjujFdRq7FlsIfhI8HANjTguHw=; b=MFsZqCe86Tqnwu1ku4jYWbmOD/Sb/0bB9NvEMJbM7f8lexPPIyK7BQrdyMEyQBf/00 /VAWutVrlfsU8LYviqBiyQfGPG0mvNP5x2b+8WiT3uEsXkibEt1ybhp1ezXUPos7sW/K GWO+nVUNGWH6KVnS+QZCiKTStokZSlQwIm+hzWq6YNwI1fG0H0Xsx503UgYuTwShO2BV 8iR1L63nqMkcp5oaaLMe0MkwgBgUcdgNzeatVk8MY3pMqXEj9UEfUHDIzsQy0hPSwDB3 lhr+KK4yiw4yEnox7Unk3oWB7aUkuLX3D4H4vD/l9YnmVdxIsQ3ykmroePpiHcZfjkx4 ivWA== X-Gm-Message-State: ABy/qLYiHIx3ZFXGYNFGy8Xs1h2FMjQghteli+mBBSIzOawdGYUmU/uS s6UhSkAGWr5WXfoM4Gf+uYkDKZRkyfSaGphyEGyAZQ== X-Google-Smtp-Source: APBJJlGluggRecxpGtRa/+FBaXtwnrQaDPtF/4dR1v50qxLN1yBRFGJ5jRoB34A9tca3w2HOOUObKw== X-Received: by 2002:a05:6602:3e90:b0:783:58f2:b8f2 with SMTP id el16-20020a0566023e9000b0078358f2b8f2mr14526288iob.6.1689092048348; Tue, 11 Jul 2023 09:14:08 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id x3-20020a6bfe03000000b0077e40979b41sm702370ioh.45.2023.07.11.09.14.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 09:14:08 -0700 (PDT) Date: Tue, 11 Jul 2023 10:14:01 -0600 Subject: [PATCH v2 1/6] Minor cleanups in py-inferior.exp MIME-Version: 1.0 Message-Id: <20230711-py-inf-fixes-30615-v2-1-64a2540864e6@adacore.com> References: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> In-Reply-To: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 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, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" While working on this series, I noticed a some oddities in py-inferior.exp. One is an obviously incorrect comment, and the others are confusing test names. This patch fixes these. --- gdb/testsuite/gdb.python/py-inferior.exp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 58deece5d5f..576c2b716e6 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -206,8 +206,7 @@ if [isnative] { } } -# Test Inferior is_valid. This must always be the last test in -# this testcase as it kills the inferior. +# Test Inferior is_valid. with_test_prefix "is_valid" { gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get initial list" 1 @@ -247,7 +246,7 @@ with_test_prefix "is_valid" { gdb_test "python print (inf_list\[1\].is_valid())" "True" \ "check inferior validity 3" - gdb_test_no_output "remove-inferiors 2" "remove-inferiors 3" + gdb_test_no_output "remove-inferiors 2" gdb_test "python print (inf_list\[0\].is_valid())" "True" \ "check inferior validity 4" @@ -318,7 +317,7 @@ with_test_prefix "selected_inferior" { "print a connection object" gdb_test "inferior 1" ".*" "switch back to first inferior" - gdb_test_no_output "remove-inferiors 3" "remove second inferior" + gdb_test_no_output "remove-inferiors 3" } # Test repr()/str() From patchwork Tue Jul 11 16:14:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72500 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 C48C83857009 for ; Tue, 11 Jul 2023 16:14:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C48C83857009 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689092079; bh=Hc43djFECLk2fRZ3M6NkUPcvg60roiXJ8CgQZLcTDUs=; h=Date:Subject:References:In-Reply-To:To:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Qx1MM02zpf8Q+gRL5eQ/HaM+GQGD6l42olJVdVvy5KMSt9tQohJR8rkrkuhpezCOw MDiKiYxNapDnMnpJ7Y5F8/FEmZb+S0DUP8uI19uKiBjPBiWTEyHBeGDqPmlWj6a/y3 k+XJWcT0y0djtcuzG6Gkmug0RZgW98yp6UR3lZeQ= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by sourceware.org (Postfix) with ESMTPS id B03A23858D28 for ; Tue, 11 Jul 2023 16:14:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B03A23858D28 Received: by mail-io1-xd30.google.com with SMTP id ca18e2360f4ac-7866189cff1so271447539f.0 for ; Tue, 11 Jul 2023 09:14:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689092049; x=1691684049; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Hc43djFECLk2fRZ3M6NkUPcvg60roiXJ8CgQZLcTDUs=; b=C0dxJF2R3mTQgAljntEYVfigPUDIaA3EAttFbFC00khVHeicvQ5ACcQs/1fspis8ZZ WsF8AhxPjJ34HBYuI1FXF+RyUkBkejQx/RgyO0t+hS/pxJk2OT5OWDM2xLIV72mqfJqz hir6kPWaOu/ilARtgzN4ajwZpTAKu6slPRkgoDaVWpRAcfeUpvqd31Sb0pyTAQbq3L71 1S6xJGrimkvaCYodY6B2Cs++pwvjqPRV9fMyDf1QwWrTHzppmiXVMI6145F2GZNESoOH wxULXHl+SxqhhBxGY50yKVENLCCEQ3HRWxvFYvesmSgDsJRrf0t1BclqdpLvbjCg+vHe dlHA== X-Gm-Message-State: ABy/qLapXkK9LvbB8hJQvlNkivj++W67HF2vPxykoedMH3BJAnFCeKxK zkVswNE2QZt55kFjZbaVt1CKs9ENO6LeV7KTvDqLAw== X-Google-Smtp-Source: APBJJlE1218gRrmO1SjfIcFfQ4UUVN80437Dr7/ioEFd1wC3xP29QSuBZyIzUftb203xxQmd5ZCexw== X-Received: by 2002:a5e:8c17:0:b0:787:822:30f1 with SMTP id n23-20020a5e8c17000000b00787082230f1mr6947235ioj.1.1689092048946; Tue, 11 Jul 2023 09:14:08 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id x3-20020a6bfe03000000b0077e40979b41sm702370ioh.45.2023.07.11.09.14.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 09:14:08 -0700 (PDT) Date: Tue, 11 Jul 2023 10:14:02 -0600 Subject: [PATCH v2 2/6] Refactor py-inferior.exp MIME-Version: 1.0 Message-Id: <20230711-py-inf-fixes-30615-v2-2-64a2540864e6@adacore.com> References: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> In-Reply-To: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> To: gdb-patches@sourceware.org Cc: Pedro Alves X-Mailer: b4 0.12.3 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, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This changes py-inferior.exp to make it a bit more robust when adding new inferiors during the course of the test. Approved-By: Pedro Alves --- gdb/testsuite/gdb.python/py-inferior.exp | 43 +++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 576c2b716e6..723f212ecc3 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -40,6 +40,20 @@ if {![runto_main]} { return 0 } +# The most recently added inferior. +set most_recent_inf 1 + +# A helper function that adds a new inferior. It returns the expected +# number of the new inferior. ARG is a string to pass to +# add-inferior. +proc add_inferior {{arg ""}} { + global most_recent_inf + incr most_recent_inf + gdb_test "add-inferior $arg" "Added inferior $most_recent_inf.*" \ + "add inferior $most_recent_inf" + return $most_recent_inf +} + # Test basic gdb.Inferior attributes and methods. gdb_py_test_silent_cmd "python inferiors = gdb.inferiors ()" "get inferiors list" 1 @@ -234,7 +248,7 @@ with_test_prefix "is_valid" { "gdb.events.inferior_deleted.connect(del_inf_handler)" "" \ "end" "" - gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2" + set num [add_inferior] gdb_py_test_silent_cmd "python inf_list = gdb.inferiors()" "get new list" 1 gdb_test "python print (len(inf_list))" "2" "get inferior list length 2" gdb_test "python print (inf_list\[0\].is_valid())" "True" \ @@ -246,7 +260,7 @@ with_test_prefix "is_valid" { gdb_test "python print (inf_list\[1\].is_valid())" "True" \ "check inferior validity 3" - gdb_test_no_output "remove-inferiors 2" + gdb_test_no_output "remove-inferiors $num" gdb_test "python print (inf_list\[0\].is_valid())" "True" \ "check inferior validity 4" @@ -287,15 +301,16 @@ with_test_prefix "selected_inferior" { # Figure out if inf 1 has a native target. set inf_1_is_native [gdb_is_target_native] - gdb_test "add-inferior -no-connection" "Added inferior 3" "create new inferior" - gdb_test "inferior 3" ".*" "switch to third inferior" - gdb_test "py print (gdb.selected_inferior().num)" "3" "third inferior selected" + set num [add_inferior "-no-connection"] + gdb_test "inferior $num" ".*" "switch to inferior $num" + gdb_test "py print (gdb.selected_inferior().num)" "$num" \ + "inferior $num selected" gdb_test "py print (gdb.selected_inferior().connection_num)" "None" \ - "third inferior's None connection number" + "inferior $num's None connection number" gdb_test "py print (gdb.selected_inferior().connection)" "None" \ - "third inferior's None connection" + "inferior $num's None connection" gdb_test "target native" "Done. Use the \"run\" command to start a process." \ - "target for the third inferior" + "target for inferior $num" # If inf 1 has a native target, inf 3's target is shared with 1's. # Otherwise, it must have created a new target with a new number. @@ -306,10 +321,10 @@ with_test_prefix "selected_inferior" { } gdb_test "py print (gdb.selected_inferior().connection_num)" \ "$expected_connection_num" \ - "third inferior's native connection number" + "inferior $num's native connection number" gdb_test "py print (gdb.selected_inferior().connection.num)" \ "$expected_connection_num" \ - "third inferior's native connection number, though connection object" + "inferior $num's native connection number, though connection object" # Test printing of gdb.TargetConnection object. gdb_test "py print (gdb.selected_inferior().connection)" \ @@ -317,18 +332,18 @@ with_test_prefix "selected_inferior" { "print a connection object" gdb_test "inferior 1" ".*" "switch back to first inferior" - gdb_test_no_output "remove-inferiors 3" + gdb_test_no_output "remove-inferiors $num" } # Test repr()/str() with_test_prefix "__repr__" { - gdb_test "add-inferior" "Added inferior 4 on connection .*" "add inferior 4" + set num [add_inferior] gdb_py_test_silent_cmd "python infs = gdb.inferiors()" "get inferior list" 1 gdb_test "python print (infs\[0\])" "" gdb_test "python print (infs)" \ - "\\\(, \\\)" \ + "\\\(, \\\)" \ "print all inferiors 1" - gdb_test_no_output "remove-inferiors 4" + gdb_test_no_output "remove-inferiors $num" gdb_test "python print (infs)" \ "\\\(, \\\)" \ "print all inferiors 2" From patchwork Tue Jul 11 16:14:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72501 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 053B4385700B for ; Tue, 11 Jul 2023 16:14:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 053B4385700B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689092082; bh=fuzOD0S+gc2J0wbl8rnoIaPBlfhPGq445lXV4XZcONc=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=wmEnKXAGsjuT01v48cqEfiKvtT0YVtp63ztGy3iApo+aEeim8n8qnpPg7gIdt69GT RcUDMcgj0R252027vDPWLiVlseehqbGAq0AbXyrLmTq6LQYMTZ3Y3YGRYmkzqcpok+ KRK+NvLw7B5CIeVNWP0O5czQHIhgN4pf3/UxEIVE= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) by sourceware.org (Postfix) with ESMTPS id 37D383858C62 for ; Tue, 11 Jul 2023 16:14:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 37D383858C62 Received: by mail-io1-xd32.google.com with SMTP id ca18e2360f4ac-78372625badso271146539f.3 for ; Tue, 11 Jul 2023 09:14:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689092049; x=1691684049; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fuzOD0S+gc2J0wbl8rnoIaPBlfhPGq445lXV4XZcONc=; b=k16iUn0Ibu6K92veFkBe/06wFN/dJyirAsA7A5zwENt8VMtzM3aip4xgLq0iRUPTIR molsPvlW3/v7o6s4vTPRvxFVKH0pwmW+/THqXw8DeRPxTDt6mlBZ0T6JogfCy3WGH9nK b4Rjm0vT1JdJGFqwQ1tbLP2d9SoejHcB1O7uJSCcxorw9mVL70ApVE9qu3052ck8b91K 7S0A7fCv28a4jS51IYN5GyoLqws3EoY2Wew6kdjraMxwpHAR2/lqCeFG6kzqDo4wuCQO VaHwUeT/rjcW7JyRpBv1+2HrkuFYY1CCJ8xy6j1nCbtV0qc5Gw7B47y7+PUtD132aqeQ tC4A== X-Gm-Message-State: ABy/qLbJSFoUD3d8rBEbpoSxIErCENLSiz1tEzN3T+bftRCS7roMPLkk 23NyxO/3htOi9c4HbU0zgYHtBG785l/l7FTznwWHkw== X-Google-Smtp-Source: APBJJlHTyRapkwVPIq8WdeEC7IKWosHjAgGrlWFaHI1PbJBq8Goh+SC5y1i/apHQxT1bau+DyMJoXQ== X-Received: by 2002:a6b:5916:0:b0:787:169a:927 with SMTP id n22-20020a6b5916000000b00787169a0927mr5466034iob.20.1689092049486; Tue, 11 Jul 2023 09:14:09 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id x3-20020a6bfe03000000b0077e40979b41sm702370ioh.45.2023.07.11.09.14.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 09:14:09 -0700 (PDT) Date: Tue, 11 Jul 2023 10:14:03 -0600 Subject: [PATCH v2 3/6] Rename Python variable in py-inferior.exp MIME-Version: 1.0 Message-Id: <20230711-py-inf-fixes-30615-v2-3-64a2540864e6@adacore.com> References: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> In-Reply-To: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 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, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" py-inferior.exp creates a Python variable named 'str'. This clashes with the built-in type of the same name and can be confusing when trying to evaluate Python code when debugging the test case. This patch renames it. --- gdb/testsuite/gdb.python/py-inferior.exp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 723f212ecc3..8762b6992ca 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -88,13 +88,14 @@ gdb_continue_to_breakpoint "cont to Break here." ".*Break here\..*" gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \ "read str address" 0 -gdb_py_test_silent_cmd "python str = gdb.inferiors()\[0\].read_memory (addr, 5); print(str)" \ - "read str contents" 1 +gdb_test "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(astr)" \ + "" \ + "read str contents" gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0 -gdb_py_test_silent_cmd "python str\[1\] = a" "change str" 0 -gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, str)" \ +gdb_py_test_silent_cmd "python astr\[1\] = a" "change str" 0 +gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \ "write str" 1 -gdb_test "print (str)" " = \"hallo, testsuite\"" \ +gdb_test "print str" " = \"hallo, testsuite\"" \ "ensure str was changed in the inferior" # Test memory search. From patchwork Tue Jul 11 16:14:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72503 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 AD5CC385840B for ; Tue, 11 Jul 2023 16:15:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD5CC385840B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689092109; bh=CgBqoVXNBSK3YIGKKk0HC35fnVzOUOZqHhlLZ0FarDU=; h=Date:Subject:References:In-Reply-To:To:Cc:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Kt0E11hDMN9Q4ZnPeB0NoWGvozp1cvDJkEGtwzo4i2RXv9L+UgIAW1jI1WEVtTMHR vi2jM/PFvwqZo4FO1NK6ihWxLQsCnvsCiDxxfzB57pLZhTXeIFIg2YQ+JHRxle1RWU hjY/AxqcMSjVhLbPJMnqzpvPB0CQwED6bNS8sbzw= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd31.google.com (mail-io1-xd31.google.com [IPv6:2607:f8b0:4864:20::d31]) by sourceware.org (Postfix) with ESMTPS id CA3313858C1F for ; Tue, 11 Jul 2023 16:14:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CA3313858C1F Received: by mail-io1-xd31.google.com with SMTP id ca18e2360f4ac-77acb04309dso284134639f.2 for ; Tue, 11 Jul 2023 09:14:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689092050; x=1691684050; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CgBqoVXNBSK3YIGKKk0HC35fnVzOUOZqHhlLZ0FarDU=; b=PIleGuIhMpLZZjWMrylg4k+BtNNe+BBBKmD6XiJo3oejZIOn9vsxj3oyCOaWV7N+A7 0o7YF01j3RYF6M8+f68uDGNJeUOEY/nvK26eS+UUn/eG0fwLU0DUSxCVLRUqwb8mwE1k RD0fKARnO8HIQWQjK7ULQOonyWjM1AAysBYYKHVh5BdXl9A8P3fsxUq+ruFtPGdTDS1H xoA5hiG1yjaL/URY0gm4FQEy7M5x7N+UIdobo21V+H4+LHCPn91bpdvuLnzEo5B9DTNC DnYRMPTwhnOtnQRVyN0GnlzCEbroo/M9QrRfzeqgEUqTWhfSKP8yLDcWVqiqfwy/xyAR bApQ== X-Gm-Message-State: ABy/qLawYmSHy9NpujXTXRNKnOF2AwzcfQ9GjfIrj/xIuXca7AznQKjH PXU0ODdg+eM2k1SVfzfmGJMp56La9KbOeYC5vyBwVA== X-Google-Smtp-Source: APBJJlESqR4bnrLiwVVajRfuLMHrUl5uoX1m2BBVW2XrmA66hYMrsfKFEKo1aV13pc8gb9ZdTymfVg== X-Received: by 2002:a5e:9241:0:b0:787:8f3:65e7 with SMTP id z1-20020a5e9241000000b0078708f365e7mr7130029iop.10.1689092050030; Tue, 11 Jul 2023 09:14:10 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id x3-20020a6bfe03000000b0077e40979b41sm702370ioh.45.2023.07.11.09.14.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 09:14:09 -0700 (PDT) Date: Tue, 11 Jul 2023 10:14:04 -0600 Subject: [PATCH v2 4/6] Remove obsolete comment from gdbthread.h MIME-Version: 1.0 Message-Id: <20230711-py-inf-fixes-30615-v2-4-64a2540864e6@adacore.com> References: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> In-Reply-To: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> To: gdb-patches@sourceware.org Cc: Pedro Alves X-Mailer: b4 0.12.3 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, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" A comment in gdbthread.h refers to a global that no longer exists. Approved-By: Pedro Alves --- gdb/gdbthread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h index 7135515bf45..d294be6762b 100644 --- a/gdb/gdbthread.h +++ b/gdb/gdbthread.h @@ -764,7 +764,7 @@ extern int thread_count (process_stratum_target *proc_target); /* Return true if we have any thread in any inferior. */ extern bool any_thread_p (); -/* Switch context to thread THR. Also sets the STOP_PC global. */ +/* Switch context to thread THR. */ extern void switch_to_thread (struct thread_info *thr); /* Switch context to no thread selected. */ From patchwork Tue Jul 11 16:14:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72504 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 6877E3857702 for ; Tue, 11 Jul 2023 16:15:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6877E3857702 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689092137; bh=3TJyL9VXGTdhfrjnFX8NGe3dhiRDXcCizlSU3zWpqVA=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=H0GGJLaChWMCiNpZj7VsdqQl/ebOKOn+Vf5R61FBZBI46EnsS8eIQ5iRpMvp3a4Su nkKRYsfrSZDUyAxbHGkN0EY3k1CEozs0onI6AkHuD9Ia5mIq+Uddi6owSWlbns3e5m 9siHVbvij2bor3Bi7mTdloYDoJZWb3O9XfPpzBpQ= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd35.google.com (mail-io1-xd35.google.com [IPv6:2607:f8b0:4864:20::d35]) by sourceware.org (Postfix) with ESMTPS id 4F25D3858C31 for ; Tue, 11 Jul 2023 16:14:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4F25D3858C31 Received: by mail-io1-xd35.google.com with SMTP id ca18e2360f4ac-77ac14ff51bso200221839f.3 for ; Tue, 11 Jul 2023 09:14:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689092050; x=1691684050; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3TJyL9VXGTdhfrjnFX8NGe3dhiRDXcCizlSU3zWpqVA=; b=SR/u/WlCHnBtMtMB8f2SjXHIYxCBZqtLZ9u8kgQqATaqJtXYsc6rpWl2uAsgBNkx/x XuIF8stzws+0ggmnts4iPi0b054kVArWyCJetB96yj1cabnifM6MOe4CNuQWa9baAEkJ kYVZJJQStB+FzGEn21GPEAB5BKQDN59iQyuxvqhijEZrBFvjOUfgWyPmmGytenmyLfIi KeaWtlCLZ3nkowEubvLVvRERDw9MDfksw6TvS6kQjQJeBP9tx8rqwq8sW25YWtHohJKc 4GkCFWMwbd06TA7rXwlTEv3ShEu+m33sQPL7lnZnoo8Toapkx9Ic6oqg7ZYUi+Em/1h0 MUpA== X-Gm-Message-State: ABy/qLbhVMlkBoHZCc2JhN/nWSy5sULvYN31tBAHXAgWQxR5crrvRlyd jj4dztHwqP8V6mpsDSsCL3mQoKKfAeQmk2dcF0knyw== X-Google-Smtp-Source: APBJJlHNmYF5ZqdnkRj22StPQWN6ldxNHJ8e99RiyCAXZqH0Gf51G/E07NIL6ImeNgM/zMZZiDH0PQ== X-Received: by 2002:a5d:8894:0:b0:760:e308:107e with SMTP id d20-20020a5d8894000000b00760e308107emr15691018ioo.0.1689092050579; Tue, 11 Jul 2023 09:14:10 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id x3-20020a6bfe03000000b0077e40979b41sm702370ioh.45.2023.07.11.09.14.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 09:14:10 -0700 (PDT) Date: Tue, 11 Jul 2023 10:14:05 -0600 Subject: [PATCH v2 5/6] Introduce scoped_restore_current_inferior_for_memory MIME-Version: 1.0 Message-Id: <20230711-py-inf-fixes-30615-v2-5-64a2540864e6@adacore.com> References: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> In-Reply-To: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 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, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This introduces a new class, scoped_restore_current_inferior_for_memory, and arranges to use it in a few places. This class is intended to handle setting up and restoring the various globals that are needed to read or write memory -- but without invalidating the frame cache. I wasn't able to test the change to aix-thread.c. --- gdb/aix-thread.c | 18 ++++-------------- gdb/inferior.h | 29 +++++++++++++++++++++++++++++ gdb/proc-service.c | 10 ++-------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c index fbe80d656c2..74cc67c942b 100644 --- a/gdb/aix-thread.c +++ b/gdb/aix-thread.c @@ -615,13 +615,8 @@ pdc_read_data (pthdb_user_t user_current_pid, void *buf, /* This is needed to eliminate the dependency of current thread which is null so that thread reads the correct target memory. */ { - scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); - inferior_ptid = ptid_t (user_current_pid); - scoped_restore_current_inferior restore_inferior; - set_current_inferior (inf); - - scoped_restore_current_program_space restore_current_progspace; - set_current_program_space (inf->pspace); + scoped_restore_current_inferior_for_memory save_inferior + (inf, ptid_t (user_current_pid)); status = target_read_memory (addr, (gdb_byte *) buf, len); } ret = status == 0 ? PDC_SUCCESS : PDC_FAILURE; @@ -648,13 +643,8 @@ pdc_write_data (pthdb_user_t user_current_pid, void *buf, user_current_pid, (long) buf, hex_string (addr), len); { - scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); - inferior_ptid = ptid_t (user_current_pid); - scoped_restore_current_inferior restore_inferior; - set_current_inferior (inf); - - scoped_restore_current_program_space restore_current_progspace; - set_current_program_space (inf->pspace); + scoped_restore_current_inferior_for_memory save_inferior + (inf, ptid_t (user_current_pid)); status = target_write_memory (addr, (gdb_byte *) buf, len); } diff --git a/gdb/inferior.h b/gdb/inferior.h index caa8e4d494a..be76c456c8c 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -761,6 +761,35 @@ class scoped_restore_current_inferior inferior *m_saved_inf; }; +/* When reading memory from an inferior, the global inferior_ptid must + also be set. This class arranges to save and restore the necessary + state for reading or writing memory, but without invalidating the + frame cache. */ + +class scoped_restore_current_inferior_for_memory +{ +public: + + /* Save the current globals and switch to the given inferior and the + inferior's program space. PTID must name a thread in INF, it is + used as the new inferior_ptid. */ + scoped_restore_current_inferior_for_memory (inferior *inf, ptid_t ptid) + : m_save_ptid (&inferior_ptid) + { + set_current_inferior (inf); + set_current_program_space (inf->pspace); + inferior_ptid = ptid; + } + + DISABLE_COPY_AND_ASSIGN (scoped_restore_current_inferior_for_memory); + +private: + + scoped_restore_current_inferior m_save_inferior; + scoped_restore_current_program_space m_save_progspace; + scoped_restore_tmpl m_save_ptid; +}; + /* Traverse all inferiors. */ diff --git a/gdb/proc-service.c b/gdb/proc-service.c index 509836ec1a8..366e0510070 100644 --- a/gdb/proc-service.c +++ b/gdb/proc-service.c @@ -72,14 +72,8 @@ static ps_err_e ps_xfer_memory (const struct ps_prochandle *ph, psaddr_t addr, gdb_byte *buf, size_t len, int write) { - scoped_restore_current_inferior restore_inferior; - set_current_inferior (ph->thread->inf); - - scoped_restore_current_program_space restore_current_progspace; - set_current_program_space (ph->thread->inf->pspace); - - scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid); - inferior_ptid = ph->thread->ptid; + scoped_restore_current_inferior_for_memory save_inferior (ph->thread->inf, + ph->thread->ptid); CORE_ADDR core_addr = ps_addr_to_core_addr (addr); From patchwork Tue Jul 11 16:14:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 72505 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 2096B3858D20 for ; Tue, 11 Jul 2023 16:16:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2096B3858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1689092165; bh=iIjKZaQfkqNN1eXn7ctcu6LiA0El3H3QXZLH41kKF3k=; h=Date:Subject:References:In-Reply-To:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=oVezPGgMxGvFC+6FJ7EZzgUJalBLlR6sef8kwqqdFG1po1xp4+UFeNfkrXXBFHyVt PQWqiwrTDKqYLCzDOnTJXquGFCmWaDggsYPuhTp0yR8tQB07ih+EHqXhSC4I5Iy8/O M2kvKNLhvFfsYBkrFTn4Kr/mvcgcuhacoOdp82tI= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by sourceware.org (Postfix) with ESMTPS id 1202C3858408 for ; Tue, 11 Jul 2023 16:14:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1202C3858408 Received: by mail-io1-xd2a.google.com with SMTP id ca18e2360f4ac-77a62a84855so198605739f.1 for ; Tue, 11 Jul 2023 09:14:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689092051; x=1691684051; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iIjKZaQfkqNN1eXn7ctcu6LiA0El3H3QXZLH41kKF3k=; b=SvKEb8aMmQRaxzlGoyYhk+FbPwFHGhTd34rw3vu5mwHpXF9IpQQUh4kHmbqia7C91I cTSAB3MrPa2KyY59WEA60NtuUBoROkWvIyw9lGCTdZzQCiYPf4fAvU8vtEKynRG+xYTj ri1lM6TkVrlnWL9KB7yqozxId031sf5vKJxGnPeWYzQLyl9R8HAE3OCufknJLqM8Vao2 /JCfSMugSzP8fH1bPckM0HnDNxuXHj8BYneJ1QCK4d/xwXclBF07C2HP5/8ugWwfIFXY iDccYz/ODEW1EInX8Iv/BMIW2HoRWGsZv4+ZrX01j/pXglM8AZIMgpB9wbOC3lrk9wh7 vgsw== X-Gm-Message-State: ABy/qLZJRKlE6PfQpN8P6bLUHtVyfmMiyKGCKNWW27MW+rtNeyRm2mF+ ZISRA0ZOavuu4bKxMkzBvRR/NZ+6D3XZJx1vwr0zBw== X-Google-Smtp-Source: APBJJlHAo+4hqIndjcoMe00HRpZBYKJm1Vv0Dq2Q7pE4q1EQFhCkOXsNcPXIoFq8uBlCTTFhnH/lTA== X-Received: by 2002:a6b:ea15:0:b0:787:a73:b411 with SMTP id m21-20020a6bea15000000b007870a73b411mr6515850ioc.17.1689092051131; Tue, 11 Jul 2023 09:14:11 -0700 (PDT) Received: from localhost.localdomain (75-166-135-140.hlrn.qwest.net. [75.166.135.140]) by smtp.gmail.com with ESMTPSA id x3-20020a6bfe03000000b0077e40979b41sm702370ioh.45.2023.07.11.09.14.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 09:14:10 -0700 (PDT) Date: Tue, 11 Jul 2023 10:14:06 -0600 Subject: [PATCH v2 6/6] Use correct inferior in Inferior.read_memory et al MIME-Version: 1.0 Message-Id: <20230711-py-inf-fixes-30615-v2-6-64a2540864e6@adacore.com> References: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> In-Reply-To: <20230711-py-inf-fixes-30615-v2-0-64a2540864e6@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.3 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, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" A user noticed that Inferior.read_memory and a few other Python APIs will always use the currently selected inferior, not the one passed to the call. This patch fixes the bug by arranging to switch to the inferior. I found this same issue in several APIs, so this fixes them all. I also added a few missing calls to INFPY_REQUIRE_VALID to these methods. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30615 --- gdb/python/py-inferior.c | 28 ++++++++++++++++++++++------ gdb/testsuite/gdb.python/py-inferior.exp | 26 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index af8bd8855a3..46b9a32c87d 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -30,6 +30,7 @@ #include "gdbsupport/gdb_signals.h" #include "py-event.h" #include "py-stopevent.h" +#include "progspace-and-thread.h" #include using thread_map_t @@ -528,11 +529,14 @@ gdbpy_inferiors (PyObject *unused, PyObject *unused2) static PyObject * infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) { + inferior_object *inf = (inferior_object *) self; CORE_ADDR addr, length; gdb::unique_xmalloc_ptr buffer; PyObject *addr_obj, *length_obj; static const char *keywords[] = { "address", "length", NULL }; + INFPY_REQUIRE_VALID (inf); + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OO", keywords, &addr_obj, &length_obj)) return NULL; @@ -543,6 +547,9 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) try { + scoped_restore_current_inferior_for_memory restore_inferior + (inf->inferior, any_thread_of_inferior (inf->inferior)->ptid); + buffer.reset ((gdb_byte *) xmalloc (length)); read_memory (addr, buffer.get (), length); @@ -565,6 +572,7 @@ infpy_read_memory (PyObject *self, PyObject *args, PyObject *kw) static PyObject * infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) { + inferior_object *inf = (inferior_object *) self; struct gdb_exception except; Py_ssize_t buf_len; const gdb_byte *buffer; @@ -573,6 +581,8 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) static const char *keywords[] = { "address", "buffer", "length", NULL }; Py_buffer pybuf; + INFPY_REQUIRE_VALID (inf); + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "Os*|O", keywords, &addr_obj, &pybuf, &length_obj)) return NULL; @@ -591,6 +601,9 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) try { + scoped_restore_current_inferior_for_memory restore_inferior + (inf->inferior, any_thread_of_inferior (inf->inferior)->ptid); + write_memory_with_notification (addr, buffer, length); } catch (gdb_exception &ex) @@ -604,7 +617,7 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) } /* Implementation of - gdb.search_memory (address, length, pattern). ADDRESS is the + Inferior.search_memory (address, length, pattern). ADDRESS is the address to start the search. LENGTH specifies the scope of the search from ADDRESS. PATTERN is the pattern to search for (and must be a Python object supporting the buffer protocol). @@ -614,6 +627,7 @@ infpy_write_memory (PyObject *self, PyObject *args, PyObject *kw) static PyObject * infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) { + inferior_object *inf = (inferior_object *) self; struct gdb_exception except; CORE_ADDR start_addr, length; static const char *keywords[] = { "address", "length", "pattern", NULL }; @@ -624,6 +638,8 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) int found = 0; Py_buffer pybuf; + INFPY_REQUIRE_VALID (inf); + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "OOs*", keywords, &start_addr_obj, &length_obj, &pybuf)) @@ -656,6 +672,9 @@ infpy_search_memory (PyObject *self, PyObject *args, PyObject *kw) try { + scoped_restore_current_inferior_for_memory restore_inferior + (inf->inferior, any_thread_of_inferior (inf->inferior)->ptid); + found = target_search_memory (start_addr, length, buffer, pattern_size, &found_addr); @@ -911,11 +930,8 @@ infpy_get_main_name (PyObject *self, void *closure) { /* This is unfortunate but the implementation of main_name can reach into memory. */ - scoped_restore_current_inferior restore_inferior; - set_current_inferior (inf->inferior); - - scoped_restore_current_program_space restore_current_progspace; - set_current_program_space (inf->inferior->pspace); + scoped_restore_current_inferior_for_memory restore_inferior + (inf->inferior, any_thread_of_inferior (inf->inferior)->ptid); name = main_name (); } diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp index 8762b6992ca..eb501838117 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -91,6 +91,7 @@ gdb_py_test_silent_cmd "python addr = gdb.selected_frame ().read_var ('str')" \ gdb_test "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(astr)" \ "" \ "read str contents" +gdb_test "python print(astr\[0\])" .* gdb_py_test_silent_cmd "python a = bytes('a', 'ascii')" "" 0 gdb_py_test_silent_cmd "python astr\[1\] = a" "change str" 0 gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \ @@ -98,6 +99,10 @@ gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \ gdb_test "print str" " = \"hallo, testsuite\"" \ "ensure str was changed in the inferior" +# Add a new inferior here, so we can test that operations work on the +# correct inferior. +set num [add_inferior] + # Test memory search. set hex_number {0x[0-9a-fA-F][0-9a-fA-F]*} @@ -115,6 +120,9 @@ with_test_prefix "string" { gdb_test_no_output "py start_addr = search_buf.address" gdb_test_no_output "py length = search_buf.type.sizeof" + # Switch to the new inferior before testing. + gdb_test "inferior $num" ".*" "switch to inferior $num" + gdb_test "py print (gdb.inferiors()\[0\].search_memory (start_addr, length, 'aaa'))" \ "${one_pattern_found}" "find string pattern" @@ -128,6 +136,24 @@ with_test_prefix "string" { "${one_pattern_found}" "pattern found at end of range" } +# While still in the new inferior, test reading and writing memory +# again. +gdb_test "python astr = gdb.inferiors()\[0\].read_memory (addr, 5); print(astr)" \ + "" \ + "read str while other inferior selected" +gdb_test "python print(astr\[1\])" "b'a'" \ + "print a character from the string" +gdb_py_test_silent_cmd "python astr\[1\] = b'X'" "change str again" 0 +gdb_py_test_silent_cmd "python gdb.inferiors()\[0\].write_memory (addr, astr)" \ + "write str while other inferior selected" 1 + +gdb_test "inferior 1" ".*" "switch back to inferior 1" + +gdb_test "print str" " = \"hXllo, testsuite\"" \ + "ensure str was changed while other inferior selected" + +gdb_test_no_output "remove-inferiors $num" "remove-inferiors $num" + # Import struct to pack the following patterns. gdb_test_no_output "py from struct import *"