gdb/debuginfod: Improve progress updates

If the download size is known, a progress bar is displayed along with
the percentage of completion and the total download size.

  Downloading separate debug info for /lib/libxyz.so
  [############                                      ]  25% (10.01 M)

If the download size is not known, a progress indicator is displayed
with a ticker ("###") that moves across the screen at a rate of 1 tick
every 0.5 seconds.

  Downloading separate debug info for /lib/libxyz.so
  [         ###                                                      ]

If the output stream is not a tty, batch mode is enabled, the screen is
too narrow or width has been set to 'unlimited', then only a static
description of the download is printed. No bar or ticker is displayed.

  Downloading separate debug info for /lib/libxyz.so...

In any case, if the size of the download is known at the time the
description is printed then it will be included in the description.

  Downloading 10.01 MB separate debug info for /lib/libxyz.so...
This commit is contained in:
Aaron Merey
2022-10-24 14:05:06 -04:00
parent f71e3f86e8
commit 27859c6b9d
6 changed files with 299 additions and 165 deletions

View File

@ -24,6 +24,7 @@
#include "gdbsupport/gdb_optional.h"
#include "cli/cli-cmds.h"
#include "cli/cli-style.h"
#include "cli-out.h"
#include "target.h"
/* Set/show debuginfod commands. */
@ -87,12 +88,12 @@ debuginfod_exec_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)
{ }
const char * const desc;
const char * const fname;
bool has_printed;
ui_out::progress_update progress;
};
/* Deleter for a debuginfod_client. */
@ -108,47 +109,74 @@ struct debuginfod_client_deleter
using debuginfod_client_up
= std::unique_ptr<debuginfod_client, debuginfod_client_deleter>;
/* Convert SIZE into a unit suitable for use with progress updates.
SIZE should in given in bytes and will be converted into KB, MB, GB
or remain unchanged. UNIT will be set to "B", "KB", "MB" or "GB"
accordingly. */
static const char *
get_size_and_unit (double &size)
{
if (size < 1024)
/* If size is less than 1 KB then set unit to B. */
return "B";
size /= 1024;
if (size < 1024)
/* If size is less than 1 MB then set unit to KB. */
return "K";
size /= 1024;
if (size < 1024)
/* If size is less than 1 GB then set unit to MB. */
return "M";
size /= 1024;
return "G";
}
static int
progressfn (debuginfod_client *c, long cur, long total)
{
user_data *data = static_cast<user_data *> (debuginfod_get_user_data (c));
gdb_assert (data != nullptr);
string_file styled_fname (current_uiout->can_emit_style_escape ());
fprintf_styled (&styled_fname, file_name_style.style (), "%s",
data->fname);
if (check_quit_flag ())
{
gdb_printf ("Cancelling download of %s %ps...\n",
data->desc,
styled_string (file_name_style.style (), data->fname));
gdb_printf ("Cancelling download of %s %s...\n",
data->desc, styled_fname.c_str ());
return 1;
}
if (!data->has_printed)
if (debuginfod_verbose == 0)
return 0;
/* Print progress update. Include the transfer size if available. */
if (total > 0)
{
/* Include the transfer size, if available. */
if (total > 0)
/* Transfer size is known. */
double howmuch = (double) cur / (double) total;
if (howmuch >= 0.0 && howmuch <= 1.0)
{
float size = 1.0f * total / 1024;
const char *unit = "KB";
/* If size is greater than 0.01 MB, set unit to MB. */
if (size > 10.24)
{
size /= 1024;
unit = "MB";
}
gdb_printf ("Downloading %.2f %s %s %ps...\n",
size, unit, data->desc,
styled_string (file_name_style.style (),
data->fname));
double d_total = (double) total;
const char *unit = get_size_and_unit (d_total);
std::string msg = string_printf ("Downloading %0.2f %s %s %s",
d_total, unit, data->desc,
styled_fname.c_str ());
data->progress.update_progress (msg, unit, howmuch, d_total);
return 0;
}
else
gdb_printf ("Downloading %s %ps...\n", data->desc,
styled_string (file_name_style.style (), data->fname));
data->has_printed = true;
}
std::string msg = string_printf ("Downloading %s %s",
data->desc, styled_fname.c_str ());
data->progress.update_progress (msg);
return 0;
}
@ -230,6 +258,21 @@ debuginfod_is_enabled ()
return true;
}
/* Print the result of the most recent attempted download. */
static void
print_outcome (user_data &data, int fd)
{
/* Clears the current line of progress output. */
current_uiout->do_progress_end ();
if (fd < 0 && fd != -ENOENT)
gdb_printf (_("Download failed: %s. Continuing without %s %ps.\n"),
safe_strerror (-fd),
data.desc,
styled_string (file_name_style.style (), data.fname));
}
/* See debuginfod-support.h */
scoped_fd
@ -263,11 +306,7 @@ debuginfod_source_query (const unsigned char *build_id,
srcpath,
&dname));
debuginfod_set_user_data (c, nullptr);
if (fd.get () < 0 && fd.get () != -ENOENT)
gdb_printf (_("Download failed: %s. Continuing without source file %ps.\n"),
safe_strerror (-fd.get ()),
styled_string (file_name_style.style (), srcpath));
print_outcome (data, fd.get ());
if (fd.get () >= 0)
destname->reset (dname);
@ -305,11 +344,7 @@ debuginfod_debuginfo_query (const unsigned char *build_id,
scoped_fd fd (debuginfod_find_debuginfo (c, build_id, build_id_len,
&dname));
debuginfod_set_user_data (c, nullptr);
if (fd.get () < 0 && fd.get () != -ENOENT)
gdb_printf (_("Download failed: %s. Continuing without debug info for %ps.\n"),
safe_strerror (-fd.get ()),
styled_string (file_name_style.style (), filename));
print_outcome (data, fd.get ());
if (fd.get () >= 0)
destname->reset (dname);
@ -346,12 +381,7 @@ debuginfod_exec_query (const unsigned char *build_id,
scoped_fd fd (debuginfod_find_executable (c, build_id, build_id_len, &dname));
debuginfod_set_user_data (c, nullptr);
if (fd.get () < 0 && fd.get () != -ENOENT)
gdb_printf (_("Download failed: %s. " \
"Continuing without executable for %ps.\n"),
safe_strerror (-fd.get ()),
styled_string (file_name_style.style (), filename));
print_outcome (data, fd.get ());
if (fd.get () >= 0)
destname->reset (dname);