Re: [PATCH] gdb: print size of downloaded debuginfod binary

Tom de Vries
2020-12-09T23:26:20Z

[-- Attachment #1: Type: text/plain, Size: 1502 bytes --]

On 12/9/20 11:51 AM, Martin Liška wrote:
> On 12/8/20 3:28 PM, Simon Marchi wrote:
>> On 2020-12-08 5:16 a.m., Martin Liška wrote:
>> Temporary breakpoint 1 at 0x406150: file
>>> /home/marxin/Programming/gcc/gcc/gcc-main.c, line 44.
>>> Starting program: /home/marxin/bin/gcc/bin/gcc
>>> Downloading 1.00 MB separate debug info for
>>> /lib64/ld-linux-x86-64.so.2...10%..20%..30%..40%..50%..60%..70%..80%..90%..100%
>>>
>>> Downloading 4.20 MB separate debug info for
>>> /lib64/libm.so.6...10%..20%..30%..40%..50%..60%..70%..80%..90%..100%
>>> Downloading 10.72 MB separate debug info for
>>> /lib64/libc.so.6...10%..20%..30%..40%..50%
>>>
>>> Thoughts?
>>
>> Could you make it so the percentage always gets written at the same
>> place, overwriting the  previous percentage written?
>
> Sure, implemented in the attached patch.
>

Hi,

I played around with this patch on openSUSE Tumbleweed and found two
problems:
- '\r' is not handled in fputs_maybe_filtered, so we run into
the pagination prompt after having printed just one downloading line
- when printing a downloading line longer than chars_per_line,
it will wrap, so the '\r' does not reset back to the actual start of
the line. The effect you see is that the download line is printed
over and over again, flooding the screen.

This updated patch fixes both problems, the latter by printing the %
progress on its own line (which is cleared by a final '\r' after
reaching 100%, in order not to waste lines on this).

Thanks,
- Tom

[-- Attachment #2: 0010-gdb-Print-percent-progress-for-debuginfod.patch --]
[-- Type: text/x-patch, Size: 3051 bytes --]

gdb: Print percent progress for debuginfod

Prints progress like:

Downloading 4.89 MB separate debug info for /usr/lib64/libgcrypt.so.20.
Downloading 1.10 MB separate debug info for /usr/lib64/liblzma.so.5.
Downloading 1.31 MB separate debug info for /usr/lib64/liblz4.so.1.
Downloading 0.96 MB separate debug info for /usr/lib64/libsmime3.so.
36%

ChangeLog:

2020-12-10 Martin Liska <mliska@suse.cz>
Tom de Vries <tdevries@suse.de>

* gdb/debuginfod-support.c (struct user_data): Add
last_percent_printed and first_line_printed field.
(progressfn): Print progress.
* utils.c (fputs_maybe_filtered): Handle '\r'.

---
gdb/debuginfod-support.c | 36 ++++++++++++++++++++++++++++++------
gdb/utils.c | 6 ++++++
2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/gdb/debuginfod-support.c b/gdb/debuginfod-support.c
index e21b2f40ca..3d74b2d9c8 100644
--- a/gdb/debuginfod-support.c
+++ b/gdb/debuginfod-support.c
@@ -46,12 +46,14 @@ debuginfod_debuginfo_query (const unsigned char *build_id,
struct user_data
{
user_data (const char *desc, const char *fname)
- : desc (desc), fname (fname), has_printed (false)
+ : desc (desc), fname (fname), last_percent_printed (-1),
+ first_line_printed (false)
{ }

const char * const desc;
const char * const fname;
- bool has_printed;
+ long last_percent_printed;
+ bool first_line_printed;
};

/* Deleter for a debuginfod_client. */
@@ -80,14 +82,36 @@ progressfn (debuginfod_client *c, long cur, long total)
return 1;
}

- if (!data->has_printed && total != 0)
+ if (total == 0)
+ return 0;
+
+ if (!data->first_line_printed)
{
- /* Print this message only once. */
- data->has_printed = true;
- printf_filtered ("Downloading %s %ps...\n",
+ printf_filtered ("Downloading %.2f MB %s %ps.\n",
+ 1.0f * total / (1024 * 1024),
data->desc,
styled_string (file_name_style.style (), data->fname));
+ gdb_flush (gdb_stdout);
+ data->first_line_printed = true;
+ }
+
+ if (cur == total)
+ {
+ printf_filtered ("\r");
+ gdb_flush (gdb_stdout);
}
+ else if (cur > 0)
+ {
+ /* Print this message only once. */
+ long percent = 100 * cur / total;
+ if (percent != data->last_percent_printed)
+ {
+ data->last_percent_printed = percent;
+ printf_filtered ("\r%3ld%%", percent);
+ gdb_flush (gdb_stdout);
+ }
+ }
+

return 0;
}
diff --git a/gdb/utils.c b/gdb/utils.c
index 3226656e2c..26c671a44c 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -1769,6 +1769,12 @@ fputs_maybe_filtered (const char *linebuffer, struct ui_file *stream,
don't increment chars_printed here. */
lineptr += skip_bytes;
}
+ else if (*lineptr == '\r')
+ {
+ wrap_buffer.push_back ('\r');
+ chars_printed = 0;
+ lineptr++;
+ }
else
{
wrap_buffer.push_back (*lineptr);