* [PATCH 5/9] frv: gdb-stub.c use the common ascii hex helpers
@ 2008-05-01 23:05 Harvey Harrison
2008-05-02 2:09 ` David Howells
0 siblings, 1 reply; 5+ messages in thread
From: Harvey Harrison @ 2008-05-01 23:05 UTC (permalink / raw)
To: Andrew Morton; +Cc: David Howells, LKML
Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
---
arch/frv/kernel/gdb-stub.c | 163 ++++++++++++++++++++------------------------
1 files changed, 73 insertions(+), 90 deletions(-)
diff --git a/arch/frv/kernel/gdb-stub.c b/arch/frv/kernel/gdb-stub.c
index 48a0393..5a369d7 100644
--- a/arch/frv/kernel/gdb-stub.c
+++ b/arch/frv/kernel/gdb-stub.c
@@ -182,8 +182,6 @@ extern volatile u32 __attribute__((section(".bss"))) gdbstub_trace_through_excep
static char input_buffer[BUFMAX];
static char output_buffer[BUFMAX];
-static const char hexchars[] = "0123456789abcdef";
-
static const char *regnames[] = {
"PSR ", "ISR ", "CCR ", "CCCR",
"LR ", "LCR ", "PC ", "_stt",
@@ -214,25 +212,10 @@ static struct gdbstub_bkpt gdbstub_bkpts[256];
static void gdbstub_recv_packet(char *buffer);
static int gdbstub_send_packet(char *buffer);
static int gdbstub_compute_signal(unsigned long tbr);
-static int hex(unsigned char ch);
static int hexToInt(char **ptr, unsigned long *intValue);
static unsigned char *mem2hex(const void *mem, char *buf, int count, int may_fault);
static char *hex2mem(const char *buf, void *_mem, int count);
-/*
- * Convert ch from a hex digit to an int
- */
-static int hex(unsigned char ch)
-{
- if (ch >= 'a' && ch <= 'f')
- return ch-'a'+10;
- if (ch >= '0' && ch <= '9')
- return ch-'0';
- if (ch >= 'A' && ch <= 'F')
- return ch-'A'+10;
- return -1;
-}
-
void gdbstub_printk(const char *fmt, ...)
{
static char buf[1024];
@@ -314,12 +297,12 @@ static void gdbstub_recv_packet(char *buffer)
ret = gdbstub_rx_char(&ch, 0);
if (ret < 0)
error = ret;
- xmitcsum = hex(ch) << 4;
+ xmitcsum = hex_to_int(ch) << 4;
ret = gdbstub_rx_char(&ch, 0);
if (ret < 0)
error = ret;
- xmitcsum |= hex(ch);
+ xmitcsum |= hex_to_int(ch);
if (error) {
if (error == -EIO)
@@ -383,8 +366,8 @@ static int gdbstub_send_packet(char *buffer)
}
gdbstub_tx_char('#');
- gdbstub_tx_char(hexchars[checksum >> 4]);
- gdbstub_tx_char(hexchars[checksum & 0xf]);
+ gdbstub_tx_char(hex_asc_hi(checksum));
+ gdbstub_tx_char(hex_asc_lo(checksum));
} while (gdbstub_rx_char(&ch,0),
#ifdef GDBSTUB_DEBUG_PROTOCOL
@@ -413,7 +396,7 @@ static int hexToInt(char **ptr, unsigned long *_value)
*_value = 0;
while (**ptr) {
- ch = hex(**ptr);
+ ch = hex_to_int(**ptr);
if (ch < 0)
break;
@@ -674,8 +657,8 @@ static unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fa
if ((uint32_t)mem&1 && count>=1) {
if (!gdbstub_read_byte(mem,ch))
return NULL;
- *buf++ = hexchars[ch[0] >> 4];
- *buf++ = hexchars[ch[0] & 0xf];
+ *buf++ = hex_asc_hi(ch[0]);
+ *buf++ = hex_asc_lo(ch[0]);
mem++;
count--;
}
@@ -683,10 +666,10 @@ static unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fa
if ((uint32_t)mem&3 && count>=2) {
if (!gdbstub_read_word(mem,(uint16_t *)ch))
return NULL;
- *buf++ = hexchars[ch[0] >> 4];
- *buf++ = hexchars[ch[0] & 0xf];
- *buf++ = hexchars[ch[1] >> 4];
- *buf++ = hexchars[ch[1] & 0xf];
+ *buf++ = hex_asc_hi(ch[0]);
+ *buf++ = hex_asc_lo(ch[0]);
+ *buf++ = hex_asc_hi(ch[1]);
+ *buf++ = hex_asc_lo(ch[1]);
mem += 2;
count -= 2;
}
@@ -694,14 +677,14 @@ static unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fa
while (count>=4) {
if (!gdbstub_read_dword(mem,(uint32_t *)ch))
return NULL;
- *buf++ = hexchars[ch[0] >> 4];
- *buf++ = hexchars[ch[0] & 0xf];
- *buf++ = hexchars[ch[1] >> 4];
- *buf++ = hexchars[ch[1] & 0xf];
- *buf++ = hexchars[ch[2] >> 4];
- *buf++ = hexchars[ch[2] & 0xf];
- *buf++ = hexchars[ch[3] >> 4];
- *buf++ = hexchars[ch[3] & 0xf];
+ *buf++ = hex_asc_hi(ch[0]);
+ *buf++ = hex_asc_lo(ch[0]);
+ *buf++ = hex_asc_hi(ch[1]);
+ *buf++ = hex_asc_lo(ch[1]);
+ *buf++ = hex_asc_hi(ch[2]);
+ *buf++ = hex_asc_lo(ch[2]);
+ *buf++ = hex_asc_hi(ch[3]);
+ *buf++ = hex_asc_lo(ch[3]);
mem += 4;
count -= 4;
}
@@ -709,10 +692,10 @@ static unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fa
if (count>=2) {
if (!gdbstub_read_word(mem,(uint16_t *)ch))
return NULL;
- *buf++ = hexchars[ch[0] >> 4];
- *buf++ = hexchars[ch[0] & 0xf];
- *buf++ = hexchars[ch[1] >> 4];
- *buf++ = hexchars[ch[1] & 0xf];
+ *buf++ = hex_asc_hi(ch[0]);
+ *buf++ = hex_asc_lo(ch[0]);
+ *buf++ = hex_asc_hi(ch[1]);
+ *buf++ = hex_asc_lo(ch[1]);
mem += 2;
count -= 2;
}
@@ -720,8 +703,8 @@ static unsigned char *mem2hex(const void *_mem, char *buf, int count, int may_fa
if (count>=1) {
if (!gdbstub_read_byte(mem,ch))
return NULL;
- *buf++ = hexchars[ch[0] >> 4];
- *buf++ = hexchars[ch[0] & 0xf];
+ *buf++ = hex_asc_hi(ch[0]);
+ *buf++ = hex_asc_lo(ch[0]);
}
*buf = 0;
@@ -744,8 +727,8 @@ static char *hex2mem(const char *buf, void *_mem, int count)
} ch;
if ((u32)mem&1 && count>=1) {
- ch.b[0] = hex(*buf++) << 4;
- ch.b[0] |= hex(*buf++);
+ ch.b[0] = hex_to_int(*buf++) << 4;
+ ch.b[0] |= hex_to_int(*buf++);
if (!gdbstub_write_byte(mem,ch.b[0]))
return NULL;
mem++;
@@ -753,10 +736,10 @@ static char *hex2mem(const char *buf, void *_mem, int count)
}
if ((u32)mem&3 && count>=2) {
- ch.b[0] = hex(*buf++) << 4;
- ch.b[0] |= hex(*buf++);
- ch.b[1] = hex(*buf++) << 4;
- ch.b[1] |= hex(*buf++);
+ ch.b[0] = hex_to_int(*buf++) << 4;
+ ch.b[0] |= hex_to_int(*buf++);
+ ch.b[1] = hex_to_int(*buf++) << 4;
+ ch.b[1] |= hex_to_int(*buf++);
if (!gdbstub_write_word(mem,ch.w))
return NULL;
mem += 2;
@@ -764,14 +747,14 @@ static char *hex2mem(const char *buf, void *_mem, int count)
}
while (count>=4) {
- ch.b[0] = hex(*buf++) << 4;
- ch.b[0] |= hex(*buf++);
- ch.b[1] = hex(*buf++) << 4;
- ch.b[1] |= hex(*buf++);
- ch.b[2] = hex(*buf++) << 4;
- ch.b[2] |= hex(*buf++);
- ch.b[3] = hex(*buf++) << 4;
- ch.b[3] |= hex(*buf++);
+ ch.b[0] = hex_to_int(*buf++) << 4;
+ ch.b[0] |= hex_to_int(*buf++);
+ ch.b[1] = hex_to_int(*buf++) << 4;
+ ch.b[1] |= hex_to_int(*buf++);
+ ch.b[2] = hex_to_int(*buf++) << 4;
+ ch.b[2] |= hex_to_int(*buf++);
+ ch.b[3] = hex_to_int(*buf++) << 4;
+ ch.b[3] |= hex_to_int(*buf++);
if (!gdbstub_write_dword(mem,ch.l))
return NULL;
mem += 4;
@@ -779,10 +762,10 @@ static char *hex2mem(const char *buf, void *_mem, int count)
}
if (count>=2) {
- ch.b[0] = hex(*buf++) << 4;
- ch.b[0] |= hex(*buf++);
- ch.b[1] = hex(*buf++) << 4;
- ch.b[1] |= hex(*buf++);
+ ch.b[0] = hex_to_int(*buf++) << 4;
+ ch.b[0] |= hex_to_int(*buf++);
+ ch.b[1] = hex_to_int(*buf++) << 4;
+ ch.b[1] |= hex_to_int(*buf++);
if (!gdbstub_write_word(mem,ch.w))
return NULL;
mem += 2;
@@ -790,8 +773,8 @@ static char *hex2mem(const char *buf, void *_mem, int count)
}
if (count>=1) {
- ch.b[0] = hex(*buf++) << 4;
- ch.b[0] |= hex(*buf++);
+ ch.b[0] = hex_to_int(*buf++) << 4;
+ ch.b[0] |= hex_to_int(*buf++);
if (!gdbstub_write_byte(mem,ch.b[0]))
return NULL;
}
@@ -1471,22 +1454,22 @@ void gdbstub(int sigval)
*ptr++ = 'O';
ptr = mem2hex(title, ptr, sizeof(title) - 1,0);
- hx = hexchars[(brr & 0xf0000000) >> 28];
- *ptr++ = hexchars[hx >> 4]; *ptr++ = hexchars[hx & 0xf];
- hx = hexchars[(brr & 0x0f000000) >> 24];
- *ptr++ = hexchars[hx >> 4]; *ptr++ = hexchars[hx & 0xf];
- hx = hexchars[(brr & 0x00f00000) >> 20];
- *ptr++ = hexchars[hx >> 4]; *ptr++ = hexchars[hx & 0xf];
- hx = hexchars[(brr & 0x000f0000) >> 16];
- *ptr++ = hexchars[hx >> 4]; *ptr++ = hexchars[hx & 0xf];
- hx = hexchars[(brr & 0x0000f000) >> 12];
- *ptr++ = hexchars[hx >> 4]; *ptr++ = hexchars[hx & 0xf];
- hx = hexchars[(brr & 0x00000f00) >> 8];
- *ptr++ = hexchars[hx >> 4]; *ptr++ = hexchars[hx & 0xf];
- hx = hexchars[(brr & 0x000000f0) >> 4];
- *ptr++ = hexchars[hx >> 4]; *ptr++ = hexchars[hx & 0xf];
- hx = hexchars[(brr & 0x0000000f)];
- *ptr++ = hexchars[hx >> 4]; *ptr++ = hexchars[hx & 0xf];
+ hx = hex_asc_hi(brr >> 24);
+ *ptr++ = hex_asc_hi(hx); *ptr++ = hex_asc_lo(hx);
+ hx = hex_asc_lo(brr >> 24);
+ *ptr++ = hex_asc_hi(hx); *ptr++ = hex_asc_lo(hx);
+ hx = hex_asc_hi(brr >> 16);
+ *ptr++ = hex_asc_hi(hx); *ptr++ = hex_asc_lo(hx);
+ hx = hex_asc_lo(brr >> 16);
+ *ptr++ = hex_asc_hi(hx); *ptr++ = hex_asc_lo(hx);
+ hx = hex_asc_hi(brr >> 8);
+ *ptr++ = hex_asc_hi(hx); *ptr++ = hex_asc_lo(hx);
+ hx = hex_asc_lo(brr >> 8);
+ *ptr++ = hex_asc_hi(hx); *ptr++ = hex_asc_lo(hx);
+ hx = hex_asc_hi(brr);
+ *ptr++ = hex_asc_hi(hx); *ptr++ = hex_asc_lo(hx);
+ hx = hex_asc_lo(brr);
+ *ptr++ = hex_asc_hi(hx); *ptr++ = hex_asc_lo(hx);
ptr = mem2hex(crlf, ptr, sizeof(crlf) - 1, 0);
*ptr = 0;
@@ -1500,12 +1483,12 @@ void gdbstub(int sigval)
/* Send trap type (converted to signal) */
*ptr++ = 'T';
- *ptr++ = hexchars[sigval >> 4];
- *ptr++ = hexchars[sigval & 0xf];
+ *ptr++ = hex_asc_hi(sigval);
+ *ptr++ = hex_asc_lo(sigval);
/* Send Error PC */
- *ptr++ = hexchars[GDB_REG_PC >> 4];
- *ptr++ = hexchars[GDB_REG_PC & 0xf];
+ *ptr++ = hex_asc_hi(GDB_REG_PC);
+ *ptr++ = hex_asc_lo(GDB_REG_PC);
*ptr++ = ':';
ptr = mem2hex(&__debug_frame->pc, ptr, 4, 0);
*ptr++ = ';';
@@ -1513,8 +1496,8 @@ void gdbstub(int sigval)
/*
* Send frame pointer
*/
- *ptr++ = hexchars[GDB_REG_FP >> 4];
- *ptr++ = hexchars[GDB_REG_FP & 0xf];
+ *ptr++ = hex_asc_hi(GDB_REG_FP);
+ *ptr++ = hex_asc_lo(GDB_REG_FP);
*ptr++ = ':';
ptr = mem2hex(&__debug_frame->fp, ptr, 4, 0);
*ptr++ = ';';
@@ -1522,8 +1505,8 @@ void gdbstub(int sigval)
/*
* Send stack pointer
*/
- *ptr++ = hexchars[GDB_REG_SP >> 4];
- *ptr++ = hexchars[GDB_REG_SP & 0xf];
+ *ptr++ = hex_asc_hi(GDB_REG_SP);
+ *ptr++ = hex_asc_lo(GDB_REG_SP);
*ptr++ = ':';
ptr = mem2hex(&__debug_frame->sp, ptr, 4, 0);
*ptr++ = ';';
@@ -1548,8 +1531,8 @@ void gdbstub(int sigval)
/* request repeat of last signal number */
case '?':
output_buffer[0] = 'S';
- output_buffer[1] = hexchars[sigval >> 4];
- output_buffer[2] = hexchars[sigval & 0xf];
+ output_buffer[1] = hex_asc_hi(sigval);
+ output_buffer[2] = hex_asc_lo(sigval);
output_buffer[3] = 0;
break;
@@ -2059,8 +2042,8 @@ void gdbstub_exit(int status)
}
gdbstub_tx_char('#');
- gdbstub_tx_char(hexchars[checksum >> 4]);
- gdbstub_tx_char(hexchars[checksum & 0xf]);
+ gdbstub_tx_char(hex_asc_hi(checksum));
+ gdbstub_tx_char(hex_asc_lo(checksum));
/* make sure the output is flushed, or else RedBoot might clobber it */
gdbstub_tx_char('-');
--
1.5.5.1.305.g7c84
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 5/9] frv: gdb-stub.c use the common ascii hex helpers
2008-05-01 23:05 [PATCH 5/9] frv: gdb-stub.c use the common ascii hex helpers Harvey Harrison
@ 2008-05-02 2:09 ` David Howells
2008-05-02 2:14 ` Harvey Harrison
0 siblings, 1 reply; 5+ messages in thread
From: David Howells @ 2008-05-02 2:09 UTC (permalink / raw)
To: Harvey Harrison; +Cc: dhowells, Andrew Morton, LKML
Harvey Harrison <harvey.harrison@gmail.com> wrote:
> arch/frv/kernel/gdb-stub.c | 163 ++++++++++++++++++++------------------------
> 1 files changed, 73 insertions(+), 90 deletions(-)
Can you send me the base patch this is built on? These functions you're using
don't seem to be upstream.
David
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 5/9] frv: gdb-stub.c use the common ascii hex helpers
2008-05-02 2:09 ` David Howells
@ 2008-05-02 2:14 ` Harvey Harrison
2008-05-09 12:07 ` David Howells
0 siblings, 1 reply; 5+ messages in thread
From: Harvey Harrison @ 2008-05-02 2:14 UTC (permalink / raw)
To: David Howells; +Cc: Andrew Morton, LKML
On Fri, 2008-05-02 at 03:09 +0100, David Howells wrote:
> Harvey Harrison <harvey.harrison@gmail.com> wrote:
>
> > arch/frv/kernel/gdb-stub.c | 163 ++++++++++++++++++++------------------------
> > 1 files changed, 73 insertions(+), 90 deletions(-)
>
> Can you send me the base patch this is built on? These functions you're using
> don't seem to be upstream.
>
> David
They get introduced in 1/9:
Subject: [PATCH 1/9] lib: add ascii hex helper functions
Everyone rolls their own version around the tree, centralize
in lib/hexdump.c
Signed-off-by: Harvey Harrison <harvey.harrison@gmail.com>
---
include/linux/kernel.h | 6 +++++-
lib/hexdump.c | 25 +++++++++++++++++++++++--
2 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 4d46e29..20cae9a 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -276,7 +276,11 @@ extern void print_hex_dump(const char *level, const char *prefix_str,
const void *buf, size_t len, bool ascii);
extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
const void *buf, size_t len);
-#define hex_asc(x) "0123456789abcdef"[x]
+
+extern const char hex_asc[];
+extern int hex_to_int(char c);
+#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
+#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
#define pr_emerg(fmt, arg...) \
printk(KERN_EMERG fmt, ##arg)
diff --git a/lib/hexdump.c b/lib/hexdump.c
index 3435465..fa1a76e 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -12,6 +12,27 @@
#include <linux/kernel.h>
#include <linux/module.h>
+const char hex_asc[] = "0123456789abcdef";
+
+/**
+ * hex_to_int - convert a single hex ASCII char to an integer value
+ * @ch: char to convert, not case sensitive [a-f][A-F][0-9]
+ *
+ * Returns -1 if the char is not a valid hexadecimal character
+ */
+int hex_to_int(char ch)
+{
+ /*
+ * Make ch lower-case, works only for digits and letters
+ */
+ ch |= 0x20;
+ if (ch >= 'a' && ch <= 'f')
+ return ch - 'a' + 10;
+ if (ch >= '0' && ch <= '9')
+ return ch - '0';
+ return -1;
+}
+
/**
* hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
* @buf: data blob to dump
@@ -93,8 +114,8 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
for (j = 0; (j < rowsize) && (j < len) && (lx + 4) < linebuflen;
j++) {
ch = ptr[j];
- linebuf[lx++] = hex_asc(ch >> 4);
- linebuf[lx++] = hex_asc(ch & 0x0f);
+ linebuf[lx++] = hex_asc_hi(ch);
+ linebuf[lx++] = hex_asc_lo(ch);
linebuf[lx++] = ' ';
}
ascii_column = 3 * rowsize + 2;
--
1.5.5.1.305.g7c84
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 5/9] frv: gdb-stub.c use the common ascii hex helpers
2008-05-02 2:14 ` Harvey Harrison
@ 2008-05-09 12:07 ` David Howells
2008-05-09 17:28 ` Harvey Harrison
0 siblings, 1 reply; 5+ messages in thread
From: David Howells @ 2008-05-09 12:07 UTC (permalink / raw)
To: Harvey Harrison; +Cc: dhowells, Andrew Morton, LKML
Harvey Harrison <harvey.harrison@gmail.com> wrote:
> Everyone rolls their own version around the tree, centralize
> in lib/hexdump.c
Can you instruct that these are only to be used for GDB stubs? Otherwise if
someone slaps a breakpoint in there accidentally, they might kill the GDB
stub.
David
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 5/9] frv: gdb-stub.c use the common ascii hex helpers
2008-05-09 12:07 ` David Howells
@ 2008-05-09 17:28 ` Harvey Harrison
0 siblings, 0 replies; 5+ messages in thread
From: Harvey Harrison @ 2008-05-09 17:28 UTC (permalink / raw)
To: David Howells; +Cc: Andrew Morton, LKML
On Fri, 2008-05-09 at 13:07 +0100, David Howells wrote:
> Harvey Harrison <harvey.harrison@gmail.com> wrote:
>
> > Everyone rolls their own version around the tree, centralize
> > in lib/hexdump.c
>
> Can you instruct that these are only to be used for GDB stubs? Otherwise if
> someone slaps a breakpoint in there accidentally, they might kill the GDB
> stub.
>
This isn't a problem for hex_asc_{hi|lo} obviously, just the
hexchar_to_int function, I've been looking at making this available in
another way due to its similarity to simple_strtol etc (akpm noticed).
So the hex_asc_hi/lo helpers are probably useful as-is. I'll make
sure to note the effects on gdb as you've pointed out when I send
out the revised usage.
Cheers,
Harvey
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2008-05-09 17:28 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-05-01 23:05 [PATCH 5/9] frv: gdb-stub.c use the common ascii hex helpers Harvey Harrison
2008-05-02 2:09 ` David Howells
2008-05-02 2:14 ` Harvey Harrison
2008-05-09 12:07 ` David Howells
2008-05-09 17:28 ` Harvey Harrison
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox