linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephen Brennan <stephen.s.brennan@oracle.com>
To: Amal Raj T <tjarlama@gmail.com>,
	danielt@kernel.org, dianders@chromium.org,
	jason.wessel@windriver.com
Cc: tjarlama@gmail.com, amalrajt@meta.com, osandov@osandov.com,
	linux-debuggers@vger.kernel.org, linux-serial@vger.kernel.org,
	kgdb-bugreport@lists.sourceforge.net
Subject: Re: [PATCH v3 3/3] kgdb: Add command linux.vmcoreinfo to kgdb
Date: Tue, 14 Jan 2025 13:18:07 -0800	[thread overview]
Message-ID: <87v7uhrt68.fsf@oracle.com> (raw)
In-Reply-To: <87bjwati2e.fsf@oracle.com>

Stephen Brennan <stephen.s.brennan@oracle.com> writes:

> Amal Raj T <tjarlama@gmail.com> writes:
>
>> From: Amal Raj T <amalrajt@meta.com>
>>
>> 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).
>>
>> Link: https://github.com/osandov/drgn/wiki/GDB-Remote-Protocol-proposal:-linux.vmcoreinfo-query-packet
>> ---
>>  kernel/debug/gdbstub.c | 18 ++++++++++++++----
>>  lib/Kconfig.kgdb       |  1 +
>>  2 files changed, 15 insertions(+), 4 deletions(-)
>>
>> diff --git a/kernel/debug/gdbstub.c b/kernel/debug/gdbstub.c
>> index f88e21d5502a..f2c80bd368e2 100644
>> --- a/kernel/debug/gdbstub.c
>> +++ b/kernel/debug/gdbstub.c
>> @@ -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)];
>
> 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.

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/87y0zds39y.fsf@oracle.com/T/#t

      reply	other threads:[~2025-01-14 21:18 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <gmail>
2024-12-10 13:34 ` [PATCH 0/3] Add a new command in kgdb for vmcoreinfo Amal Raj T
2024-12-10 13:34   ` [PATCH 1/3] kgdb: Add kgdb_mem2ebin function for converting memory to binary format Amal Raj T
2024-12-10 13:34   ` [PATCH 2/3] serial: Move LF -> CRLF replacement from serial console to kdb Amal Raj T
2024-12-10 15:16     ` Daniel Thompson
2024-12-11 15:40       ` Amal
2024-12-10 13:34   ` [PATCH 3/3] kgdb: Add command linux.vmcoreinfo to kgdb Amal Raj T
2024-12-11 15:39 ` [PATCH v2 0/3] Add a new command in kgdb for vmcoreinfo Amal Raj T
2024-12-11 15:39   ` [PATCH v2 1/3] kgdb: Add kgdb_mem2ebin function for converting memory to binary format Amal Raj T
2024-12-13  0:55     ` Doug Anderson
     [not found]       ` <CAOfKSRMBYp6dSbhRqQXm09QUoJTaLjQr0XFqzqGVGeJ-KKoMuQ@mail.gmail.com>
2025-01-24 23:17         ` Doug Anderson
2025-01-08 11:40     ` Daniel Thompson
2024-12-11 15:39   ` [PATCH v2 2/3] serial: Move LF -> CRLF replacement from serial console to kdb Amal Raj T
2024-12-13  0:55     ` Doug Anderson
2024-12-11 15:39   ` [PATCH v2 3/3] kgdb: Add command linux.vmcoreinfo to kgdb Amal Raj T
2024-12-13  0:56     ` Doug Anderson
2025-01-13 17:29 ` [PATCH v3 0/3] Add a new command in kgdb for vmcoreinfo Amal Raj T
2025-01-13 17:29   ` [PATCH v3 1/3] kgdb: Add kgdb_mem2ebin function for converting memory to binary format Amal Raj T
2025-01-13 20:38     ` Stephen Brennan
2025-01-13 17:29   ` [PATCH v3 2/3] kgdb: Add command linux.vmcoreinfo to kgdb Amal Raj T
2025-01-13 23:24     ` Stephen Brennan
2025-01-13 17:29   ` [PATCH v3 2/3] serial: Move LF -> CRLF replacement from serial console to kdb Amal Raj T
2025-01-13 21:29     ` Stephen Brennan
2025-01-24 22:55     ` Doug Anderson
2025-01-13 17:29   ` [PATCH v3 3/3] kgdb: Add command linux.vmcoreinfo to kgdb Amal Raj T
2025-01-13 23:22     ` Stephen Brennan
2025-01-14 21:18       ` Stephen Brennan [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87v7uhrt68.fsf@oracle.com \
    --to=stephen.s.brennan@oracle.com \
    --cc=amalrajt@meta.com \
    --cc=danielt@kernel.org \
    --cc=dianders@chromium.org \
    --cc=jason.wessel@windriver.com \
    --cc=kgdb-bugreport@lists.sourceforge.net \
    --cc=linux-debuggers@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=osandov@osandov.com \
    --cc=tjarlama@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).