From patchwork Wed Apr 10 08:01:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amitay Isaacs X-Patchwork-Id: 1083236 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.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44fGrG1ySGz9s8m for ; Wed, 10 Apr 2019 18:03:42 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=ozlabs.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.b="kZuzTkXB"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 44fGrG0ks2zDqNF for ; Wed, 10 Apr 2019 18:03:42 +1000 (AEST) X-Original-To: pdbg@lists.ozlabs.org Delivered-To: pdbg@lists.ozlabs.org Received: from ozlabs.org (bilbo.ozlabs.org [203.11.71.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 44fGpC2NyXzDqNr for ; Wed, 10 Apr 2019 18:01:55 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=pass (p=none dis=none) header.from=ozlabs.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=ozlabs.org header.i=@ozlabs.org header.b="kZuzTkXB"; dkim-atps=neutral Received: from authenticated.ozlabs.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 mail.ozlabs.org (Postfix) with ESMTPSA id 44fGpB61Ssz9s9h; Wed, 10 Apr 2019 18:01:54 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ozlabs.org; s=201707; t=1554883314; bh=pZ2y9RD0LiWBfBHDVIwl7B7VoYvC5dVDMcRjyVoSNl4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kZuzTkXB9jFBoxrKHvKzrT+NtWQErrVf77Pf22i0Kc/6e3WYdcdgd7AFw06V5QEga zm2Wo8sbWKKnmCYfxgJOKZ5KqGCFyZTqjWtnLHMZQCMYH8PN3FEMVX97UwpK8SUVQm MRrKg48Qc54HvHmtEQzpSw+OyWnxhu7Lj1rS/Y4ICeUDW/T5XJljs4kZR/KRqU2er1 alH+3dHgGayXB3lhZQQyOqikouy91yBQk0p82xysKLYJCWCL85MBc474bOYO027fui k205gunYCThJjniAVGUhmK2yQR2wqd/9JpbQKFZiGn9Dn0gHL1j0kNfTVP+LbG0nzB d9MLiRu4emdHQ== From: Amitay Isaacs To: pdbg@lists.ozlabs.org Date: Wed, 10 Apr 2019 18:01:47 +1000 Message-Id: <20190410080151.429876-2-amitay@ozlabs.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190410080151.429876-1-amitay@ozlabs.org> References: <20190410080151.429876-1-amitay@ozlabs.org> MIME-Version: 1.0 Subject: [Pdbg] [PATCH v5 1/5] main: Add hexdump() function to dump bytes 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: Amitay Isaacs Errors-To: pdbg-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Pdbg" Signed-off-by: Amitay Isaacs Reviewed-by: Joel Stanley --- src/util.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 16 ++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/util.c b/src/util.c index 3a4520d..24c3bee 100644 --- a/src/util.c +++ b/src/util.c @@ -19,6 +19,9 @@ #include #include #include +#include + +#include "util.h" /* Parse argument of the form 0-5,7,9-11,15,17 */ bool parse_list(const char *arg, int max, int *list, int *count) @@ -93,3 +96,45 @@ bool parse_list(const char *arg, int max, int *list, int *count) return true; } +void hexdump(uint64_t addr, uint8_t *buf, uint64_t size, uint8_t group_size) +{ + uint64_t start_addr, offset, i; + int j, k; + + start_addr = addr & (~(uint64_t)0xf); + offset = addr - start_addr; + + if (group_size == 0) + group_size = 1; + + assert(group_size == 1 || group_size == 2 || group_size == 4 || group_size == 8); + + for (i = 0; i < size + 15; i += 16) { + bool do_prefix = true; + + if (start_addr + i >= addr + size) + break; + + for (j = 0; j < 16; j += group_size) { + for (k = j; k < j + group_size; k++) { + uint64_t cur_addr = start_addr + i + k; + + if (cur_addr >= addr + size) { + printf("\n"); + return; + } + + if (do_prefix) { + printf("0x%016" PRIx64 ": ", start_addr + i); + do_prefix = false; + } + if (i+k >= offset && i+k <= offset + size) + printf("%02x", buf[i+k - offset]); + else + printf(" "); + } + printf(" "); + } + printf("\n"); + } +} diff --git a/src/util.h b/src/util.h index 131e3f9..f63fc81 100644 --- a/src/util.h +++ b/src/util.h @@ -16,6 +16,9 @@ #ifndef __UTIL_H #define __UTIL_H +#include +#include + /** * @brief Parse a range or a list of numbers from a string into an array * @@ -31,4 +34,17 @@ */ bool parse_list(const char *arg, int max, int *list, int *count); +/** + * @brief Dump bytes in hex similar to hexdump format + * + * Prints 16 bytes per line in the specified groups. The addresses are + * printed aligned to 16 bytes. + * + * @param[in] addr Address + * @param[in] buf Data to print + * @param[in] size Number of bytes to print + * @param[in] group_size How to group bytes (valid values 1, 2, 4, 8) + */ +void hexdump(uint64_t addr, uint8_t *buf, uint64_t size, uint8_t group_size); + #endif