Add a new command in kgdb for vmcoreinfo
Subsystems (4)
v3 cover Add a new command in kgdb for vmcoreinfo
From: Amal Raj T <[email protected]>
This patch set introduces a new custom query packet, qlinux.vmcoreinfo, to kgdb. This packet retrieves kernel's vmcoreinfo text, which contains crucial debugging information such as the KASLR offset, kernel release, phys_base etc.
This implementation includes a new binary-encoder (mem2ebin) instead of the existing hex-encoder (mem2hex) to reduce the amount of data sent over the wire.
These changes also move the LF -> CRLF replacement logic from serial drivers to KDB, since KDB is the only user of this logic, and the existing replacement results in incorrect checksums generated by KGDB.
Link:
- https://github.com/osandov/drgn/wiki/GDB-Remote-Protocol-proposal:-linux.vmcoreinfo-query-packet
v3:
- Added relevant links for the new query
- Updated tmp+=1 to tmp++
- Added few examples and additional documentation for encoding function Amal Raj T (3):
kgdb: Add kgdb_mem2ebin function for converting memory to binary
format
serial: Move LF -> CRLF replacement from serial console to kdb
kgdb: Add command linux.vmcoreinfo to kgdb
drivers/tty/serial/serial_core.c | 2 - include/linux/kgdb.h | 1 + kernel/debug/gdbstub.c | 131 ++++++++++++++++++++----------- kernel/debug/kdb/kdb_io.c | 2 + lib/Kconfig.kgdb | 1 + 5 files changed, 91 insertions(+), 46 deletions(-)
Signature · 1 line
2.43.5
PATCH v3 1/3 kgdb: Add kgdb_mem2ebin function for converting memory to binary format
From: Amal Raj T <[email protected]>
Add a new function kgdb_mem2ebin that converts memory to binary format, escaping special characters ('$', '#', '*', and '}'). These ASCII characters have the following meaning in GDB binary encoding
- $: Seven repeats in run-length encoding
- #: Six repeats
- *: Three repeats
- }: Used as escape characterkgdb_mem2ebin function ensures that memory data
is properly formatted and escaped before being
sent over the wire. Additionally, this function
reduces the amount of data exchanged between
debugger compared to hex.
--- include/linux/kgdb.h | 1 + kernel/debug/gdbstub.c | 119 ++++++++++++++++++++++++++--------------- 2 files changed, 77 insertions(+), 43 deletions(-)
include/linux/kgdb.h +1 -0
@@ -322,6 +322,7 @@ extern struct kgdb_io *dbg_io_ops;extern int kgdb_hex2long (char **ptr,unsigned long *long_val);extern char *kgdb_mem2hex (char *mem,char *buf,int count); +extern char *kgdb_mem2ebin (char *mem,char *buf,int count);extern int kgdb_hex2mem (char *buf,char *mem,int count);extern int kgdb_isremovedbreak (unsigned long addr);
kernel/debug/gdbstub.c +76 -43
229 lines not on this page · raw message
Signature · 1 line
2.43.5
Re: [PATCH v3 1/3] kgdb: Add kgdb_mem2ebin function for converting memory to binary format
Hi Amal,
Quoted 8 lines · Amal Raj T
From: Amal Raj T <[email protected]>
Add a new function
kgdb_mem2ebinthat converts memory to binary format, escaping special characters ('$', '#', '*', and '}'). These ASCII characters have the following meaning in GDB binary encoding
-$: Seven repeats in run-length encoding
-#: Six repeats
-*: Three repeats
These aren't the justification for escaping. From the GDB doc linked below:
- '$' is used to introduce a packet
- '#' is used to end a packet
These cannot appear in any of the packet contents, otherwise they would confuse the code which determines the packet framing. So they must be escaped.
In fact, in run-length encoding, these characters are banned -- so they don't represent seven or six repeats, they simply cannot be used, for the same reason.
- '*' is used to introduce a run-length encoding
Note that the language of the document indicates that only the "stub" must escape the '*', because only the stub can use run-length encoding. Presumably the debugger side of the connection need not escape it, because the protocol is designed to allow the stub to be implemented more simply, without needing how to interpret run-length encodings.
I don't think you need to include that level of detail, but the short description of each character and the reasoning would be good. And put the same corrected description in the comment below.
Quoted 40 lines
-
}: Used as escape characterkgdb_mem2ebinfunction ensures that memory data
is properly formatted and escaped before being
sent over the wire. Additionally, this function
reduces the amount of data exchanged between
debugger compared to hex.Signed-off-by Amal Raj T <[email protected]>--- include/linux/kgdb.h | 1 + kernel/debug/gdbstub.c | 119 ++++++++++++++++++++++++++--------------- 2 files changed, 77 insertions(+), 43 deletions(-)include/linux/kgdb.h +1
-0@@ -322,6 +322,7 @@ extern struct kgdb_io *dbg_io_ops;extern int kgdb_hex2long (char **ptr,unsigned long *long_val);extern char *kgdb_mem2hex (char *mem,char *buf,int count); +extern char *kgdb_mem2ebin (char *mem,char *buf,int count);extern int kgdb_hex2mem (char *buf,char *mem,int count);extern int kgdb_isremovedbreak (unsigned long addr);kernel/debug/gdbstub.c +1
-4@@ -39,15 +39,14 @@#define KGDB_MAX_THREAD_QUERY 17/* Our I/O buffers. */ -static char remcom_in_buffer[BUFMAX ]; -static char remcom_out_buffer[BUFMAX ]; -static int gdbstub_use_prev_in_buf; -static int gdbstub_prev_in_buf_pos; +static char remcom_in_buffer[BUFMAX ];
kernel/debug/gdbstub.c line 49
Quoted 10 lines
kernel/debug/gdbstub.c +5
-3+static char remcom_out_buffer[BUFMAX ]; +static int gdbstub_use_prev_in_buf; +static int gdbstub_prev_in_buf_pos;/* Storage for the registers, in GDB format. */ -static unsigned long gdb_regs[(NUMREGBYTES + -sizeof (unsigned long ) -1 ) / -sizeof (unsigned long )]; +static unsigned long gdb_regs[(NUMREGBYTES +sizeof (unsigned long ) -1 ) / +sizeof (unsigned long )];
It looks like the majority of the code changes in this patch are formatting, maybe due to an automatic formatter?
Generally it's bad practice to include those sorts of changes since they're unrelated to the subject of this patch.
Quoted 20 lines
kernel/debug/gdbstub.c +13
-0/* * GDB remote protocol parser: @@ -257,6 +256,49 @@ char *kgdb_mem2hex(char *mem, char *buf, int count)return buf; } +/** +* Convert memory to binary format for GDB remote protocol transmission, +* escaping special characters ($, #, *, and }) using the `}` character as an escape. +* +* The `$` (seven repeats), `#`(six repeats), `*`(three run-length), and `}` +* characters are considered special because they have +* specific meanings in the GDB remote protocol. To avoid conflicts, these +* characters are escaped by prefixing them with the `}` character and then XORing +* the original character with 0x20. +* +* Examples: +* - `0x7d` (ASCII '}') is transmitted as `0x7d 0x5d` +* - `0x23` (ASCII '#') is transmitted as `0x7d 0x43`
kernel/debug/gdbstub.c char *kgdb_mem2hex(char *mem, char *buf, int count) · line 281
Quoted 10 lines
kernel/debug/gdbstub.c +10
-0+*/ +char *kgdb_mem2ebin (char *mem,char *buf,int count) +{ +char *tmp; +int err; + +/* We use the upper half of buf as an intermediate buffer +* for the raw memory copy. +*/ + tmp = buf + count;
It's a fun mental experiment to confirm that, even if every character in the source buffer required escaping, this function would still work :)
Quoted 5 lines
kernel/debug/gdbstub.c +5
-0+ err =copy_from_kernel_nofault (tmp, mem, count); +if (err) +return NULL ; +while (count >0 ) { +unsigned char c = *tmp;
kernel/debug/gdbstub.c char *kgdb_mem2hex(char *mem, char *buf, int count) · line 297
Quoted 10 lines
kernel/debug/gdbstub.c +10
-0+ +if (c =='}' || c =='#' || c =='*' || c =='#' ) { + *buf++ ='}' ; + *buf++ = c ^0x20 ; + }else { + *buf++ = c; + } + count -=1 ; + tmp +=1 ; + }
Give that the caller can't know how many bytes the escaped data will consume, how does this function return that information? Is the idea that "count" includes the nul byte, and so the final iteration of this loop will nul-terminate the buffer?
Thanks,
Stephen
Quoted 181 lines
kernel/debug/gdbstub.c +30
-36+return buf; +} +/* * Convert the hex array pointed to by buf into binary to be placed in * mem. Return a pointer to the character AFTER the last byte @@ -400,7 +442,7 @@ static void error_packet(char *pkt, int error)* remapped to negative TIDs. */ -#define BUF_THREAD_ID_SIZE 8 +#define BUF_THREAD_ID_SIZE 8static char *pack_threadid (char *pkt,unsigned char *id) {@@ -454,7 +496,6 @@ static struct task_struct *getthread(struct pt_regs *regs, int tid)return find_task_by_pid_ns (tid, &init_pid_ns); } -/* * Remap normal tasks to their real PID, * CPU shadow threads are mapped to -CPU - 2 @@ -560,7 +601,7 @@ static void gdb_cmd_memread(struct kgdb_state *ks)char *err;if (kgdb_hex2long (&ptr, &addr) >0 && *ptr++ ==',' && -kgdb_hex2long (&ptr, &length) >0 ) { +kgdb_hex2long (&ptr, &length) >0 ) { err =kgdb_mem2hex ((char *)addr, remcom_out_buffer, length);if (!err)error_packet (remcom_out_buffer, -EINVAL );@@ -615,8 +656,7 @@ static void gdb_cmd_reg_set(struct kgdb_state *ks)int i =0 ;kgdb_hex2long (&ptr, ®num); -if (*ptr++ !='=' || - !(!kgdb_usethread || kgdb_usethread == current) || +if (*ptr++ !='=' || !(!kgdb_usethread || kgdb_usethread == current) || !dbg_get_reg (regnum, gdb_regs, ks->linux_regs)) {error_packet (remcom_out_buffer, -EINVAL );return ;@@ -756,14 +796,14 @@ static void gdb_cmd_query(struct kgdb_state *ks)break ; }if ((int )ks->threadid >0 ) { -kgdb_mem2hex (getthread (ks->linux_regs, - ks->threadid)->comm, - remcom_out_buffer,16 ); +kgdb_mem2hex ( +getthread (ks->linux_regs, ks->threadid)->comm, + remcom_out_buffer,16 ); }else {static char tmpstr[23 +BUF_THREAD_ID_SIZE ];sprintf (tmpstr,"shadowCPU%d" , - (int )(-ks->threadid -2 )); + (int )(-ks->threadid -2 ));kgdb_mem2hex (tmpstr, remcom_out_buffer,strlen (tmpstr)); }break ;@@ -776,8 +816,8 @@ static void gdb_cmd_query(struct kgdb_state *ks)strcpy (remcom_out_buffer,"E01" );break ; } -kgdb_hex2mem (remcom_in_buffer +6 , - remcom_out_buffer, len); +kgdb_hex2mem (remcom_in_buffer +6 , remcom_out_buffer, + len); len = len /2 ; remcom_out_buffer[len++] =0 ;@@ -895,8 +935,7 @@ static void gdb_cmd_break(struct kgdb_state *ks)error_packet (remcom_out_buffer, -EINVAL );return ; } -if (*(ptr++) !=',' || - !kgdb_hex2long (&ptr, &length)) { +if (*(ptr++) !=',' || !kgdb_hex2long (&ptr, &length)) {error_packet (remcom_out_buffer, -EINVAL );return ; }@@ -906,11 +945,11 @@ static void gdb_cmd_break(struct kgdb_state *ks)else if (remcom_in_buffer[0 ] =='z' && *bpt_type =='0' ) error =dbg_remove_sw_break (addr);else if (remcom_in_buffer[0 ] =='Z' ) - error = arch_kgdb_ops.set_hw_breakpoint (addr, - (int )length, *bpt_type -'0' ); + error = arch_kgdb_ops.set_hw_breakpoint (addr, (int )length, + *bpt_type -'0' );else if (remcom_in_buffer[0 ] =='z' ) - error = arch_kgdb_ops.remove_hw_breakpoint (addr, - (int ) length, *bpt_type -'0' ); + error = arch_kgdb_ops.remove_hw_breakpoint (addr, (int )length, + *bpt_type -'0' );if (error ==0 )strcpy (remcom_out_buffer,"OK" );@@ -925,12 +964,10 @@ static int gdb_cmd_exception_pass(struct kgdb_state *ks)* C15 == detach kgdb, pass exception */ if (remcom_in_buffer[1 ] =='0' && remcom_in_buffer[2 ] =='9' ) { - ks->pass_exception =1 ; remcom_in_buffer[0 ] ='c' ; }else if (remcom_in_buffer[1 ] =='1' && remcom_in_buffer[2 ] =='5' ) { - ks->pass_exception =1 ; remcom_in_buffer[0 ] ='D' ;dbg_remove_all_break ();@@ -938,9 +975,11 @@ static int gdb_cmd_exception_pass(struct kgdb_state *ks)return 1 ; }else { -gdbstub_msg_write ("KGDB only knows signal 9 (pass)" +gdbstub_msg_write ( +"KGDB only knows signal 9 (pass)" " and 15 (pass and disconnect)\n" -"Executing a continue without signal passing\n" ,0 ); +"Executing a continue without signal passing\n" , +0 ); remcom_in_buffer[0 ] ='c' ; }@@ -1050,7 +1089,7 @@ int gdb_serial_stub(struct kgdb_state *ks) goto default_handle;if (tmp ==0 )break ; - fallthrough;/* on tmp < 0 */ + fallthrough;/* on tmp < 0 */ case 'c' :/* Continue packet */ case 's' :/* Single step packet */ if (kgdb_contthread && kgdb_contthread != current) {@@ -1058,15 +1097,13 @@ int gdb_serial_stub(struct kgdb_state *ks)error_packet (remcom_out_buffer, -EINVAL );break ; } - fallthrough;/* to default processing */ + fallthrough;/* to default processing */ default : default_handle: - error =kgdb_arch_handle_exception (ks->ex_vector, - ks->signo, - ks->err_code, - remcom_in_buffer, - remcom_out_buffer, - ks->linux_regs); + error =kgdb_arch_handle_exception ( + ks->ex_vector, ks->signo, ks->err_code, + remcom_in_buffer, remcom_out_buffer, + ks->linux_regs);/* * Leave cmd processing on error, detach, * kill, continue, or single step. @@ -1076,7 +1113,6 @@ int gdb_serial_stub(struct kgdb_state *ks) error =0 ; goto kgdb_exit; } - }/* reply to the request */ @@ -1095,12 +1131,9 @@ int gdbstub_state(struct kgdb_state *ks, char *cmd)switch (cmd[0 ]) {case 'e' : - error =kgdb_arch_handle_exception (ks->ex_vector, - ks->signo, - ks->err_code, - remcom_in_buffer, - remcom_out_buffer, - ks->linux_regs); + error =kgdb_arch_handle_exception ( + ks->ex_vector, ks->signo, ks->err_code, + remcom_in_buffer, remcom_out_buffer, ks->linux_regs);return error;case 's' :case 'c' :Signature · 1 line
2.43.5
PATCH v3 2/3 kgdb: Add command linux.vmcoreinfo to kgdb
In reply to Amal Raj TFrom: Amal Raj T <[email protected]>
The current implementation of poll_put_char in the serial console driver performs LF -> CRLF replacement, which can corrupt binary data. Since kdb is the only user of poll_put_char, this patch moves the LF -> CRLF replacement logic to kdb.
--- drivers/tty/serial/serial_core.c | 2 -- kernel/debug/kdb/kdb_io.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/tty/serial/serial_core.c +0 -2
@@ -2738,8 +2738,6 @@ static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)if (!port)return ; -if (ch =='\n' ) - port->ops->poll_put_char (port,'\r' ); port->ops->poll_put_char (port, ch);uart_port_deref (port); }
kernel/debug/kdb/kdb_io.c +2 -0
@@ -572,6 +572,8 @@ static void kdb_msg_write(const char *msg, int msg_len) len = msg_len;while (len--) { +if (*cp =='\n' ) + dbg_io_ops->write_char ('\r' ); dbg_io_ops->write_char (*cp); cp++; }
Signature · 1 line
2.43.5
Re: [PATCH v3 2/3] kgdb: Add command linux.vmcoreinfo to kgdb
Hi Amal,
There are two "PATCH v3 2/3" messages. This one's contents do not match its subject line, so I assume that the correct one is "[PATCH v3 2/3]
serial: Move LF -> CRLF replacement from serial console to kdb".
Thanks,
Stephen
Quoted 41 lines · Amal Raj T
From: Amal Raj T <[email protected]>
The current implementation of
poll_put_charin the serial console driver performs LF -> CRLF replacement, which can corrupt binary data. Since kdb is the only user ofpoll_put_char, this patch moves the LF -> CRLF replacement logic to kdb.--- drivers/tty/serial/serial_core.c | 2 -- kernel/debug/kdb/kdb_io.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-)drivers/tty/serial/serial_core.c +0
-2@@ -2738,8 +2738,6 @@ static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)if (!port)return ; -if (ch =='\n' ) - port->ops->poll_put_char (port,'\r' ); port->ops->poll_put_char (port, ch);uart_port_deref (port); }kernel/debug/kdb/kdb_io.c +2
-0@@ -572,6 +572,8 @@ static void kdb_msg_write(const char *msg, int msg_len) len = msg_len;while (len--) { +if (*cp =='\n' ) + dbg_io_ops->write_char ('\r' ); dbg_io_ops->write_char (*cp); cp++; }Signature · 1 line
2.43.5
PATCH v3 2/3 serial: Move LF -> CRLF replacement from serial console to kdb
In reply to Amal Raj TFrom: Amal Raj T <[email protected]>
The current implementation of poll_put_char in the serial console driver performs LF -> CRLF replacement, which can corrupt binary data. Since kdb is the only user of poll_put_char, this patch moves the LF -> CRLF replacement logic to kdb.
--- drivers/tty/serial/serial_core.c | 2 -- kernel/debug/kdb/kdb_io.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/tty/serial/serial_core.c +0 -2
@@ -2738,8 +2738,6 @@ static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)if (!port)return ; -if (ch =='\n' ) - port->ops->poll_put_char (port,'\r' ); port->ops->poll_put_char (port, ch);uart_port_deref (port); }
kernel/debug/kdb/kdb_io.c +2 -0
@@ -572,6 +572,8 @@ static void kdb_msg_write(const char *msg, int msg_len) len = msg_len;while (len--) { +if (*cp =='\n' ) + dbg_io_ops->write_char ('\r' ); dbg_io_ops->write_char (*cp); cp++; }
Signature · 1 line
2.43.5
Re: [PATCH v3 2/3] serial: Move LF -> CRLF replacement from serial console to kdb
Quoted 8 lines · Amal Raj T
From: Amal Raj T <[email protected]>
The current implementation of
poll_put_charin the serial console driver performs LF -> CRLF replacement, which can corrupt binary data. Since kdb is the only user ofpoll_put_char, this patch moves the LF -> CRLF replacement logic to kdb.
Quoted 33 lines
--- drivers/tty/serial/serial_core.c | 2 -- kernel/debug/kdb/kdb_io.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-)drivers/tty/serial/serial_core.c +0
-2@@ -2738,8 +2738,6 @@ static void uart_poll_put_char(struct tty_driver *driver, int line, char ch)if (!port)return ; -if (ch =='\n' ) - port->ops->poll_put_char (port,'\r' ); port->ops->poll_put_char (port, ch);uart_port_deref (port); }kernel/debug/kdb/kdb_io.c +2
-0@@ -572,6 +572,8 @@ static void kdb_msg_write(const char *msg, int msg_len) len = msg_len;while (len--) { +if (*cp =='\n' ) + dbg_io_ops->write_char ('\r' ); dbg_io_ops->write_char (*cp); cp++; }Signature · 1 line
2.43.5
Re: [PATCH v3 2/3] serial: Move LF -> CRLF replacement from serial console to kdb
In reply to Amal Raj THi,
Quoted 8 lines · Amal Raj T
From: Amal Raj T <[email protected]>
The current implementation of
poll_put_charin the serial console driver performs LF -> CRLF replacement, which can corrupt binary data. Since kdb is the only user ofpoll_put_char, this patch moves the LF -> CRLF replacement logic to kdb.
Quoted 5 lines
--- drivers/tty/serial/serial_core.c | 2 -- kernel/debug/kdb/kdb_io.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-)
You seem to have dropped your Signed-off-by? It was there on V2 [1]. ...and speaking of V2, I provided my Reviewed-by there. You should have carried it forward since there are no differences in the code between V2 and V3. I can give it again, but it would be nice not to need to repeat when you send the V4.
Once you've added back your own Signed-off-by, you can also add:
...also speaking of V2, I did ask if you could maybe add something to the commit message pointing at the previous discussion. If you really have a reason not to do this I won't insist, but it would be nice to include it...
PATCH v3 3/3 kgdb: Add command linux.vmcoreinfo to kgdb
In reply to Amal Raj TFrom: Amal Raj T <[email protected]>
Add a new query linux.vmcoreinfo to kgdb that returns vmcoreinfo to the client using the mem2ebin encoding. Maximum size of output buffer is set to 2x the maximum size of VMCOREINFO_BYTES (kgdb_mem2ebin() requires 1x for the temporary copy plus another 1x (max) for the escaped data).
--- kernel/debug/gdbstub.c | 18 ++++++++++++++---- lib/Kconfig.kgdb | 1 + 2 files changed, 15 insertions(+), 4 deletions(-)
kernel/debug/gdbstub.c +14 -4
@@ -30,20 +30,21 @@#include <linux/kgdb.h> #include <linux/kdb.h> #include <linux/serial_core.h> +#include <linux/string.h> #include <linux/reboot.h> #include <linux/uaccess.h> #include <asm/cacheflush.h> #include <linux/unaligned.h> +#include <linux/vmcore_info.h> #include "debug_core.h" #define KGDB_MAX_THREAD_QUERY 17/* Our I/O buffers. */ static char remcom_in_buffer[BUFMAX ]; -static char remcom_out_buffer[BUFMAX ]; +static char remcom_out_buffer[MAX (VMCOREINFO_BYTES *2 ,BUFMAX )];static int gdbstub_use_prev_in_buf;static int gdbstub_prev_in_buf_pos; -/* Storage for the registers, in GDB format. */ static unsigned long gdb_regs[(NUMREGBYTES +sizeof (unsigned long ) -1 ) /sizeof (unsigned long )];
@@ -292,8 +293,8 @@ char *kgdb_mem2ebin(char *mem, char *buf, int count) }else { *buf++ = c; } - count -=1 ; - tmp +=1 ; + count--; + tmp++; }return buf;
@@ -777,6 +778,15 @@ static void gdb_cmd_query(struct kgdb_state *ks) *(--ptr) ='\0' ;break ; +case 'l' : +if (strncmp (remcom_in_buffer +1 ,"linux.vmcoreinfo" ,17 ) == +0 ) { + remcom_out_buffer[0 ] ='Q' ; +kgdb_mem2ebin (vmcoreinfo_data, remcom_out_buffer +1 , + vmcoreinfo_size); + } +break ; +case 'C' :/* Current thread id */ strcpy (remcom_out_buffer,"QC" );
lib/Kconfig.kgdb +1 -0
@@ -12,6 +12,7 @@ menuconfig KGDB bool "KGDB: kernel debugger" depends on HAVE_ARCH_KGDB depends on DEBUG_KERNEL + select VMCORE_INFO help If you say Y here, it will be possible to remotely debug the kernel using gdb. It is recommended but not required, that
Signature · 1 line
2.43.5
Re: [PATCH v3 3/3] kgdb: Add command linux.vmcoreinfo to kgdb
Quoted 27 lines · Amal Raj T
From: Amal Raj T <[email protected]>
Add a new query
linux.vmcoreinfoto kgdb that returns vmcoreinfo to the client using the mem2ebin encoding. Maximum size of output buffer is set to 2x the maximum size ofVMCOREINFO_BYTES(kgdb_mem2ebin()requires 1x for the temporary copy plus another 1x (max) for the escaped data).--- kernel/debug/gdbstub.c | 18 ++++++++++++++---- lib/Kconfig.kgdb | 1 + 2 files changed, 15 insertions(+), 4 deletions(-)kernel/debug/gdbstub.c +1
-0@@ -30,20 +30,21 @@#include <linux/kgdb.h> #include <linux/kdb.h> #include <linux/serial_core.h> +#include <linux/string.h> #include <linux/reboot.h> #include <linux/uaccess.h> #include <asm/cacheflush.h>
kernel/debug/gdbstub.c line 45
Quoted 10 lines
kernel/debug/gdbstub.c +2
-1#include <linux/unaligned.h> +#include <linux/vmcore_info.h> #include "debug_core.h" #define KGDB_MAX_THREAD_QUERY 17/* Our I/O buffers. */ static char remcom_in_buffer[BUFMAX ]; -static char remcom_out_buffer[BUFMAX ]; +static char remcom_out_buffer[MAX (VMCOREINFO_BYTES *2 ,BUFMAX )];
Looking at the code added to gdb_cmd_query(), the actual maximum size of the response is VMCOREINFO_BYTES * 2 + 1, to account for the 'Q' character. This is a large buffer, for most architectures it would be 8193 bytes, compared to the current 2048 or 4096.
The more I look at this, the more concerned I am that we're going about this wrong. GDB has packet types which allow reading uninterpreted bytes of OS-related data, and they allow specifying a requested offset and length:
https://sourceware.org/gdb/current/onlinedocs/gdb.html/General-Query-Packets.html#qXfer-read
This would ensure that we could break up the data into multiple smaller packets, which may go with the protocol design better. I can't find any documented maximum packet size, but in the GDB remote.c code, I see it being initialized as small as 400.
I'm aware that I come up with the "qlinux.vmcoreinfo" idea so I'm the one who should be chastised for missing this potential limitation if it does exist. I think I'll need to go into the GDB code and play around with this more, and also share this idea with the binutils people who I probably should have consulted in the first place.
Quoted 7 lines
kernel/debug/gdbstub.c +0
-1static int gdbstub_use_prev_in_buf;static int gdbstub_prev_in_buf_pos; -/* Storage for the registers, in GDB format. */ static unsigned long gdb_regs[(NUMREGBYTES +sizeof (unsigned long ) -1 ) /sizeof (unsigned long )];
kernel/debug/gdbstub.c char *kgdb_mem2ebin(char *mem, char *buf, int count) · line 297
Quoted 7 lines
kernel/debug/gdbstub.c +2
-2@@ -292,8 +293,8 @@ char *kgdb_mem2ebin(char *mem, char *buf, int count) }else { *buf++ = c; } - count -=1 ; - tmp +=1 ; + count--; + tmp++;
I don't think this update goes with the rest of this patch. If you'd prefer the "++" and "--" syntax, then please incorporate that into patch 1.
kernel/debug/gdbstub.c static void gdb_cmd_query(struct kgdb_state *ks) · line 786
Quoted 13 lines
kernel/debug/gdbstub.c +6
-0}return buf;@@ -777,6 +778,15 @@ static void gdb_cmd_query(struct kgdb_state *ks) *(--ptr) ='\0' ;break ; +case 'l' : +if (strncmp (remcom_in_buffer +1 ,"linux.vmcoreinfo" ,17 ) == +0 ) { + remcom_out_buffer[0 ] ='Q' ; +kgdb_mem2ebin (vmcoreinfo_data, remcom_out_buffer +1 , + vmcoreinfo_size);
Answering my question on patch 1: vmcoreinfo_size does not include the NUL byte. So when we get to put_packet(), the loop would continue into the staged copy of the data. Effectively, some portion of the original (unescaped) vmcoreinfo would get output after the escaped vmcoreinfo.
Or at least, that's how I read the code. Has this occurred in testing, or is there something I misunderstood in this analysis?
Quoted 11 lines
kernel/debug/gdbstub.c +3
-0+ } +break ; +case 'C' :/* Current thread id */ strcpy (remcom_out_buffer,"QC" );lib/Kconfig.kgdb +0
-0
lib/Kconfig.kgdb menuconfig KGDB · line 15
Quoted 4 lines
lib/Kconfig.kgdb +1
-0@@ -12,6 +12,7 @@ menuconfig KGDB bool "KGDB: kernel debugger" depends on HAVE_ARCH_KGDB depends on DEBUG_KERNEL + select VMCORE_INFO
I don't know enough to know whether this would be an issue in practice, but I'd guess that some people may use kgdb without VMCORE_INFO, and they may be more likely to be in an embedded application where they track memory usage carefully, and where the vmcoreinfo may be less useful to them. It may be wise to avoid selecting VMCORE_INFO, but instead fall back to a stub implementation that returns an error packet indicating that there is no data.
Thanks,
Stephen
Quoted 5 lines
lib/Kconfig.kgdb +0
-0help If you say Y here, it will be possible to remotely debug the kernel using gdb. It is recommended but not required, thatSignature · 1 line
2.43.5
Re: [PATCH v3 3/3] kgdb: Add command linux.vmcoreinfo to kgdb
Quoted 57 lines · Stephen Brennan
Quoted 27 lines · Amal Raj T
From: Amal Raj T <[email protected]>
Add a new query
linux.vmcoreinfoto kgdb that returns vmcoreinfo to the client using the mem2ebin encoding. Maximum size of output buffer is set to 2x the maximum size ofVMCOREINFO_BYTES(kgdb_mem2ebin()requires 1x for the temporary copy plus another 1x (max) for the escaped data).--- kernel/debug/gdbstub.c | 18 ++++++++++++++---- lib/Kconfig.kgdb | 1 + 2 files changed, 15 insertions(+), 4 deletions(-)kernel/debug/gdbstub.c +1
-0@@ -30,20 +30,21 @@#include <linux/kgdb.h> #include <linux/kdb.h> #include <linux/serial_core.h> +#include <linux/string.h> #include <linux/reboot.h> #include <linux/uaccess.h> #include <asm/cacheflush.h> kernel/debug/gdbstub.c line 45
Quoted 10 lines
kernel/debug/gdbstub.c +2
-1#include <linux/unaligned.h> +#include <linux/vmcore_info.h> #include "debug_core.h" #define KGDB_MAX_THREAD_QUERY 17/* Our I/O buffers. */ static char remcom_in_buffer[BUFMAX ]; -static char remcom_out_buffer[BUFMAX ]; +static char remcom_out_buffer[MAX (VMCOREINFO_BYTES *2 ,BUFMAX )];Looking at the code added to
gdb_cmd_query(), the actual maximum size of the response isVMCOREINFO_BYTES* 2 + 1, to account for the 'Q' character. This is a large buffer, for most architectures it would be 8193 bytes, compared to the current 2048 or 4096.The more I look at this, the more concerned I am that we're going about this wrong. GDB has packet types which allow reading uninterpreted bytes of OS-related data, and they allow specifying a requested offset and length:
https://sourceware.org/gdb/current/onlinedocs/gdb.html/General-Query-Packets.html#qXfer-read
This would ensure that we could break up the data into multiple smaller packets, which may go with the protocol design better. I can't find any documented maximum packet size, but in the GDB
remote.ccode, I see it being initialized as small as 400.
Quoted 5 lines
I'm aware that I come up with the "qlinux.vmcoreinfo" idea so I'm the one who should be chastised for missing this potential limitation if it does exist. I think I'll need to go into the GDB code and play around with this more, and also share this idea with the binutils people who I probably should have consulted in the first place.
After checking in with some helpful members of the GDB mailing list[1], it sounds like the answer here is to use the qXfer command linked above. Essentially, the format would be like this:
Request: qXfer:vmcoreinfo:read::OFFSET,LENGTH
Reply:
'm [DATA]' - DATA read from the target OFFSET. More data may be
present. The amount of data may be less than LENGTH.
'l [DATA]' - DATA read from the target offset. There is no more data
to be read. The amount of data may be less than LENGTH.
'l' - There is no data at that offset
The DATA would still be encoded in the same escaped binary format implemented in patch 1. The useful difference here is that we would not need to expand remcom_out_buffer. We simply reply with as much data as requested, or as much as we can fit into the output buffer, whichever is smaller. The client will use multiple requests until the entire data is read.
So patch 1 would need to be updated to be aware of remaining space in the buffer, and it would need to do partial reads. Since the escaping scheme rarely needs to escape characters (especially for vmcoreinfo), we can optimistically fill the whole buffer (not just half) and then we could determine the amount of escapes and do the escaping & shifting all at once. Something like this:
int kgdb_mem2ebin(char *mem, char *buf, int count, int cap)
{
int err, i, esc = 0;
err = copy_from_kernel_nofault(buf, mem, min(count, cap));
if (err)
return err;
/* Some characters need to be escaped, but it's uncommon.
* Count the number of escaped characters and then perform
* the escaping in reverse. */
for (i = 0; i < count && i + esc < cap; i++) {
if (buf[i] == '}' || buf[i] == '#' || buf[i] == '*' || buf[i] == '#') {
if (i + esc + 1 == cap) {
/* Skip characters that need escaping when we only have
* one byte of capacity remaining. */
buf[i] = '\0';
break;
} else {
esc += 1;
}
}
}
count = i;
i -= 1;
while (esc > 0) {
char c = buf[i];
if (c == '}' || c == '#' || c == '*' || c == '#') {
buf[i + esc] = c ^ 0x20;
esc -= 1;
buf[i + esc] = '}';
} else {
buf[i + esc] = c;
}
i -= 1;
}
return count;
}[1]: https://inbox.sourceware.org/gdb/[email protected]/T/#t