From patchwork Fri Sep 21 03:38:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 972828 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42GfTs0hfCz9sDN for ; Fri, 21 Sep 2018 13:39:13 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nKQNpI2I"; dkim-atps=neutral Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 42GfTr6FQYzF3Pd for ; Fri, 21 Sep 2018 13:39:12 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nKQNpI2I"; dkim-atps=neutral X-Original-To: pdbg@lists.ozlabs.org Delivered-To: pdbg@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gmail.com (client-ip=2607:f8b0:4864:20::641; helo=mail-pl1-x641.google.com; envelope-from=npiggin@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="nKQNpI2I"; dkim-atps=neutral Received: from mail-pl1-x641.google.com (mail-pl1-x641.google.com [IPv6:2607:f8b0:4864:20::641]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 42GfTl0MttzF3PQ for ; Fri, 21 Sep 2018 13:39:06 +1000 (AEST) Received: by mail-pl1-x641.google.com with SMTP id u11-v6so5299510plq.5 for ; Thu, 20 Sep 2018 20:39:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=eurQLhaSEewLuVSMLr4ig5qIR6ypFMLCA73ooRIg8yE=; b=nKQNpI2ItRXtEBtCTmEKaLOeWiRD3B0DSlbsXsKAIGBRh7XPOQeWyAgSMcbxVkNUwk o+1XU+aU7RA5I+aNaKHlaVkXUV/RJZ47/PY+YwKfiLyUcH/1Opv1QKoD+uW6MF2uDVM9 YJsQYJqgDCNCmQL889K9kAJc5pb7jb9byj67Cm1ekiKwNYYm7zG4p6SRVx8MUciepHFN Knamp8QHG4yqNaA6IzaZyOht1RjqBjTRfAeg4aPfWptZ289Oe5Te+BO0mLYlvbOjaXiS bgz3CiVo6pxmyW4AHXidOEQtrOaP5Wm4zWsJZ95QTThH/ZROKxYyewQN4sZnsx1J6dUE MspQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=eurQLhaSEewLuVSMLr4ig5qIR6ypFMLCA73ooRIg8yE=; b=obIttRjgrcuToJ05zlDFg2712orjUKdXfI6ENESlqjhdDILhDsMkYBWcJhmFIvnBre +Z0VVtYW1I0NmHe3Daba4iEqLRVb+aXEzLLaH7ak6M64izJh3FdmfaqkrpEQmoRjdlTc anj4SLN0NpakI2r4j/d3hUewJYHmAZiFv1Hd86JQ6zow8dZUj1qrqtF55S1UWOCDFkGM Y7ysNTULiVi77mZUgThYr43Q6NrXJFNOquKk0AVaSApTbbf4/sUan7hiVvMWOlGjabIt VR61AHh6aCvKpnNCzG6HpFoiVYZrPknF2IewSQGFQwxv7c8730xnBtasBIMsQlpij2DG oIZQ== X-Gm-Message-State: APzg51DsXNs5SO1F3aQHiN3XLCUkR44RRnx8AUB5c4qW0iE7hjZi7Emh KqNmdnORu6KpR/XULJQleWxxtbf4Trw= X-Google-Smtp-Source: ANB0VdZ36OlVOwTySzs6HO4BCukY86VBqamv6DYG3x7vh7wQ+9um6HAzL1QN/Tgj6UplgLGpsB0aJA== X-Received: by 2002:a17:902:d917:: with SMTP id c23-v6mr41993233plz.65.1537501144681; Thu, 20 Sep 2018 20:39:04 -0700 (PDT) Received: from roar.local0.net (14-202-184-212.tpgi.com.au. [14.202.184.212]) by smtp.gmail.com with ESMTPSA id r25-v6sm30603506pgm.59.2018.09.20.20.39.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 20:39:04 -0700 (PDT) From: Nicholas Piggin To: pdbg@lists.ozlabs.org Date: Fri, 21 Sep 2018 13:38:55 +1000 Message-Id: <20180921033855.31740-1-npiggin@gmail.com> X-Mailer: git-send-email 2.18.0 Subject: [Pdbg] [PATCH] stack: guess endian for stack frame walking X-BeenThere: pdbg@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "mailing list for https://github.com/open-power/pdbg development" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Nicholas Piggin MIME-Version: 1.0 Errors-To: pdbg-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Pdbg" The stack unwinder currently does not do any endian conversion, which means it won't work correctly if the stack does not match pdbg endian. This patch attempts an endian flip if the stack looks wrong, and goes with that if it's an improvement. This was nice for debugging skiboot. Signed-off-by: Nicholas Piggin --- src/thread.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/src/thread.c b/src/thread.c index d282307..6d568b9 100644 --- a/src/thread.c +++ b/src/thread.c @@ -105,6 +105,15 @@ static int load8(struct pdbg_target *target, uint64_t addr, uint64_t *value) return 1; } +uint64_t flip_endian(uint64_t v) +{ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + return be64toh(v); +#else + return le64toh(v); +#endif +} + static int dump_stack(struct thread_regs *regs) { struct pdbg_target *target; @@ -117,24 +126,67 @@ static int dump_stack(struct thread_regs *regs) break; } - printf("STACK:\n"); + printf("STACK: SP NIA\n"); if (!target) pdbg_log(PDBG_ERROR, "Unable to read memory (no ADU found)\n"); if (sp && is_real_address(regs, sp)) { - if (!load8(target, sp, &sp)) + uint64_t tmp; + bool flip = false; + bool be; + + if (!load8(target, sp, &tmp)) return 1; - while (sp && is_real_address(regs, sp)) { + + if (!tmp) { +badstack: + printf("SP:0x%016" PRIx64 " points to 0x%016" PRIx64 ", not decoding\n", sp, tmp); + return 0; + } + + /* Haphazard endian detection */ + if (tmp < sp || (tmp - sp > (1024*1024*1024))) { + uint64_t tmp2; + tmp2 = flip_endian(tmp); + if (tmp2 < sp || (tmp2 - sp > (1024*1024*1024))) + goto badstack; + sp = tmp2; + flip = true; + } else { + sp = tmp; + } + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + be = false; +#else + be = true; +#endif + if (flip) + be = !be; + if (be) + printf("Looks like big-endian\n"); + else + printf("Looks like little-endian\n"); + + while (sp) { + if (!is_real_address(regs, sp)) + break; + if (!load8(target, sp + 16, &pc)) return 1; + if (flip) + pc = flip_endian(pc); - printf(" 0x%016" PRIx64 " 0x%16" PRIx64 "\n", sp, pc); + printf(" 0x%016" PRIx64 " 0x%016" PRIx64 "\n", sp, pc); if (!load8(target, sp, &sp)) return 1; + if (flip) + sp = flip_endian(sp); } + } else { + printf("SP:0x%016" PRIx64 " does not appear to be a stack\n", sp); } - return 0; }