From nobody Thu Dec 18 01:22:58 2025 Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AB9B71CCB4B; Wed, 19 Mar 2025 16:12:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742400725; cv=none; b=J4WHLgkIZuyEuTa2pJMaho1CFcde0e5mlybnToZbEHOz7aA2/fWmDBykduOOum4kHEixyCC5qN//0Eyg52N4jEMcf4n97xT0Gs2q1aWb/ot/YIHepqga+XaLzDIt83FgSjp5u3mzfsoYTV2Tysc6SV6n+HUR3Kp62UizGyndL4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742400725; c=relaxed/simple; bh=Yit60MizxPdRAT0dGt9yEpVcp5G813hgs0g6iVfQ2dQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=t6uMs8MxR2L+F0YHNhmiTS0pSEeSWZFMbA/wVts3ZYKuw9bT3BadTzLXHLFLWEJvIi5UCpAKMDKDkVXroRctcTM3Zdcimdt8T98bmCruhkaDKkoOtvaZu0cxM247DASyiY9FlDgP0Bz8/BZZJd/ls74XucLesrUpdEDtswDn7oY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=XAerFY9T; arc=none smtp.client-ip=217.70.183.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="XAerFY9T" Received: by mail.gandi.net (Postfix) with ESMTPSA id 998F620487; Wed, 19 Mar 2025 16:11:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1742400715; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TgQW7SKf+36bry4gG5ZfyulYo2oF3QSahnUBo0Xtw3I=; b=XAerFY9THmzvZNFx0XpeSs74R2niZnVDVdnTLzDm9rMXentyby3uKDSs1w4pzjWKKvtiAR pv706IKVNRpdTJVfaVsJRNaL3UCi9ACGBGcxUvd3OOt66Ao44PIsM/STyiiQ/2ndamIM5r +5bYoPsjFuwU6AERdphYwryE/Ws5cLRBD9ett6FEjwoD2pY6vIW2Z58FfmApESUtCnhHJf g2TA/heaaalFhrv5u5fSvR7X4b+Fw2vP/Fimalcocw6Z/tjKLs1exERlREmdawFT92T7P3 cJXpCOrtDDXoWh8lzwDsjVopXsG5/6ZpBIbGlhx0TNOqhao76Uzbu15+1QcbcA== From: Miquel Raynal Date: Wed, 19 Mar 2025 17:08:10 +0100 Subject: [PATCH v3 1/3] hexdump: Simplify print_hex_dump() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250319-perso-hexdump-v3-1-a6ba3a9f3742@bootlin.com> References: <20250319-perso-hexdump-v3-0-a6ba3a9f3742@bootlin.com> In-Reply-To: <20250319-perso-hexdump-v3-0-a6ba3a9f3742@bootlin.com> To: Petr Mladek , David Laight , Steven Rostedt , Rasmus Villemoes , Sergey Senozhatsky , Jonathan Corbet , John Ogness , Andrew Morton Cc: Andy Shevchenko , Thomas Petazzoni , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Miquel Raynal X-Mailer: b4 0.15-dev X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddugeehjeejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthejredtredtjeenucfhrhhomhepofhiqhhuvghlucftrgihnhgrlhcuoehmihhquhgvlhdrrhgrhihnrghlsegsohhothhlihhnrdgtohhmqeenucggtffrrghtthgvrhhnpeelgfehvdduieefieeikeffgffggfdttdeugeffieetheeuleelfeehffdtffetveenucfkphepledtrdekledrudeifedruddvjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeeltddrkeelrdduieefrdduvdejpdhhvghloheplgduledvrdduieekrdegvddrgeeingdpmhgrihhlfhhrohhmpehmihhquhgvlhdrrhgrhihnrghlsegsohhothhlihhnrdgtohhmpdhnsggprhgtphhtthhopedufedprhgtphhtthhopegrnhgurhhihidrshhhvghvtghhvghnkhhosehlihhnuhigrdhinhhtvghlrdgtohhmpdhrtghpthhtohepthhhohhmrghsrdhpvghtrgiiiihonhhisegsohhothhlihhnrdgtohhmpdhrtghpthhtoheplhhinhhugidqughotgesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegtohhrsggvtheslhifnhdrnhgvthdprhgtphhtthhopehrohhst hgvughtsehgohhoughmihhsrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepjhhohhhnrdhoghhnvghssheslhhinhhuthhrohhnihigrdguvgdprhgtphhtthhopehmihhquhgvlhdrrhgrhihnrghlsegsohhothhlihhnrdgtohhm X-GND-Sasl: miquel.raynal@bootlin.com print_hex_dump() already has numerous parameters, and could be extended with a new one. Adding new parameters is super painful due to the number of users, and it makes the function calls even longer. Create a print_hex() to replace print_hex_dump(), with 'prefix_type' and 'ascii' being merged into a 'dump_flags' parameter. This way extending the list of dump flags will be much easier. For convenience, a print_hex_dump macro is created to fallback on the print_hex() implementation. A tree-wide change to remove its use could be done in the future. No functional change intended. Suggested-by: Petr Mladek Suggested-by: David Laight Signed-off-by: Miquel Raynal --- Documentation/core-api/printk-formats.rst | 2 +- include/linux/printk.h | 42 +++++++++++++++++++++++----= --- lib/hexdump.c | 43 ++++++++++++++-------------= ---- 3 files changed, 53 insertions(+), 34 deletions(-) diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core= -api/printk-formats.rst index ecccc0473da9c10f45f2464566f690472c61401e..f80b5e262e9933992d1291f1d78= fba97589d5631 100644 --- a/Documentation/core-api/printk-formats.rst +++ b/Documentation/core-api/printk-formats.rst @@ -310,7 +310,7 @@ Raw buffer as a hex string =20 For printing small buffers (up to 64 bytes long) as a hex string with a certain separator. For larger buffers consider using -:c:func:`print_hex_dump`. +:c:func:`print_hex`. =20 MAC/FDDI addresses ------------------ diff --git a/include/linux/printk.h b/include/linux/printk.h index 4217a9f412b265f1dc027be88eff7f5a0e0e4aab..7dca2270c82c0ed788cd706274f= 1c1b14ed9a7fe 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -747,22 +747,31 @@ do { \ =20 extern const struct file_operations kmsg_fops; =20 +/* + * Dump flags for print_hex(). + * DUMP_PREFIX_{NONE,ADDRESS,OFFSET} are mutually exclusive. + */ enum { - DUMP_PREFIX_NONE, - DUMP_PREFIX_ADDRESS, - DUMP_PREFIX_OFFSET + DUMP_HEX_DATA =3D 0, + DUMP_ASCII =3D BIT(0), + DUMP_PREFIX_NONE =3D 0, /* Legacy definition for print_hex_dump() */ + DUMP_PREFIX_ADDRESS =3D BIT(1), + DUMP_PREFIX_OFFSET =3D BIT(2), }; + extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, char *linebuf, size_t linebuflen, bool ascii); #ifdef CONFIG_PRINTK -extern void print_hex_dump(const char *level, const char *prefix_str, - int prefix_type, int rowsize, int groupsize, - const void *buf, size_t len, bool ascii); +extern void print_hex(const char *level, const char *prefix_str, + int rowsize, int groupsize, + const void *buf, size_t len, + unsigned int dump_flags); #else -static inline void print_hex_dump(const char *level, const char *prefix_st= r, - int prefix_type, int rowsize, int groupsize, - const void *buf, size_t len, bool ascii) +static inline void print_hex(const char *level, const char *prefix_str, + int rowsize, int groupsize, + const void *buf, size_t len, + unsigned int dump_flags) { } static inline void print_hex_dump_bytes(const char *prefix_str, int prefix= _type, @@ -791,6 +800,21 @@ static inline void print_hex_dump_debug(const char *pr= efix_str, int prefix_type, } #endif =20 +/* + * print_hex_dump - legacy version of print_hex() with a longer parameter = list + * + * Refer to print_hex() for the parameters definition which are identical = except: + * - prefix_type: controls whether prefix of an offset, address, or none + * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NON= E). + * This parameter has been removed in favor of a common 'flags' parameter. + * - ascii: include ASCII after the hex output. + * This parameter has been removed in favor of a common 'flags' parameter. + */ + +#define print_hex_dump(level, prefix_str, prefix_type, rowsize, groupsize,= buf, len, ascii) \ + print_hex(level, prefix_str, rowsize, groupsize, buf, len, \ + (prefix_type) | ((ascii) ? DUMP_ASCII : DUMP_HEX_DATA)) + /** * print_hex_dump_bytes - shorthand form of print_hex_dump() with default = params * @prefix_str: string to prefix each line with; diff --git a/lib/hexdump.c b/lib/hexdump.c index c3db7c3a764365b01e78f8ed0b7f782f33a5ce34..74fdcb4566d27f257a0e1288c26= 1d81d231b06bf 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -228,39 +228,39 @@ EXPORT_SYMBOL(hex_dump_to_buffer); =20 #ifdef CONFIG_PRINTK /** - * print_hex_dump - print a text hex dump to syslog for a binary blob of d= ata + * print_hex - print a text hex dump to syslog for a binary blob of data * @level: kernel log level (e.g. KERN_DEBUG) * @prefix_str: string to prefix each line with; * caller supplies trailing spaces for alignment if desired - * @prefix_type: controls whether prefix of an offset, address, or none - * is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NO= NE) * @rowsize: number of bytes to print per line; must be 16 or 32 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default =3D= 1) * @buf: data blob to dump * @len: number of bytes in the @buf - * @ascii: include ASCII after the hex output + * @dump_flags: controls the output format, typically: + * - %DUMP_PREFIX_OFFSET shows the offset in front of each line + * - %DUMP_PREFIX_ADDRESS shows the address in front of each line + * - %DUMP_ASCII prints the ascii equivalent after the hex output * - * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump + * Given a buffer of u8 data, print_hex() prints a hex + ASCII dump * to the kernel log at the specified kernel log level, with an optional * leading prefix. * - * print_hex_dump() works on one "line" of output at a time, i.e., + * print_hex() works on one "line" of output at a time, i.e., * 16 or 32 bytes of input data converted to hex + ASCII output. - * print_hex_dump() iterates over the entire input @buf, breaking it into + * print_hex() iterates over the entire input @buf, breaking it into * "line size" chunks to format and print. * * E.g.: - * print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS, - * 16, 1, frame->data, frame->len, true); + * print_hex(KERN_DEBUG, "raw data: ", 16, 1, frame->data, frame->len, + * DUMP_PREFIX_ADDRESS | DUMP_ASCII); * * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode: * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJK= LMNO * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode: * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c pqrstuvwxyz{|}~. */ -void print_hex_dump(const char *level, const char *prefix_str, int prefix_= type, - int rowsize, int groupsize, - const void *buf, size_t len, bool ascii) +void print_hex(const char *level, const char *prefix_str, int rowsize, int= groupsize, + const void *buf, size_t len, unsigned int dump_flags) { const u8 *ptr =3D buf; int i, linelen, remaining =3D len; @@ -274,22 +274,17 @@ void print_hex_dump(const char *level, const char *pr= efix_str, int prefix_type, remaining -=3D rowsize; =20 hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, - linebuf, sizeof(linebuf), ascii); + linebuf, sizeof(linebuf), + dump_flags & DUMP_ASCII); =20 - switch (prefix_type) { - case DUMP_PREFIX_ADDRESS: - printk("%s%s%p: %s\n", - level, prefix_str, ptr + i, linebuf); - break; - case DUMP_PREFIX_OFFSET: + if (dump_flags & DUMP_PREFIX_ADDRESS) + printk("%s%s%p: %s\n", level, prefix_str, ptr + i, linebuf); + else if (dump_flags & DUMP_PREFIX_OFFSET) printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf); - break; - default: + else printk("%s%s%s\n", level, prefix_str, linebuf); - break; - } } } -EXPORT_SYMBOL(print_hex_dump); +EXPORT_SYMBOL(print_hex); =20 #endif /* defined(CONFIG_PRINTK) */ --=20 2.48.1 From nobody Thu Dec 18 01:22:58 2025 Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 992281DE3AE; Wed, 19 Mar 2025 16:12:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742400727; cv=none; b=ljempT8UpGhhtHhKefw5BX+GXEviUlnKUmKyiEon18NA2QsofU7i6hDcpddu7dGaAxsTtHUjzUctocyolpF6BhKbUrZAut36LZOyrz06lEqcQbdXEyHZIeUbLpPUeIyolEmdl505LVChkuV+5j9KbTXCadzkFrfIIQ4UAf7U0Y4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742400727; c=relaxed/simple; bh=ZTORiOP8lCWlxqPdsA9WOVthA28claYMiiXwkgF4t0E=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dx8hYCpbv5Misoqe6piPkz2er05okY8iE0wiB/5AhYS9GslPpq0E/ajb0BtRCOdGb1AI4u7SqdU81PDZ8DlJbSNAaAxVmJAJkMM0gvMqb8Nk2/JNNT/L72MUlVng6ebDpsZ0qppI7ptj7oyNm50wIPtwfxdY9bNMkDyEzQCZR8M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=FtX/YH15; arc=none smtp.client-ip=217.70.183.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="FtX/YH15" Received: by mail.gandi.net (Postfix) with ESMTPSA id 4BE5620488; Wed, 19 Mar 2025 16:11:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1742400715; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b1yAFy1jrUWs7EUz51HeUQ/XlIqYqsEuyGm9trXWWY0=; b=FtX/YH15cz5MBTfAEo1M12IL65wfN42SFhpKoASraBoMJDBTvS3Tu5HafwMPgDsXEAUhE2 jqlUcNzzEv+QOQk8avYXqEulDkmqtiZX2ZAozeZK1eoEtBokU5NiW3bzVRKUW2G1S+WG+k TCgoMuK9FCmz6Z7C/HiZqONxNfBRThlDx2vPmMh/ZGmARtVUNSa3FdXIH2GiqfczN0JDs5 YvNTwAUftXNmZ0GB9ptPOd0a40rvE+cTsb5D72bbw72q4+qIhG7ofrgmFu9at7cWS4zG/H xe56a+se3incp1g8dbYNyMQMATYV3XhmCwD7loYS7yt0YOMD89ZsxW+NYZ837w== From: Miquel Raynal Date: Wed, 19 Mar 2025 17:08:11 +0100 Subject: [PATCH v3 2/3] hexdump: Allow skipping identical lines Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250319-perso-hexdump-v3-2-a6ba3a9f3742@bootlin.com> References: <20250319-perso-hexdump-v3-0-a6ba3a9f3742@bootlin.com> In-Reply-To: <20250319-perso-hexdump-v3-0-a6ba3a9f3742@bootlin.com> To: Petr Mladek , David Laight , Steven Rostedt , Rasmus Villemoes , Sergey Senozhatsky , Jonathan Corbet , John Ogness , Andrew Morton Cc: Andy Shevchenko , Thomas Petazzoni , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Miquel Raynal X-Mailer: b4 0.15-dev X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddugeehjeejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthejredtredtjeenucfhrhhomhepofhiqhhuvghlucftrgihnhgrlhcuoehmihhquhgvlhdrrhgrhihnrghlsegsohhothhlihhnrdgtohhmqeenucggtffrrghtthgvrhhnpeelgfehvdduieefieeikeffgffggfdttdeugeffieetheeuleelfeehffdtffetveenucfkphepledtrdekledrudeifedruddvjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeeltddrkeelrdduieefrdduvdejpdhhvghloheplgduledvrdduieekrdegvddrgeeingdpmhgrihhlfhhrohhmpehmihhquhgvlhdrrhgrhihnrghlsegsohhothhlihhnrdgtohhmpdhnsggprhgtphhtthhopedufedprhgtphhtthhopegrnhgurhhihidrshhhvghvtghhvghnkhhosehlihhnuhigrdhinhhtvghlrdgtohhmpdhrtghpthhtohepthhhohhmrghsrdhpvghtrgiiiihonhhisegsohhothhlihhnrdgtohhmpdhrtghpthhtoheplhhinhhugidqughotgesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegtohhrsggvtheslhifnhdrnhgvthdprhgtphhtthhopehrohhst hgvughtsehgohhoughmihhsrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepjhhohhhnrdhoghhnvghssheslhhinhhuthhrohhnihigrdguvgdprhgtphhtthhopehmihhquhgvlhdrrhgrhihnrghlsegsohhothhlihhnrdgtohhm X-GND-Sasl: miquel.raynal@bootlin.com When dumping long buffers (especially for debug purposes) it may be very convenient to sometimes avoid spitting all the lines of the buffer if the lines are identical. Typically on embedded devices, the console would be wired to a UART running at 115200 bauds, which makes the dumps very (very) slow. In this case, having a flag to avoid printing duplicated lines is handy. Example of a made up repetitive output: 0f 53 63 47 56 55 78 7a aa b7 8c ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 01 2a 39 eb Same but with the flag enabled: 0f 53 63 47 56 55 78 7a aa b7 8c ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff * ff ff ff ff ff ff ff ff ff ff ff ff 01 2a 39 eb Signed-off-by: Miquel Raynal --- Documentation/core-api/printk-formats.rst | 4 +++- include/linux/printk.h | 1 + lib/hexdump.c | 20 +++++++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core= -api/printk-formats.rst index f80b5e262e9933992d1291f1d78fba97589d5631..820f92c65dc64e7d24af5c0031e= e8c8d6bb0f931 100644 --- a/Documentation/core-api/printk-formats.rst +++ b/Documentation/core-api/printk-formats.rst @@ -310,7 +310,9 @@ Raw buffer as a hex string =20 For printing small buffers (up to 64 bytes long) as a hex string with a certain separator. For larger buffers consider using -:c:func:`print_hex`. +:c:func:`print_hex`, especially since duplicated lines can be +skipped automatically to reduce the overhead with the +``DUMP_SKIP_IDENTICAL_LINES`` flag. =20 MAC/FDDI addresses ------------------ diff --git a/include/linux/printk.h b/include/linux/printk.h index 7dca2270c82c0ed788cd706274f1c1b14ed9a7fe..d9e3e4b0bab8d3ff6a49600abbd= bc9b1e6320a60 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -757,6 +757,7 @@ enum { DUMP_PREFIX_NONE =3D 0, /* Legacy definition for print_hex_dump() */ DUMP_PREFIX_ADDRESS =3D BIT(1), DUMP_PREFIX_OFFSET =3D BIT(2), + DUMP_SKIP_IDENTICAL_LINES =3D BIT(3), }; =20 extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, diff --git a/lib/hexdump.c b/lib/hexdump.c index 74fdcb4566d27f257a0e1288c261d81d231b06bf..f0d1a7f1ce817fd53a7ffd259fb= e9b9c8348db16 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include =20 @@ -240,6 +241,8 @@ EXPORT_SYMBOL(hex_dump_to_buffer); * - %DUMP_PREFIX_OFFSET shows the offset in front of each line * - %DUMP_PREFIX_ADDRESS shows the address in front of each line * - %DUMP_ASCII prints the ascii equivalent after the hex output + * - %DUMP_SKIP_IDENTICAL_LINES will display a single '*' instead of + * duplicated lines. * * Given a buffer of u8 data, print_hex() prints a hex + ASCII dump * to the kernel log at the specified kernel log level, with an optional @@ -263,8 +266,9 @@ void print_hex(const char *level, const char *prefix_st= r, int rowsize, int group const void *buf, size_t len, unsigned int dump_flags) { const u8 *ptr =3D buf; - int i, linelen, remaining =3D len; + int i, prev_i, linelen, remaining =3D len; unsigned char linebuf[32 * 3 + 2 + 32 + 1]; + bool same_line =3D false; =20 if (rowsize !=3D 16 && rowsize !=3D 32) rowsize =3D 16; @@ -273,6 +277,20 @@ void print_hex(const char *level, const char *prefix_s= tr, int rowsize, int group linelen =3D min(remaining, rowsize); remaining -=3D rowsize; =20 + if (dump_flags & DUMP_SKIP_IDENTICAL_LINES) { + if (i && !memcmp(ptr + i, ptr + prev_i, linelen)) { + prev_i =3D i; + if (same_line) + continue; + same_line =3D true; + printk("%s*\n", level); + continue; + } else { + prev_i =3D i; + same_line =3D false; + } + } + hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize, linebuf, sizeof(linebuf), dump_flags & DUMP_ASCII); --=20 2.48.1 From nobody Thu Dec 18 01:22:58 2025 Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 991A41DE2B9; Wed, 19 Mar 2025 16:12:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.195 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742400726; cv=none; b=UZ7eJsmb/gnB3nsEng2tLn/ofTjCCAZKu5oR5K9PGK4P/KqxSMl2F0ERraPM64+j1UHLqvxmWtLoJZXj+uF9DxdArMR9Lt83B2D+J4wArtNeKAYv6KorwnyTzHh0fNEArRkQGO/jascFRc6ZZFxanmFFX+UPPOpJDx54bUSyeUE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742400726; c=relaxed/simple; bh=gvzv+JoKyTfzZBnuQVo92/nHh4afUX9iQfLLKtCWUSE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oanaYA76TAkB2kM7rWzRzntWfrm+TjwyDzJGCpFpuOVyAbw7dENJ0QIKX39MYNemPN4bJx4r9X6Kxe1i7IvdqpQftB90qIMUOd22PeeWWC3DBrBzRO/tkgCfZPFaI6nn5ndVPAZ9Cp9Q7WlcXwj8rHaTvS9EMEjCDWafxRpxpC4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=YXaUQrDf; arc=none smtp.client-ip=217.70.183.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="YXaUQrDf" Received: by mail.gandi.net (Postfix) with ESMTPSA id E07762058B; Wed, 19 Mar 2025 16:11:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1742400716; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m555aCLnWKiYrn41gUCNlBZiTLZxQmM5uW++76DvNe4=; b=YXaUQrDfATdDyNAa78V1Tc/SezMyPuoLoOnGdpxgbUfrtestNRz65wZuuOBMLQK1dZxecz G8uG+1Ps8S8WFCIGZ+7c5clWJ7yql0jiWWBeIgNPEQHgVrPyvFM6YtPI5fT4PfJG2r8Tqe RKj/7st1cFDNB4521RSxRI3MsAKVGbqUSbHQs45PRVGGOeER/LP8wzj9KB76IsMox49JLE sY/jJWbKlcUnMr2XEUgbAmN9b715OskiyOwFjmI7K7mIQdf7To1NLqpUi0xc9AE9EzLEaZ kKPqeZw1ZLkMY5NrRxGJcJ9vEbwnx5AVyXNChdFEEvqrLHlFIwSIid+kXzCtpg== From: Miquel Raynal Date: Wed, 19 Mar 2025 17:08:12 +0100 Subject: [PATCH v3 3/3] hexdump: Print the prefix after the last line to show the dump is over Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250319-perso-hexdump-v3-3-a6ba3a9f3742@bootlin.com> References: <20250319-perso-hexdump-v3-0-a6ba3a9f3742@bootlin.com> In-Reply-To: <20250319-perso-hexdump-v3-0-a6ba3a9f3742@bootlin.com> To: Petr Mladek , David Laight , Steven Rostedt , Rasmus Villemoes , Sergey Senozhatsky , Jonathan Corbet , John Ogness , Andrew Morton Cc: Andy Shevchenko , Thomas Petazzoni , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Miquel Raynal X-Mailer: b4 0.15-dev X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgddugeehjeejucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuifetpfffkfdpucggtfgfnhhsuhgsshgtrhhisggvnecuuegrihhlohhuthemuceftddunecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthejredtredtjeenucfhrhhomhepofhiqhhuvghlucftrgihnhgrlhcuoehmihhquhgvlhdrrhgrhihnrghlsegsohhothhlihhnrdgtohhmqeenucggtffrrghtthgvrhhnpeelgfehvdduieefieeikeffgffggfdttdeugeffieetheeuleelfeehffdtffetveenucfkphepledtrdekledrudeifedruddvjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepihhnvghtpeeltddrkeelrdduieefrdduvdejpdhhvghloheplgduledvrdduieekrdegvddrgeeingdpmhgrihhlfhhrohhmpehmihhquhgvlhdrrhgrhihnrghlsegsohhothhlihhnrdgtohhmpdhnsggprhgtphhtthhopedufedprhgtphhtthhopegrnhgurhhihidrshhhvghvtghhvghnkhhosehlihhnuhigrdhinhhtvghlrdgtohhmpdhrtghpthhtohepthhhohhmrghsrdhpvghtrgiiiihonhhisegsohhothhlihhnrdgtohhmpdhrtghpthhtoheplhhinhhugidqughotgesvhhgvghrrdhkvghrnhgvlhdrohhrghdprhgtphhtthhopegtohhrsggvtheslhifnhdrnhgvthdprhgtphhtthhopehrohhst hgvughtsehgohhoughmihhsrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepjhhohhhnrdhoghhnvghssheslhhinhhuthhrohhnihigrdguvgdprhgtphhtthhopehmihhquhgvlhdrrhgrhihnrghlsegsohhothhlihhnrdgtohhm X-GND-Sasl: miquel.raynal@bootlin.com When skipping duplicated lines, the end of the log can just be a star ("*") which may be confusing (?), so in order to clarify the end of the log, tell the user how many lines where skipped and mimic the userspace hexdump output, let's add in this case a new line with the next offset. With the following data: 00000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000010: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000020: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000030: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000040: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000050: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000060: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000070: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ When skipping identical lines we were seeing: 00000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ * And now we will have: 00000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ * 00000080 However if the output was (first bit of first byte on last line changed): 00000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000010: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000020: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000030: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000040: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000050: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000060: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ 00000070: fe ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ ^ When skipping identical lines we would see: 00000000: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ * 00000070: fe ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ................ And in this case the output would not change with this patch. Suggested-by: Andy Shevchenko Signed-off-by: Miquel Raynal Reviewed-by: Andy Shevchenko --- This change is not related to the two first patches and is just an addition that was requested by Andy who felt like it was not clear where the dump was ending with the 'SKIP_DUPLICATE' flag when the last line was skipped. Honestly this never bothered me, so depending on the maintainers wishes, this can either be applied or skipped. --- lib/hexdump.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/hexdump.c b/lib/hexdump.c index f0d1a7f1ce817fd53a7ffd259fbe9b9c8348db16..c0c9a13838513bc7dec2ebdeadf= 622cd319ea4f8 100644 --- a/lib/hexdump.c +++ b/lib/hexdump.c @@ -302,6 +302,13 @@ void print_hex(const char *level, const char *prefix_s= tr, int rowsize, int group else printk("%s%s%s\n", level, prefix_str, linebuf); } + + if (same_line) { + if (dump_flags & DUMP_PREFIX_ADDRESS) + printk("%s%s%p\n", level, prefix_str, ptr + i); + else if (dump_flags & DUMP_PREFIX_OFFSET) + printk("%s%s%.8x\n", level, prefix_str, i); + } } EXPORT_SYMBOL(print_hex); =20 --=20 2.48.1