From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id 414993858D33 for ; Tue, 27 Jun 2023 16:38:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 414993858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-4f9fdb0ef35so4683379e87.0 for ; Tue, 27 Jun 2023 09:38:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687883926; x=1690475926; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=VTDZw934dyFboFQM2ZdoDHNUDzzfSh9LgvzBHfeQ6Cw=; b=eBejWQB6Jddl0PptOVOB1ADfYr+IcXakCAQ6j4vaJxNUXCimpY2PEV5oGYkU3xg9G4 bkUCjEq3EHVnxRQvRFacNgnr3yrushHaxk7zOUWfWdPg/foqUNcr6NGxjcZGWzOiVY1b rLTsc1UT91+sJo3oG88XfeGS/yIA4J8G/CX1aN7rSVu+ekeYOlJ31ngq+SEffB1dfoEC iDNn4x2SCDn0vPnDsigHy/rPIaNeOfsfDZmMNgox8e7dsiduOmkv48EPVnu/1eJ4g33V h1dNznkQ6skR89Sn/EKI1dI02eMBdeg6X4NYgrFnEemrNEXSOLgi5q099Zp6J75Hnjsw 7T8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687883926; x=1690475926; 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=VTDZw934dyFboFQM2ZdoDHNUDzzfSh9LgvzBHfeQ6Cw=; b=XZZff0PjjLwwPVSBgYSd2BRRsuSYbguPiVwJRGlfh/vIIDTZQNI9r9bEBX7itvW753 MksqM2dmJt5lscw07Ak7Mm3buHjF9yiaOwXRbmVb0vCULwhWpf4zjTtxjdsmBV4MNAAU PICOF1aiaWl3RWKTrZdNVDTT7EahcGaDXtJG5VzB+EqHAfkqOHDm4KM12sXuVQ4iUNBg fIG0YhpqDwXilWYbP188kUartoysXlnagJww4PFCJesBKNeCWUHGlo7k6+r46wVsSuq6 5L+nKLkV+M3J8PDN+05v3d07tRtx3ZRSPmDLgUTNzIvIvEheslfKcl6tQ490eDoTWYvV HkGA== X-Gm-Message-State: AC+VfDzdWRGjYicxFQS3OU/fKVn/M5jWGxvSglNyG5Dz+t2EPQGpohOm +6/aHHoZKQnJ5UW1j2ir9ureP0D9Fso= X-Google-Smtp-Source: ACHHUZ6iNtvi2vlNu53tGSaa11ozyyH72/T+8tMXbLWEsp7ds2pTgo/KiRkYcKLbTFC1U1yifGHOfg== X-Received: by 2002:a19:2d19:0:b0:4f8:6dbf:401d with SMTP id k25-20020a192d19000000b004f86dbf401dmr15458400lfj.57.1687883926129; Tue, 27 Jun 2023 09:38:46 -0700 (PDT) Received: from fedora.. (78-73-77-63-no2450.tbcn.telia.com. [78.73.77.63]) by smtp.gmail.com with ESMTPSA id u15-20020a056512040f00b004faa2de9877sm1335065lfk.286.2023.06.27.09.38.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Jun 2023 09:38:45 -0700 (PDT) From: Simon Farre To: gdb-patches@sourceware.org Cc: Simon Farre Subject: [PATCH v3] gdb/DAP Fix disassemble bug Date: Tue, 27 Jun 2023 18:38:36 +0200 Message-ID: <20230627163836.84269-1-simon.farre.cx@gmail.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,LIKELY_SPAM_BODY,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 List-Id: v3. This adds the ability to "disassemble backwards" by retrieving "known sources of truth" by using the gdb.Block data type, Block.start is always a valid position in executable code, no? If not, then gdb.Block and all code that relates to gdb.Block and it's underlying type, must also be invalid, so I assume here that Block.start is always "good", as it were. It would probably be nice to have a test for this if we don't already, but from my dog fooding experience with Midas+VSCode+GDB-DAP, it *seems* to be correct. But looks can be deceiving, I guess. v1. Fixes disassembleRequest The field instructionOffset can be negative. Previous patch made it so that sometimes the request got calculated to 0 instructions, when it meant to retrieve disasm for -50 to 0 (current position). --- gdb/python/lib/gdb/dap/disassemble.py | 44 +++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/gdb/python/lib/gdb/dap/disassemble.py b/gdb/python/lib/gdb/dap/disassemble.py index bc091eb2c89..ce016067b98 100644 --- a/gdb/python/lib/gdb/dap/disassemble.py +++ b/gdb/python/lib/gdb/dap/disassemble.py @@ -18,6 +18,39 @@ import gdb from .server import request, capability from .startup import send_gdb_with_response, in_gdb_thread +# Disassemble "backwards" +def disasm_backwards(arch: gdb.Architecture, end_pc: int, offset: int, count: int): + ins_at_pc = arch.disassemble(start_pc=end_pc)[0] + offset = abs(offset) + # We take an arbitrary jump backwards + # Guessing that an instruction averages at 8 bytes + start = end_pc - 8 * offset + instructions = [] + while len(instructions) < (offset + 1): + block = gdb.current_progspace().block_for_pc(start) + # Fail fast; if we can't find a block backwards + # fill all with "invalid values" + if block is None: + tmp = [] + for i in range(0, offset - len(instructions)): + tmp.append({"addr": 0, "asm": "unknown"}) + instructions = tmp + instructions + else: + ins = arch.disassemble(start_pc=block.start, end_pc=end_pc) + instructions = ins + instructions + start = start - 8 * (offset - len(instructions)) + end_pc = block.start + + # Disassembled instructions count is >= OFFSET+1 + diff = len(instructions) - offset + result = instructions[diff : diff + count] + # DAP seem to not want the actual instruction *at* end_pc + # when disassembling backwards + if result[-1]["addr"] == ins_at_pc["addr"]: + result.pop() + result = [instructions[diff - 1]] + result + return result[:count] + @in_gdb_thread def _disassemble(pc, skip_insns, count): @@ -27,8 +60,15 @@ def _disassemble(pc, skip_insns, count): # Maybe there was no frame. arch = gdb.selected_inferior().architecture() result = [] - total_count = skip_insns + count - for elt in arch.disassemble(pc, count=total_count)[skip_insns:]: + if skip_insns < 0: + ins = disasm_backwards(arch, pc, skip_insns, count) + skip_insns = 0 + count = count - len(ins) + result = [ + {"address": hex(elt["addr"]), "instruction": elt["asm"]} for elt in ins + ] + + for elt in arch.disassemble(pc, count=count + skip_insns)[skip_insns:]: result.append( { "address": hex(elt["addr"]), -- 2.41.0