summaryrefslogtreecommitdiff
path: root/gdb/debuginfod-support.c
diff options
context:
space:
mode:
authorAaron Merey <amerey@redhat.com>2022-10-24 14:05:06 -0400
committerAaron Merey <amerey@redhat.com>2022-11-10 12:01:18 -0500
commit27859c6b9d73a8ae1b51c5c40fc2b3aefd2228a0 (patch)
treeae917c80fce78e048cbbd357f68ac2e9d2a61434 /gdb/debuginfod-support.c
parentf71e3f86e83c9c22e3d86112b5ddb61919390a1a (diff)
downloadbinutils-gdb-27859c6b9d73a8ae1b51c5c40fc2b3aefd2228a0.tar.gz
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...
Diffstat (limited to 'gdb/debuginfod-support.c')
-rw-r--r--gdb/debuginfod-support.c116
1 files changed, 73 insertions, 43 deletions
diff --git a/gdb/debuginfod-support.c b/gdb/debuginfod-support.c
index 5f04a2b38ca..f1249203da0 100644
--- a/gdb/debuginfod-support.c
+++ b/gdb/debuginfod-support.c
@@ -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);