* [RFC] vsprintf: Add %pb[1-64] specifier to print hex memory dump
@ 2012-06-29 14:11 Andrei Emeltchenko
2012-06-29 14:54 ` Joe Perches
0 siblings, 1 reply; 4+ messages in thread
From: Andrei Emeltchenko @ 2012-06-29 14:11 UTC (permalink / raw)
To: akpm, linux-kernel
From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
Add new specifier which may be used to print 1-64 bytes of memory.
There is often a need to print small (up to 64 bytes) objects like
bluetooth keys in debug purposes. Currently we have to create special
function for that.
Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
---
lib/vsprintf.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 7369745..59835a7 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -655,6 +655,51 @@ char *resource_string(char *buf, char *end, struct resource *res,
}
static noinline_for_stack
+char *hex_memory_string(char *buf, char *end, const u8 *addr,
+ struct printf_spec spec, const char *fmt)
+{
+ const char *p_fmt = fmt + 1;
+ size_t len, bytes = 0;
+
+ p_fmt = &fmt[1];
+ if (isdigit(*p_fmt))
+ bytes = skip_atoi(&p_fmt);
+
+ /* Print up to 64 bytes */
+ if (!bytes || bytes > 64)
+ return string(buf, end, "(%pb[1-64])", spec);
+
+ /* for each byte 2 chars + separator; + terminator; + 1st newline */
+ len = bytes * (sizeof(char) * 3) + 2 * sizeof(char);
+ if (end - buf > len) {
+ char mem_str[len];
+ char *p = mem_str;
+ const char separator = ' ';
+ u16 columns = 15;
+ int i;
+
+ for (i = 0; i < bytes; i++, columns--) {
+ p = hex_byte_pack(p, addr[i]);
+
+ if (columns) {
+ *p++ = separator;
+ } else {
+ if (i < bytes - 1)
+ *p++ = '\n';
+
+ columns = 16;
+ }
+ }
+
+ *p = '\0';
+
+ return string(buf, end, mem_str, spec);
+ }
+
+ return string(buf, end, "(too big)", spec);
+}
+
+static noinline_for_stack
char *mac_address_string(char *buf, char *end, u8 *addr,
struct printf_spec spec, const char *fmt)
{
@@ -934,6 +979,7 @@ int kptr_restrict __read_mostly;
*
* Right now we handle:
*
+ * - 'b[1-64]' For printing memory dump 1-64 bytes long
* - 'F' For symbolic function descriptor pointers with offset
* - 'f' For simple symbolic function names without offset
* - 'S' For symbolic direct pointers with offset
@@ -996,6 +1042,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
}
switch (*fmt) {
+ case 'b':
+ return hex_memory_string(buf, end, ptr, spec, fmt);
case 'F':
case 'f':
ptr = dereference_function_descriptor(ptr);
--
1.7.9.5
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [RFC] vsprintf: Add %pb[1-64] specifier to print hex memory dump
2012-06-29 14:11 [RFC] vsprintf: Add %pb[1-64] specifier to print hex memory dump Andrei Emeltchenko
@ 2012-06-29 14:54 ` Joe Perches
2012-06-29 14:59 ` Andrei Emeltchenko
0 siblings, 1 reply; 4+ messages in thread
From: Joe Perches @ 2012-06-29 14:54 UTC (permalink / raw)
To: Andrei Emeltchenko; +Cc: akpm, linux-kernel
On Fri, 2012-06-29 at 17:11 +0300, Andrei Emeltchenko wrote:
> From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
>
> Add new specifier which may be used to print 1-64 bytes of memory.
> There is often a need to print small (up to 64 bytes) objects like
> bluetooth keys in debug purposes. Currently we have to create special
> function for that.
print_hex_dump() ?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [RFC] vsprintf: Add %pb[1-64] specifier to print hex memory dump
2012-06-29 14:54 ` Joe Perches
@ 2012-06-29 14:59 ` Andrei Emeltchenko
2012-06-29 15:12 ` Joe Perches
0 siblings, 1 reply; 4+ messages in thread
From: Andrei Emeltchenko @ 2012-06-29 14:59 UTC (permalink / raw)
To: Joe Perches; +Cc: akpm, linux-kernel
Hi,
On Fri, Jun 29, 2012 at 5:54 PM, Joe Perches <joe@perches.com> wrote:
> On Fri, 2012-06-29 at 17:11 +0300, Andrei Emeltchenko wrote:
>> From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
>>
>> Add new specifier which may be used to print 1-64 bytes of memory.
>> There is often a need to print small (up to 64 bytes) objects like
>> bluetooth keys in debug purposes. Currently we have to create special
>> function for that.
>
> print_hex_dump() ?
If this only needed for debug we would need to undef this?
Regards,
Andrei
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [RFC] vsprintf: Add %pb[1-64] specifier to print hex memory dump
2012-06-29 14:59 ` Andrei Emeltchenko
@ 2012-06-29 15:12 ` Joe Perches
0 siblings, 0 replies; 4+ messages in thread
From: Joe Perches @ 2012-06-29 15:12 UTC (permalink / raw)
To: Andrei Emeltchenko; +Cc: akpm, linux-kernel
On Fri, 2012-06-29 at 17:59 +0300, Andrei Emeltchenko wrote:
> Hi,
>
> On Fri, Jun 29, 2012 at 5:54 PM, Joe Perches <joe@perches.com> wrote:
> > On Fri, 2012-06-29 at 17:11 +0300, Andrei Emeltchenko wrote:
> >> From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
> >>
> >> Add new specifier which may be used to print 1-64 bytes of memory.
> >> There is often a need to print small (up to 64 bytes) objects like
> >> bluetooth keys in debug purposes. Currently we have to create special
> >> function for that.
> >
> > print_hex_dump() ?
>
> If this only needed for debug we would need to undef this?
If it's only needed for your debugging it should not go
into vsprintf at all.
Why not something like:
#ifdef SOME_BLUETOOTH_DEBUG_FLAG
#define bt_hex_dump_dbg(...) \
print_hex_dump(...)
#else
#define bt_hex_dump_dbg(...) \
do { } while (0)
#endif
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2012-06-29 15:12 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-29 14:11 [RFC] vsprintf: Add %pb[1-64] specifier to print hex memory dump Andrei Emeltchenko
2012-06-29 14:54 ` Joe Perches
2012-06-29 14:59 ` Andrei Emeltchenko
2012-06-29 15:12 ` Joe Perches
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®