From patchwork Wed Jun 18 05:54:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thiago Jung Bauermann X-Patchwork-Id: 897544 Delivered-To: patch@linaro.org Received: by 2002:a5d:5f54:0:b0:3a4:ee3f:8f15 with SMTP id cm20csp136783wrb; Tue, 17 Jun 2025 23:02:00 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX1ye4JQI9OqEPr/rJ5/XoxDOTdRyNezs0x1M3EZMBtZvn3IbXLoSa2ca8ks25/qzhXhNM0SQ==@linaro.org X-Google-Smtp-Source: AGHT+IFFzRRXQoGx0EILFEdkFi0X5HIf/a6JG1XHNMwDqT4Q6VaYxtMTNmzXoAArdDf/CBoUkIg7 X-Received: by 2002:a05:6102:4bc3:b0:4e5:996d:f23c with SMTP id ada2fe7eead31-4e7f61f1150mr12322162137.20.1750226520207; Tue, 17 Jun 2025 23:02:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1750226520; cv=pass; d=google.com; s=arc-20240605; b=czuDjjW+eYXhnl43Rn+m3L4IIL4dygMlFPdNAlVWDGEeJLcMyIc6hfXLecAC7HKejH qFIy9i8NnuRZhzt0AwoDebvDZ07Vz4yxxYCMfhvaXQ4g+Lvl5Jk9fCWygW5RdJZLT+dn g+4YL9mq8SdObegVIFAWy2BGDNkQiTWwtvEsKP15hws7SyBNISJTBce1hKW0uwNW2PlY I3awQvYUJbQ6VL6NyWUCQ9eH1ya9n1vTVVvAXf6AQuhBdF4VvVU0OC9RpWNboDshetwW hXdlEnbObpogMcQ96K/EiCpX9Hmw0DKqnRJR8j5V2q7ex6T4Dlu7QW/cMe8fYvlRJ6zC 4gfg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dkim-filter:arc-filter:dmarc-filter :delivered-to:dkim-filter; bh=MhUqNCE6cYIx7WzuNKIg+n1jjyKZEmm3GfiThxLvxrs=; fh=v0Swrqm/HGzmFfs2Lyn4KGEK39u4NVWMm4qGXeDZh/s=; b=iwU4D4/6aWQJwYXHjijoCyd5xdMvDJZp9bwhsZk6jv7UGDQeCz3dtJwdCuFG/AfYov t2BOgc04SW4kT2hCO6yMkH4E/MQyfSlZzelt7IOBJJu87oOn27GqGz4eKpED78ZfOiiP uMqZBJAlNKHvy4LxwOtskMTn29gSPaaHzoczR1iiC4QJqVuULb6v6kpvKjv1jCrfXV8C PG8gaARUoRheeFcGpU2aAxasezgPrvPm4FYvZzHzvSSCIm0KVwqFNu+wxvkBFmT/XVpu xfTTIxU4k/YrpN0GgKeZlbp9J3863/pi8juyQTS+L1+sY7If1pZp7LZoXb+RuMADHvXu dAtQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EpRa4qef; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gdb-patches-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ada2fe7eead31-4e7e710797bsi2765242137.515.2025.06.17.23.02.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 23:02:00 -0700 (PDT) Received-SPF: pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EpRa4qef; arc=pass (i=1); spf=pass (google.com: domain of gdb-patches-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gdb-patches-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9C515381695B for ; Wed, 18 Jun 2025 06:01:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9C515381695B Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=EpRa4qef X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id A1DDF389A094 for ; Wed, 18 Jun 2025 05:55:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A1DDF389A094 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A1DDF389A094 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::634 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750226113; cv=none; b=dXxCr51cp4Q5Jkk5X1zvtVTm7ykPoojJMSjApZHFWc/jcQHE3i216rF7i2Hp0oNEyajaQW6dITZiH3RnEzLFe1gCChRuB/wScToUteunpOpBOA4IhhDKO/v2wozhFEYW7Qmnaw2S1vgrVBBg/4KAiH6z4xTRaTd26zWrYdRncGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750226113; c=relaxed/simple; bh=DktTPmIiLZzbMILoPRrl5fMbGNj3uDbirKBeyqukvY8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qacd07lLVTKa8IS7LOWGFkntk9xc6a5vV7oX4PE4Tcst31imOex8hlUlC4m4mhix2bolI/Yr6WGeb393M8YG8d8O425uO/qpISMBtk1PpUNtLhI3iG5aL5VBw2qks/n6OwrRee6n806VaQokGzlcIowI9GkmiqzmzBW7bl/2V44= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A1DDF389A094 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2349f096605so87680305ad.3 for ; Tue, 17 Jun 2025 22:55:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1750226112; x=1750830912; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MhUqNCE6cYIx7WzuNKIg+n1jjyKZEmm3GfiThxLvxrs=; b=EpRa4qefrTQ+d0y2qEBHtDtGnAtP5KgpN1wcgvnkTV8eQaEH8s98EiT+hKAl2yTHG/ 0vSgU/obzsjLrAQyHHq5HlScEahB653GY3FS0AmmqdTOZXVk8No+TaZkDXRcJoh9oPqt Yxb73eEfc5akCgaA0+G//Q1yh5ZYcL5FFV9K0et0wiZ9kCE1NpI5qgV5v2UkEKzqMOwu BqdqEjoVUvxt+qwP18T7x8YdAExEdgBkpYTp2DFuwTTyIq+G/aFosXDcYhwLUJEei9sI i6sdPdapBbGn6b4rN7Dw8l9ZyLtqo6pa3Hfrav7nOPS6Pr0anwuXBhUSs8Q8OtDS9/xR iP4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750226112; x=1750830912; 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=MhUqNCE6cYIx7WzuNKIg+n1jjyKZEmm3GfiThxLvxrs=; b=hIVZ2xPJLRqRGf45z1p6+UrVfjSGrudUXICCC7YMZcDoerhR7nCS3wT9jFfNDjL/7w RfYdQRD+7+6gMDGOhIgWTF7D0lsJHGKFLfGVqUm76o8IzfFkuKXLd+jdGDkzZnTCWJme MzLFRSjmSJ+LZk8Zz22tI4/VC8AJxiJlp7D30aLaY0+kRJV6VOox+sWrIQn3UNA35qdI MpX8c4pZV4JCWJcDPz+igapAcoOqL/vPaY+u6qFgqKlqNASpn289RpQ4bGIqsdW5kPC/ I/0ZCpxNw6tMkbW/wq8t4OlFlXfqIbPYzf+x7Xb3acQ7LQ51bHwOqF1xXaiOm5h96LNd u2gA== X-Gm-Message-State: AOJu0YxUQlmbx/7BmG42J00yRsovusKc3MjNR5ZBw//LBXjn8KINrG2u w/XJOs6EvleFWhEkSocUEFSiYBvgYFoYZ3v7cbWaBX3RdN4qkPyc1wStuEvcz+VJSe4QFjAyrHY lWS7L X-Gm-Gg: ASbGncsLBR24N5VSSIOIAXmtKY2dtjIpQCof08bbKEsIqjRqcDdWP7hZLhPcR0t4ECW SWT4dMX9zA/PbAQIruAdxQ5s9uQr7fhDiorGfV5rcaxp+01mh09P7IQpUpKvdX/HfN1QHE0kyfw rvkdxgMXEtoceIDeGZ6OJ0bQOqFg3+vNlRIjKwUcMLcm3HLBzAAmMztSfhuHlVCaOdb+LlXMkWA CCBA1Z8vMx4f6WncoOA6j7wiZycLBN4fSjfH81kmMQ9QFG/FWHZzE+UpQRTvVloF5KhPF7j09sb lJUQRYf+ymA4NcsWSQk0bPtAijGEMVLYDp7grxPWR+y+EBNITRZiBflcGUCibmQIGDbTH6LQ6W8 A9dk= X-Received: by 2002:a17:903:19cc:b0:235:f078:4733 with SMTP id d9443c01a7336-2366b3135f2mr211510815ad.8.1750226112478; Tue, 17 Jun 2025 22:55:12 -0700 (PDT) Received: from localhost ([2804:14d:7e39:88d6:ae12:97ab:a9:43c]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2365d8a1a89sm91481125ad.81.2025.06.17.22.55.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 22:55:11 -0700 (PDT) From: Thiago Jung Bauermann To: gdb-patches@sourceware.org Cc: Eli Zaretskii , "Schimpe, Christina" , Luis Machado Subject: [PATCH v2 8/9] GDB: aarch64-linux: Support unwinding the GCSPR Date: Wed, 18 Jun 2025 02:54:44 -0300 Message-ID: <20250618055445.709416-9-thiago.bauermann@linaro.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250618055445.709416-1-thiago.bauermann@linaro.org> References: <20250618055445.709416-1-thiago.bauermann@linaro.org> MIME-Version: 1.0 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~patch=linaro.org@sourceware.org The GCSPR is almost always updated implicitly by the hardware, so the compiler doesn't generate DWARF unwind information for it. Therefore add an unwinding function that calculates the value of the GCSPR in the previous frame based on its value in this frame. Some sanity checking is done by confirming that the calculated value is within a Guarded Control Stack memory area. This function is the same as amd64_linux_dwarf2_prev_ssp, written by Christina Schimpe to unwind Intel's SSP register. The gdb.arch/aarch64-gcs-return.exp testcase is lightly adapted from gdb.arch/amd64-shadow-stack-cmds.exp. --- gdb/aarch64-linux-tdep.c | 54 ++++++- gdb/aarch64-tdep.c | 6 + gdb/aarch64-tdep.h | 4 + gdb/testsuite/gdb.arch/aarch64-gcs-return.c | 105 ++++++++++++++ gdb/testsuite/gdb.arch/aarch64-gcs-return.exp | 132 ++++++++++++++++++ 5 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.arch/aarch64-gcs-return.c create mode 100644 gdb/testsuite/gdb.arch/aarch64-gcs-return.exp The patch is new in this series. The code and testcase are lightly adapted from: [PATCH v4 07/11] gdb: Handle shadow stack pointer register unwinding for amd64 linux. https://inbox.sourceware.org/gdb-patches/20250617121147.1956686-8-christina.schimpe@intel.com/ diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c index 63c6f6b76b92..2257b33d1e5f 100644 --- a/gdb/aarch64-linux-tdep.c +++ b/gdb/aarch64-linux-tdep.c @@ -32,6 +32,7 @@ #include "symtab.h" #include "tramp-frame.h" #include "trad-frame.h" +#include "dwarf2/frame.h" #include "target.h" #include "target/target.h" #include "expop.h" @@ -2561,6 +2562,54 @@ aarch64_linux_get_shadow_stack_pointer (gdbarch *gdbarch, regcache *regcache, return gcspr; } +/* Implement Guarded Control Stack Pointer Register unwinding. For each + previous GCS pointer check if its address is still in the GCS memory + range. If it's outside the range set the returned value to unavailable, + otherwise return a value containing the new GCS pointer. */ + +static value * +aarch64_linux_dwarf2_prev_gcspr (const frame_info_ptr &this_frame, + void **this_cache, int regnum) +{ + value *v = frame_unwind_got_register (this_frame, regnum, regnum); + gdb_assert (v != nullptr); + + gdbarch *gdbarch = get_frame_arch (this_frame); + + if (v->entirely_available () && !v->optimized_out ()) + { + int size = register_size (gdbarch, regnum); + bfd_endian byte_order = gdbarch_byte_order (gdbarch); + CORE_ADDR gcspr = extract_unsigned_integer (v->contents_all ().data (), + size, byte_order); + + /* Starting with v6.13, the Linux kernel supports Guarded Control + Stack. Using /proc/PID/smaps we can only check if the current + GCSPR points to GCS memory. Only if this is the case a valid + previous GCS pointer can be calculated. */ + std::pair range; + if (linux_address_in_shadow_stack_mem_range (gcspr, &range)) + { + /* The GCS grows downwards. To compute the previous GCS pointer, + we need to increment the GCSPR. */ + CORE_ADDR new_gcspr = gcspr + 8; + + /* If NEW_GCSPR points to the end of or before (<=) the current + GCS memory range we consider NEW_GCSPR as valid. */ + if (new_gcspr <= range.second) + return frame_unwind_got_address (this_frame, regnum, new_gcspr); + } + } + + /* Return a value which is marked as unavailable in case we could not + calculate a valid previous GCS pointer. */ + value *retval + = value::allocate_register (get_next_frame_sentinel_okay (this_frame), + regnum, register_type (gdbarch, regnum)); + retval->mark_bytes_unavailable (0, retval->type ()->length ()); + return retval; +} + /* AArch64 Linux implementation of the report_signal_info gdbarch hook. Displays information about possible memory tag violations. */ @@ -3134,8 +3183,11 @@ aarch64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) aarch64_use_target_description_from_corefile_notes); if (tdep->has_gcs_linux ()) - set_gdbarch_get_shadow_stack_pointer (gdbarch, + { + set_gdbarch_get_shadow_stack_pointer (gdbarch, aarch64_linux_get_shadow_stack_pointer); + tdep->fn_prev_gcspr = aarch64_linux_dwarf2_prev_gcspr; + } } #if GDB_SELF_TEST diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c index e21c4a8e9ae1..c479a0b70323 100644 --- a/gdb/aarch64-tdep.c +++ b/gdb/aarch64-tdep.c @@ -1408,6 +1408,12 @@ aarch64_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum, return; } } + if (tdep->has_gcs () && tdep->fn_prev_gcspr != nullptr + && regnum == tdep->gcs_reg_base) + { + reg->how = DWARF2_FRAME_REG_FN; + reg->loc.fn = tdep->fn_prev_gcspr; + } } /* Implement the execute_dwarf_cfa_vendor_op method. */ diff --git a/gdb/aarch64-tdep.h b/gdb/aarch64-tdep.h index 54ca641a35a3..99e7d26ce4ab 100644 --- a/gdb/aarch64-tdep.h +++ b/gdb/aarch64-tdep.h @@ -23,6 +23,7 @@ #define GDB_AARCH64_TDEP_H #include "arch/aarch64.h" +#include "dwarf2/frame.h" #include "displaced-stepping.h" #include "infrun.h" #include "gdbarch.h" @@ -190,6 +191,9 @@ struct aarch64_gdbarch_tdep : gdbarch_tdep_base available. */ int gcs_linux_reg_base = -1; + /* Function to unwind the GCSPR from the given frame. */ + fn_prev_register fn_prev_gcspr = nullptr; + /* Returns true if the target supports GCS. */ bool has_gcs () const diff --git a/gdb/testsuite/gdb.arch/aarch64-gcs-return.c b/gdb/testsuite/gdb.arch/aarch64-gcs-return.c new file mode 100644 index 000000000000..c6ade8af7e9f --- /dev/null +++ b/gdb/testsuite/gdb.arch/aarch64-gcs-return.c @@ -0,0 +1,105 @@ +/* This test program is part of GDB, the GNU debugger. + + Copyright 2025 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 . */ + +#include +#include +#include +#include +#include + +/* Feature check for Guarded Control Stack. */ +#ifndef HWCAP_GCS +#define HWCAP_GCS (1UL << 32) +#endif + +#ifndef PR_GET_SHADOW_STACK_STATUS +#define PR_GET_SHADOW_STACK_STATUS 74 +#define PR_SET_SHADOW_STACK_STATUS 75 +#define PR_SHADOW_STACK_ENABLE (1UL << 0) +#endif + +/* We need to use a macro to call prctl because after GCS is enabled, it's not + possible to return from the function which enabled it. This is because the + return address of the calling function isn't on the GCS. */ +#define my_syscall2(num, arg1, arg2) \ + ({ \ + register long _num __asm__("x8") = (num); \ + register long _arg1 __asm__("x0") = (long)(arg1); \ + register long _arg2 __asm__("x1") = (long)(arg2); \ + register long _arg3 __asm__("x2") = 0; \ + register long _arg4 __asm__("x3") = 0; \ + register long _arg5 __asm__("x4") = 0; \ + \ + __asm__ volatile("svc #0\n" \ + : "=r"(_arg1) \ + : "r"(_arg1), "r"(_arg2), "r"(_arg3), "r"(_arg4), \ + "r"(_arg5), "r"(_num) \ + : "memory", "cc"); \ + _arg1; \ + }) + +static int __attribute__ ((noinline)) +call2 () +{ + return 42; /* Break call2. */ +} + +static int __attribute__ ((noinline)) +call1 () +{ + return call2 (); /* Break call1. */ +} + +int +main () +{ + if (!(getauxval (AT_HWCAP) & HWCAP_GCS)) + { + fprintf (stderr, "GCS support not found in AT_HWCAP\n"); + return EXIT_FAILURE; + } + + /* Force shadow stacks on, our tests *should* be fine with or + without libc support and with or without this having ended + up tagged for GCS and enabled by the dynamic linker. We + can't use the libc prctl() function since we can't return + from enabling the stack. Also lock GCS if not already + locked so we can test behaviour when it's locked. */ + unsigned long gcs_mode; + int ret = my_syscall2 (__NR_prctl, PR_GET_SHADOW_STACK_STATUS, &gcs_mode); + if (ret) + { + fprintf (stderr, "Failed to read GCS state: %d\n", ret); + return EXIT_FAILURE; + } + + if (!(gcs_mode & PR_SHADOW_STACK_ENABLE)) + { + gcs_mode = PR_SHADOW_STACK_ENABLE; + ret = my_syscall2 (__NR_prctl, PR_SET_SHADOW_STACK_STATUS, gcs_mode); + if (ret) + { + fprintf (stderr, "Failed to configure GCS: %d\n", ret); + return EXIT_FAILURE; + } + } + + call1 (); /* Break main. */ + + /* Avoid returning, in case libc doesn't understand GCS. */ + exit (EXIT_SUCCESS); +} diff --git a/gdb/testsuite/gdb.arch/aarch64-gcs-return.exp b/gdb/testsuite/gdb.arch/aarch64-gcs-return.exp new file mode 100644 index 000000000000..717cc305c25c --- /dev/null +++ b/gdb/testsuite/gdb.arch/aarch64-gcs-return.exp @@ -0,0 +1,132 @@ +# Copyright 2025 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 . + +# Test the GDB return command in a program that uses a Guarded Control Stack. +# Based on the return tests in gdb.arch/amd64-shadow-stack-cmds.exp. + +require allow_aarch64_gcs_tests + +standard_testfile + +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { + return +} + +set main_line [gdb_get_line_number "Break main"] +set call1_line [gdb_get_line_number "Break call1"] +set call2_line [gdb_get_line_number "Break call2"] + +if { ![runto ${main_line}] } { + return +} + +proc restart_and_run_infcall_call2 {} { + global binfile call2_line + clean_restart ${binfile} + if { ![runto_main] } { + return + } + set inside_infcall_str "The program being debugged stopped while in a function called from GDB" + gdb_breakpoint ${call2_line} + gdb_continue_to_breakpoint "Break call2" ".*Break call2.*" + gdb_test "call (int) call2()" \ + "Breakpoint \[0-9\]*, call2.*$inside_infcall_str.*" +} + +with_test_prefix "test inferior call and continue" { + gdb_breakpoint ${call1_line} + gdb_continue_to_breakpoint "Break call1" ".*Break call1.*" + + gdb_test "call (int) call2()" "= 42" + + gdb_continue_to_end +} + +with_test_prefix "test return inside an inferior call" { + restart_and_run_infcall_call2 + + gdb_test "return" "\#0.*call2.*" \ + "Test GCS return inside an inferior call" \ + "Make.*return now\\? \\(y or n\\) " "y" + + gdb_continue_to_end +} + +with_test_prefix "test return 'above' an inferior call" { + restart_and_run_infcall_call2 + + gdb_test "frame 2" "call2 ().*" "move to frame 'above' inferior call" + + gdb_test "return" "\#0.*call1.*" \ + "Test GCS return 'above' an inferior call" \ + "Make.*return now\\? \\(y or n\\) " "y" + + gdb_continue_to_end +} + +clean_restart ${binfile} +if { ![runto ${main_line}] } { + return +} + +# Extract GCS pointer inside main, call1 and call2 function. +gdb_breakpoint ${call1_line} +gdb_breakpoint ${call2_line} +set gcspr_main [get_valueof /x "\$gcspr" 0 "get value of gcspr in main"] +gdb_continue_to_breakpoint "Break call1" ".*Break call1.*" +set gcspr_call1 [get_valueof /x "\$gcspr" 0 "get value of gcspr in call1"] +gdb_continue_to_breakpoint "Break call2" ".*Break call2.*" +set gcspr_call2 [get_valueof /x "\$gcspr" 0 "get value of gcspr in call2"] + +with_test_prefix "test frame level update" { + gdb_test "up" "call1.*" "move to frame 1" + gdb_test "print /x \$gcspr" "= $gcspr_call1" "check gcspr of frame 1" + gdb_test "up" "main.*" "move to frame 2" + gdb_test "print /x \$gcspr" "= $gcspr_main" "check gcspr of frame 2" + gdb_test "frame 0" "call2.*" "move to frame 0" + gdb_test "print /x \$gcspr" "= $gcspr_call2" "check gcspr of frame 0" +} + +with_test_prefix "test return from current frame" { + gdb_test "return (int) 1" "#0.*call1.*" \ + "Test GCS return from current frame" \ + "Make.*return now\\? \\(y or n\\) " "y" + + # Potential GCS violations often only occur after resuming normal + # execution. Therefore, it is important to test normal program + # continuation after testing the return command. + gdb_continue_to_end +} + +clean_restart ${binfile} +if { ![runto_main] } { + return +} + +with_test_prefix "test return from past frame" { + gdb_breakpoint ${call2_line} + gdb_continue_to_breakpoint "Break call2" ".*Break call2.*" + + gdb_test "frame 1" ".*in call1.*" + + gdb_test "return (int) 1" "#0.*main.*" \ + "Test GCS return from past frame" \ + "Make.*return now\\? \\(y or n\\) " "y" + + # Potential GCS violations often only occur after resuming normal + # execution. Therefore, it is important to test normal program + # continuation after testing the return command. + gdb_continue_to_end +}