From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id A21CA3857C4F for ; Thu, 9 May 2024 14:59:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A21CA3857C4F 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 A21CA3857C4F 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=1715266752; cv=none; b=G7MO1neCT3imCzwOUNnwPVZh58VcEGnk0fL0U5dIzFpchgJxywwku4HXzikS2I66O+GTZO8h7yiQpYoC2wQHN1Vwda1n1cR9fOEikdS4pbDBOLyNCxrzDWW/tWedQXcn5vei12nyF68t4yXRKuXLSTlQCAb1b2/S0EnPVXd1v0Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715266752; c=relaxed/simple; bh=v/E7ipvN+l/YTnKBc4VNfWTOevRTEXRdSS50PQl70fc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=rR+SDf2awLPUs7tz+DHXxa2fWKyuAjxo1e1RT2/RG4p24NEKQgIYSq+4D/bnKLAakYWSLMWphEfkXbgI8pEkhcqogD8ZgH+rlJpn8wcKV+HTGD41/nwf9uJeftRi0xN1+eXJErAkIB8tJPAsWTrRtbBRi5wKwZoTXa3ow5tsZVI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1715266750; 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=/Adra7gXmhYDPBFJkosQHyZ122DZVsH7h3TjnPcRtUk=; b=B6uP2tqcHlpjaWYaM41Thj0C2g5rYLd6mhPQ+UlK3cLZ6NIFzaLtMRJWUzE3pI5VGXLGo2 1cFNI2F1Iue6t+vQXNInGzWFdlXe6IQj03Iji8/hufd+Nb5SBt2xj5WKYW0tr0f+Riq2fB 5lgp45QCb3HOFVqwtkDDpQupalhrnco= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-581-J8U8vN30MvWYHXZITh8dYw-1; Thu, 09 May 2024 10:59:09 -0400 X-MC-Unique: J8U8vN30MvWYHXZITh8dYw-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-41a14cb009eso6516935e9.3 for ; Thu, 09 May 2024 07:59:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715266747; x=1715871547; 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=/Adra7gXmhYDPBFJkosQHyZ122DZVsH7h3TjnPcRtUk=; b=cj7AzfJWwXLtlKnqrSOKRDwAX+3jjtOOOsek5K4nNChTFZlA3EIRTprMz8yuSz+zKd p0Qu2IfMTXFT7xJyHbWZDvnQ2sQ9lDm//PddPzmDd7xFkY98mb+FoMu3+BR8vsbE9X21 IcVa2e3vKe5GUsn5wOT9EOO7Jh6ODxUyK6oeBuAGgvtjjLzu4Y9zdtmxbxkMDTPUjXwF ENNvPxU1ToodBBFwTRxCYSXvu4ZrXRwxmGaQFoggbkfG0rC2/cXztLoHOlGrWlsn1WMu o8jEWAsgm2MBMJI2yZ1pJD8izfCffdax6NlhgA7BDoNey4VMG2sPB7iW5IL9djLF3td7 iWEA== X-Gm-Message-State: AOJu0Yw6lPpaLov3Ai1WcWWHqm6MVGQP9h3ucIlp6nUf+eMQnkzuzV2X ZkiRCYv6WZXw3QoGbpcLQwwBSCHnGVegE2CV8nhGFGWBEKN8Le1R3gK3O3/kiRCiQ1LC+AQ1fLM hsXHIqwZ1i7BmWueKhDXA4BfSPhL7tBYmfM2w/ESyNegMpNuMSnZ9gAm5c9dSp7NBuslIIvuicX 24WEXoqhjKOroaTOKZjBUjQqVzv4yQIpUyl/XW3InyaF8= X-Received: by 2002:a05:600c:3b28:b0:418:a706:3209 with SMTP id 5b1f17b1804b1-41f719d601amr67232185e9.31.1715266747031; Thu, 09 May 2024 07:59:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE7anFl4QCPYaRx0INe81K1k8hF9BXd8QPf3V7B/w1hhesLr2R205HJCayKqz6g8V2/r+hbJA== X-Received: by 2002:a05:600c:3b28:b0:418:a706:3209 with SMTP id 5b1f17b1804b1-41f719d601amr67231875e9.31.1715266746270; Thu, 09 May 2024 07:59:06 -0700 (PDT) Received: from localhost ([31.111.84.240]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-41fccee9335sm28229025e9.29.2024.05.09.07.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 May 2024 07:59:06 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH] gdb: add gdbarch_stack_grows_down function Date: Thu, 9 May 2024 15:59:04 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.0 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_H4,RCVD_IN_MSPIKE_WL,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 List-Id: In another patch I'm working on I needed to ask: does the stack grow down, or grow up? Looking around I found in infcall.c some code where we needed to ask the same question, what we do there is ask: gdbarch_inner_than (gdbarch, 1, 2) which should do the job. However, I don't particularly like copying this, it feels like we're asking something slightly different that just happens to align with the question we're actually asking. I propose adding a new function `gdbarch_stack_grows_down`. This is not going to be a gdbarch method that can be overridden, instead, this will just call the gdbarch_inner_than function. We already have some gdbarch methods like this, checkout arch-utils.c for examples. I think it's now clearer what we're actually doing. There should be no user visible changes after this commit. --- gdb/gdbarch.h | 13 +++++++++++++ gdb/infcall.c | 10 ++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h index 77d3406779f..70a6f43b06f 100644 --- a/gdb/gdbarch.h +++ b/gdb/gdbarch.h @@ -370,4 +370,17 @@ gdbarch_num_cooked_regs (gdbarch *arch) return gdbarch_num_regs (arch) + gdbarch_num_pseudo_regs (arch); } +/* Return true if stacks for ARCH grow down, otherwise return true. */ + +static inline bool +gdbarch_stack_grows_down (gdbarch *arch) +{ + /* The current assumption is that stacks either grow down, or they grow + up, so one of these checks should be true. */ + gdb_assert (gdbarch_inner_than (arch, 1, 2) + || gdbarch_inner_than (arch, 2, 1)); + + return gdbarch_inner_than (arch, 1, 2); +} + #endif diff --git a/gdb/infcall.c b/gdb/infcall.c index 23d5652dd21..edac9a74179 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -947,7 +947,7 @@ reserve_stack_space (const type *values_type, CORE_ADDR &sp) struct gdbarch *gdbarch = get_frame_arch (frame); CORE_ADDR addr = 0; - if (gdbarch_inner_than (gdbarch, 1, 2)) + if (gdbarch_stack_grows_down (gdbarch)) { /* Stack grows downward. Align STRUCT_ADDR and SP after making space. */ @@ -1128,7 +1128,7 @@ call_function_by_hand_dummy (struct value *function, address. AMD64 called that region the "red zone". Skip at least the "red zone" size before allocating any space on the stack. */ - if (gdbarch_inner_than (gdbarch, 1, 2)) + if (gdbarch_stack_grows_down (gdbarch)) sp -= gdbarch_frame_red_zone_size (gdbarch); else sp += gdbarch_frame_red_zone_size (gdbarch); @@ -1156,11 +1156,9 @@ call_function_by_hand_dummy (struct value *function, to pay :-). */ if (sp == old_sp) { - if (gdbarch_inner_than (gdbarch, 1, 2)) - /* Stack grows down. */ + if (gdbarch_stack_grows_down (gdbarch)) sp = gdbarch_frame_align (gdbarch, old_sp - 1); else - /* Stack grows up. */ sp = gdbarch_frame_align (gdbarch, old_sp + 1); } /* SP may have underflown address zero here from OLD_SP. Memory access @@ -1193,7 +1191,7 @@ call_function_by_hand_dummy (struct value *function, { CORE_ADDR lastval_addr = lastval->address (); - if (gdbarch_inner_than (gdbarch, 1, 2)) + if (gdbarch_stack_grows_down (gdbarch)) { gdb_assert (sp >= lastval_addr); sp = lastval_addr; base-commit: cba95c27876724059c3e99ea1857fb19b9cf8220 -- 2.25.4