From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id +RPPLgYUxmo0NA0AWB0awg (envelope-from ) for ; Wed, 07 Oct 2026 05:42:30 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=OeT+GKmJ; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=t7MxGtb9; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=OeT+GKmJ; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=t7MxGtb9; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id B46191E04E; Wed, 07 Oct 2026 05:42:30 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 922641E04E for ; Wed, 07 Oct 2026 05:42:29 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 344184BA2E22 for ; Wed, 7 Oct 2026 09:42:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 344184BA2E22 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=OeT+GKmJ; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=t7MxGtb9; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=OeT+GKmJ; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=t7MxGtb9 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id 42C674BA2E0B for ; Wed, 7 Oct 2026 09:41:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 42C674BA2E0B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 42C674BA2E0B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791366060; cv=none; b=SiYhqmkWEp/6yugsEy5weg/jHWDnlT9/BydpEENFMtE7cVaTvW0rTBXD9ql8SyxZzqyaxSLU9un37TuJEqUKGIzNhPo5TXn4/wDZm/AhaZ9DebZezGXOozgul9X1nGv+0cDbSmLVl8KlVxUgmOeG8zdBh1lzvH75V1kIuERme84= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791366060; c=relaxed/simple; bh=YsA68pj0IEOGyetuTOs01tQaKaZ78vbGM7Rl5coaPlc=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=UjvyrdoeS10NbYW/0+r6/ioSUCOLtw+cI/SzcLBXR096OdZRd6jbtwA8XyuyZKUF+KADZJ1YaBkgRYCjSmF85x8WFMUKy07Gh8MjJso7B8SPsQYWTj63PinPLoJ5b1A3y6D5G4qcFxgJUKZNSZBbSpVJ2xO1hxv5A5q26jQEtLA= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=OeT+GKmJ; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=t7MxGtb9; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=OeT+GKmJ; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=t7MxGtb9 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 42C674BA2E0B Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id C532D21F42 for ; Wed, 7 Oct 2026 09:40:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1791366058; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+aMk4609PBkZhHkC/S1kpXPEg7v9ucy1EXXKVihjSH0=; b=OeT+GKmJpdOm9cQ7ASVbhDR1cZ1zzspMxM2VnHGmcs6oMxoLLBFCI/V6YhpBPSCOkpURoV ejGsM2huMhUUPwJVP6ZDOhUydNxY5/eC6B9XIoK+CYwXi7FNUCF9BlS4lTvGeIvCccKigx SicXxw/JAJXZiccX2vLotTBdaBzkAHk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1791366058; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+aMk4609PBkZhHkC/S1kpXPEg7v9ucy1EXXKVihjSH0=; b=t7MxGtb9333zEFNS6RV2rRyC+ki8OJsUe17FUzfcugC8BRiVujHaNd0T3azv5CqVJAoVgW UzyKmujF9wAV3qBw== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1791366058; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+aMk4609PBkZhHkC/S1kpXPEg7v9ucy1EXXKVihjSH0=; b=OeT+GKmJpdOm9cQ7ASVbhDR1cZ1zzspMxM2VnHGmcs6oMxoLLBFCI/V6YhpBPSCOkpURoV ejGsM2huMhUUPwJVP6ZDOhUydNxY5/eC6B9XIoK+CYwXi7FNUCF9BlS4lTvGeIvCccKigx SicXxw/JAJXZiccX2vLotTBdaBzkAHk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1791366058; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+aMk4609PBkZhHkC/S1kpXPEg7v9ucy1EXXKVihjSH0=; b=t7MxGtb9333zEFNS6RV2rRyC+ki8OJsUe17FUzfcugC8BRiVujHaNd0T3azv5CqVJAoVgW UzyKmujF9wAV3qBw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 9271413B95 for ; Wed, 7 Oct 2026 09:40:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id JEXHFKoTxmo3EwAAD6G6ig:T2 (envelope-from ) for ; Wed, 07 Oct 2026 09:40:58 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH v4 01/11] [gdb/tui] Use early exit in tui_find_disassembly_address Date: Wed, 7 Oct 2026 11:40:47 +0200 Message-ID: <20261007094057.1811886-2-tdevries@suse.de> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20261007094057.1811886-1-tdevries@suse.de> References: <20261007094057.1811886-1-tdevries@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; ARC_NA(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; RCVD_TLS_ALL(0.00)[] 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~public-inbox=simark.ca@sourceware.org The function tui_find_disassembly_address handles forward and backward disassembling. The first part is small, so handle it in an early exit. No functional changes. --- gdb/tui/tui-disasm.c | 211 +++++++++++++++++++++---------------------- 1 file changed, 103 insertions(+), 108 deletions(-) diff --git a/gdb/tui/tui-disasm.c b/gdb/tui/tui-disasm.c index 1a1866003dd..e9f5969a6e6 100644 --- a/gdb/tui/tui-disasm.c +++ b/gdb/tui/tui-disasm.c @@ -192,16 +192,12 @@ tui_find_backward_disassembly_start_address (CORE_ADDR addr) static CORE_ADDR tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from) { - CORE_ADDR new_low; - int max_lines; - - max_lines = (from > 0) ? from : - from; + int max_lines = (from > 0) ? from : - from; if (max_lines == 0) return pc; std::vector asm_lines; - new_low = pc; if (from > 0) { /* Always disassemble 1 extra instruction here, then if the last @@ -210,116 +206,115 @@ tui_find_disassembly_address (struct gdbarch *gdbarch, CORE_ADDR pc, int from) tui_disassemble (gdbarch, asm_lines, pc, max_lines + 1); if (asm_lines.empty ()) return pc; - new_low = asm_lines.back ().addr; + return asm_lines.back ().addr; } - else + + /* In order to disassemble backwards we need to find a suitable + address to start disassembling from and then work forward until we + re-find the address we're currently at. We can then figure out + which address will be at the top of the TUI window after our + backward scroll. During our backward disassemble we need to be + able to distinguish between the case where the last address we + _can_ disassemble is ADDR, and the case where the disassembly + just happens to stop at ADDR, for this reason we increase + MAX_LINES by one. */ + max_lines++; + + /* When we disassemble a series of instructions this will hold the + address of the last instruction disassembled. */ + CORE_ADDR last_addr; + + /* And this will hold the address of the next instruction that would + have been disassembled. */ + CORE_ADDR next_addr; + + /* As we search backward if we find an address that looks like a + promising starting point then we record it in this structure. If + the next address we try is not a suitable starting point then we + will fall back to the address held here. */ + std::optional possible_new_low; + + /* The previous value of NEW_LOW so we know if the new value is + different or not. */ + CORE_ADDR prev_low; + + CORE_ADDR new_low = pc; + do { - /* In order to disassemble backwards we need to find a suitable - address to start disassembling from and then work forward until we - re-find the address we're currently at. We can then figure out - which address will be at the top of the TUI window after our - backward scroll. During our backward disassemble we need to be - able to distinguish between the case where the last address we - _can_ disassemble is ADDR, and the case where the disassembly - just happens to stop at ADDR, for this reason we increase - MAX_LINES by one. */ - max_lines++; - - /* When we disassemble a series of instructions this will hold the - address of the last instruction disassembled. */ - CORE_ADDR last_addr; - - /* And this will hold the address of the next instruction that would - have been disassembled. */ - CORE_ADDR next_addr; - - /* As we search backward if we find an address that looks like a - promising starting point then we record it in this structure. If - the next address we try is not a suitable starting point then we - will fall back to the address held here. */ - std::optional possible_new_low; - - /* The previous value of NEW_LOW so we know if the new value is - different or not. */ - CORE_ADDR prev_low; - - do - { - /* Find an address from which we can start disassembling. */ - prev_low = new_low; - new_low = tui_find_backward_disassembly_start_address (new_low); - - /* Disassemble forward. */ - next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines); - if (asm_lines.empty ()) - break; - last_addr = asm_lines.back ().addr; - - /* If disassembling from the current value of NEW_LOW reached PC - (or went past it) then this would do as a starting point if we - can't find anything better, so remember it. */ - if (last_addr >= pc && new_low != prev_low - && asm_lines.size () >= max_lines) - possible_new_low.emplace (new_low); - - /* Continue searching until we find a value of NEW_LOW from which - disassembling MAX_LINES instructions doesn't reach PC. We - know this means we can find the required number of previous - instructions then. */ - } - while ((last_addr > pc - || (last_addr == pc && asm_lines.size () < max_lines)) - && new_low != prev_low); + /* Find an address from which we can start disassembling. */ + prev_low = new_low; + new_low = tui_find_backward_disassembly_start_address (new_low); - /* If we failed to disassemble the required number of lines, try to fall - back to a previous possible start address in POSSIBLE_NEW_LOW. */ - if (asm_lines.size () < max_lines) - { - if (!possible_new_low.has_value ()) - return new_low; + /* Disassemble forward. */ + next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines); + if (asm_lines.empty ()) + break; + last_addr = asm_lines.back ().addr; - /* Take the best possible match we have. */ - new_low = *possible_new_low; - next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines); - } + /* If disassembling from the current value of NEW_LOW reached PC + (or went past it) then this would do as a starting point if we + can't find anything better, so remember it. */ + if (last_addr >= pc && new_low != prev_low + && asm_lines.size () >= max_lines) + possible_new_low.emplace (new_low); + + /* Continue searching until we find a value of NEW_LOW from which + disassembling MAX_LINES instructions doesn't reach PC. We + know this means we can find the required number of previous + instructions then. */ + } + while ((last_addr > pc + || (last_addr == pc && asm_lines.size () < max_lines)) + && new_low != prev_low); - /* The following walk forward assumes that ASM_LINES contains exactly - MAX_LINES entries. */ - gdb_assert (asm_lines.size () == max_lines); + /* If we failed to disassemble the required number of lines, try to fall + back to a previous possible start address in POSSIBLE_NEW_LOW. */ + if (asm_lines.size () < max_lines) + { + if (!possible_new_low.has_value ()) + return new_low; - /* Scan forward disassembling one instruction at a time until - the last visible instruction of the window matches the pc. - We keep the disassembled instructions in the 'lines' window - and shift it downward (increasing its addresses). */ - int pos = max_lines - 1; - last_addr = asm_lines.back ().addr; - if (last_addr < pc) - do - { - pos++; - if (pos >= max_lines) - pos = 0; - - CORE_ADDR old_next_addr = next_addr; - std::vector single_asm_line; - next_addr = tui_disassemble (gdbarch, single_asm_line, - next_addr, 1); - /* If there are some problems while disassembling exit. */ - if (next_addr <= old_next_addr) - return pc; - gdb_assert (single_asm_line.size () == 1); - asm_lines[pos] = single_asm_line[0]; - } while (next_addr <= pc); - pos++; - if (pos >= max_lines) - pos = 0; - new_low = asm_lines[pos].addr; - - /* When scrolling backward the addresses should move backward, or at - the very least stay the same if we are at the first address that - can be disassembled. */ - gdb_assert (new_low <= pc); + /* Take the best possible match we have. */ + new_low = *possible_new_low; + next_addr = tui_disassemble (gdbarch, asm_lines, new_low, max_lines); } + + /* The following walk forward assumes that ASM_LINES contains exactly + MAX_LINES entries. */ + gdb_assert (asm_lines.size () == max_lines); + + /* Scan forward disassembling one instruction at a time until + the last visible instruction of the window matches the pc. + We keep the disassembled instructions in the 'lines' window + and shift it downward (increasing its addresses). */ + int pos = max_lines - 1; + last_addr = asm_lines.back ().addr; + if (last_addr < pc) + do + { + pos++; + if (pos >= max_lines) + pos = 0; + + CORE_ADDR old_next_addr = next_addr; + std::vector single_asm_line; + next_addr = tui_disassemble (gdbarch, single_asm_line, + next_addr, 1); + /* If there are some problems while disassembling exit. */ + if (next_addr <= old_next_addr) + return pc; + gdb_assert (single_asm_line.size () == 1); + asm_lines[pos] = single_asm_line[0]; + } while (next_addr <= pc); + pos++; + if (pos >= max_lines) + pos = 0; + new_low = asm_lines[pos].addr; + + /* When scrolling backward the addresses should move backward, or at + the very least stay the same if we are at the first address that + can be disassembled. */ + gdb_assert (new_low <= pc); return new_low; } -- 2.51.0