From patchwork Fri Sep 21 04:34:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 972846 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42Ggjj6tmyz9sDN for ; Fri, 21 Sep 2018 14:34:33 +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="OKm837L2"; 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 42Ggjj5FdZzF3Pl for ; Fri, 21 Sep 2018 14:34:33 +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="OKm837L2"; 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::543; helo=mail-pg1-x543.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="OKm837L2"; dkim-atps=neutral Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) (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 42Ggjd1YQRzF3PV for ; Fri, 21 Sep 2018 14:34:28 +1000 (AEST) Received: by mail-pg1-x543.google.com with SMTP id y18-v6so2149421pge.0 for ; Thu, 20 Sep 2018 21:34:28 -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=X9evJYALSuc7QuWjJGrYU7g1G683cw3JvuBvJAhMP78=; b=OKm837L2IYwu74KVsk2Pg13Q5myORtTvorY40ugD460lP5AlVEMATTvxyZ5r0orBRz tFlEVJl9Ps8AiYgHawzBMZTooP3xFjXRAPSO+uUgfoAkNqlkQk/UVBjs0ik6w19qq4mA NnLqstrCF3mPVbMiFPLYQ6VnV1DTJ1Gx8ByV055GKvMLj+XAsNj4imqj3fnnMLtw7Zk5 ALMizDyqXpK+n0hukwa/8rilN9biOW/PEchbwWqYONcrW1yV6QDI37cJWIegvA8wqssf 1ukUgboxR7En92AmGn68i6lUgMQAy5J9sR5yPr6xIXUFEOhcEmwkVlX1H4O/Po9s7fIO Yl+Q== 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=X9evJYALSuc7QuWjJGrYU7g1G683cw3JvuBvJAhMP78=; b=n5lWWNVkCM4qRG8zjq9cAjS6cr+vTxuJKD/X/LOqEE9EvoaSVtkEfLWiWiIg0QVisP k8ePLc/n64nXIIcOERPmUiyWf6t0/BimGxs/XRsvCWqBQuSJhX3LgLiwbeKv3TRsCV0g mShDQt1HUV+/uXlk0PdxQzSZywto1XFQuynNsu5X/X55sp5m8KchzfisbhGJ/yPS9bkm jS71lcnEQsxAikAIQrUEj2TL/4iv8HZNjAfwEcXAYFkUaCHYZ8h+cyvUiM8B1IqIRQgz wuXB9sWGt/g/XWml77aEs+JQrKazu0dmr06BemYplVQAvtTZdsR2Bf4hazcpPWOgJ0DY BjJQ== X-Gm-Message-State: APzg51DzpUtXLkgz3slGyOkRcwV00RRK7CG76x2X6BI0/8cXWn8tLoUt f7IlwHUr2RgAcibmQVNEBz/9GzxbBOM= X-Google-Smtp-Source: ANB0VdbZ8ws7MO2RYapdsEYDUSFBBaSeos8dbDDZFTc1rd6WBxHD0NE9u2CBDIQGtAxo19GTvJD38g== X-Received: by 2002:a63:6c89:: with SMTP id h131-v6mr39101298pgc.237.1537504466440; Thu, 20 Sep 2018 21:34:26 -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 o10-v6sm26896509pgp.70.2018.09.20.21.34.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 21:34:25 -0700 (PDT) From: Nicholas Piggin To: pdbg@lists.ozlabs.org Date: Fri, 21 Sep 2018 14:34:17 +1000 Message-Id: <20180921043417.9261-1-npiggin@gmail.com> X-Mailer: git-send-email 2.18.0 Subject: [Pdbg] [PATCH v2] 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 --- v2: Improve ugly code slightly src/thread.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/src/thread.c b/src/thread.c index d282307..b53dccd 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,65 @@ 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; +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + bool be = false; +#else + bool be = true; +#endif + + 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; + be = !be; + } else { + sp = tmp; + } + + 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; }