Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp205853ybx; Tue, 5 Nov 2019 22:31:27 -0800 (PST) X-Google-Smtp-Source: APXvYqwHVCRlmvdIu/WX/RCtWsubNeDt+qDFJQHA3PLeyfxMP2czCwf1fgeHgZJD9vhLIzySh4PZ X-Received: by 2002:a17:906:6813:: with SMTP id k19mr6709209ejr.34.1573021887338; Tue, 05 Nov 2019 22:31:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1573021887; cv=none; d=google.com; s=arc-20160816; b=mzRIAQgJK+2Zd59k/gzvJi5LUDRQszY57Zl41l4qXksmz2P+aNbMfVhcXLAfsVsoaH a0jyjYcVVyAd6a/vygtRICKn4TP9s+IN42UR6Rtwyz4spHoDcWlStYcdBHRsMNLAVShb c9Dt9Z6eb1Pp7wcv8XDZeMoWtUTiStndLnzOxOEKREj1qE+HlinqkXdwDWfvIu4SZ8AL HMM96AdnKDQhKSRGy7Kcvlo7TvbFXmKhAZFtapWO+FdVowLxAhsL+uVciQnlxerI3IF2 qm/g4FrG4E4mgBnicLWZRaKmV9AzXWfdaPNAF+rAELokRI8DrJjQoBn/2gkG3SsxyNM9 Lm0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=uL8ZKdkGXJka3wgtMJ0J+KuTUZ10A+Ffs2luQS0i7gM=; b=OsO5XZgAheogQPkqTRnlToL53/Q43jLRz/IVqCZYWcqlbcsPN8t8CRH34f3mPZ/iFC Jzsb+oP/6/GdS63WQhzArJG1VHwcFTOY9A9n9nmGMps3j24TaBeNjOKHT10PoiIFgZcU JrQqT7meCr+fWc8+qBgI0XPKzXis28jCxk+0mW32Kk6zvegAhs3vlMHyGreNXF52hyBP rAupWrOplgIbhgIhaV7l5W74LBW5NPxayCtxsXwneSd1wZ7xHAgjcomznfTWCmF/qr3X EWyA1J9GadNejTXspMzHx0J1WTozOD+bn5/ZUlkMjnGVg64ysHAmhxGYp4XwZ9+Oi9NA iI8Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f6si4197058edq.325.2019.11.05.22.31.03; Tue, 05 Nov 2019 22:31:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729549AbfKFG3o (ORCPT + 99 others); Wed, 6 Nov 2019 01:29:44 -0500 Received: from mga04.intel.com ([192.55.52.120]:24752 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725806AbfKFG3o (ORCPT ); Wed, 6 Nov 2019 01:29:44 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Nov 2019 22:29:43 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.68,272,1569308400"; d="scan'208";a="376952022" Received: from test-hp-compaq-8100-elite-cmt-pc.igk.intel.com ([10.237.149.93]) by orsmga005.jf.intel.com with ESMTP; 05 Nov 2019 22:29:41 -0800 From: Piotr Maziarz To: linux-kernel@vger.kernel.org Cc: rostedt@goodmis.org, mingo@redhat.com, andriy.shevchenko@intel.com, cezary.rojewski@intel.com, gustaw.lewandowski@intel.com, Piotr Maziarz Subject: [PATCH 1/2] seq_buf: Add printing formatted hex dumps Date: Wed, 6 Nov 2019 07:27:39 +0100 Message-Id: <1573021660-30540-1-git-send-email-piotrx.maziarz@linux.intel.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Provided function is an analogue of print_hex_dump(). Implementing this function in seq_buf allows using for multiple purposes (e.g. for tracing) and therefore prevents from code duplication in every layer that uses seq_buf. print_hex_dump() is an essential part of logging data to dmesg. Adding similar capability for other purposes is beneficial to all users. Signed-off-by: Piotr Maziarz Signed-off-by: Cezary Rojewski --- include/linux/seq_buf.h | 3 +++ lib/seq_buf.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h index aa5deb0..fb0205d 100644 --- a/include/linux/seq_buf.h +++ b/include/linux/seq_buf.h @@ -125,6 +125,9 @@ extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len); extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem, unsigned int len); extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc); +extern int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str, + int prefix_type, int rowsize, int groupsize, + const void *buf, size_t len, bool ascii); #ifdef CONFIG_BINARY_PRINTF extern int diff --git a/lib/seq_buf.c b/lib/seq_buf.c index bd807f5..0509706 100644 --- a/lib/seq_buf.c +++ b/lib/seq_buf.c @@ -328,3 +328,41 @@ int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt) s->readpos += cnt; return cnt; } + +int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str, int prefix_type, + int rowsize, int groupsize, + const void *buf, size_t len, bool ascii) +{ + const u8 *ptr = buf; + int i, linelen, remaining = len; + unsigned char linebuf[32 * 3 + 2 + 32 + 1]; + int ret; + + if (rowsize != 16 && rowsize != 32) + rowsize = 16; + + for (i = 0; i < len; i += rowsize) { + linelen = min(remaining, rowsize); + remaining -= rowsize; + + hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, + linebuf, sizeof(linebuf), ascii); + + switch (prefix_type) { + case DUMP_PREFIX_ADDRESS: + ret = seq_buf_printf(s, "%s%p: %s\n", + prefix_str, ptr + i, linebuf); + break; + case DUMP_PREFIX_OFFSET: + ret = seq_buf_printf(s, "%s%.8x: %s\n", + prefix_str, i, linebuf); + break; + default: + ret = seq_buf_printf(s, "%s%s\n", prefix_str, linebuf); + break; + } + if (ret) + return ret; + } + return 0; +} -- 2.7.4