2020-02-27 06:40:49 +08:00
|
|
|
/* debuginfod utilities for GDB.
|
|
|
|
Copyright (C) 2020 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
This file is part of GDB.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#include "defs.h"
|
2020-02-29 21:33:35 +08:00
|
|
|
#include <errno.h>
|
2020-02-27 06:40:49 +08:00
|
|
|
#include "cli/cli-style.h"
|
|
|
|
#include "gdbsupport/scoped_fd.h"
|
|
|
|
#include "debuginfod-support.h"
|
|
|
|
|
|
|
|
#ifndef HAVE_LIBDEBUGINFOD
|
|
|
|
scoped_fd
|
|
|
|
debuginfod_source_query (const unsigned char *build_id,
|
|
|
|
int build_id_len,
|
|
|
|
const char *srcpath,
|
|
|
|
gdb::unique_xmalloc_ptr<char> *destname)
|
|
|
|
{
|
|
|
|
return scoped_fd (-ENOSYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_fd
|
|
|
|
debuginfod_debuginfo_query (const unsigned char *build_id,
|
|
|
|
int build_id_len,
|
|
|
|
const char *filename,
|
|
|
|
gdb::unique_xmalloc_ptr<char> *destname)
|
|
|
|
{
|
|
|
|
return scoped_fd (-ENOSYS);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#include <elfutils/debuginfod.h>
|
|
|
|
|
2020-08-14 05:47:05 +08:00
|
|
|
struct user_data
|
|
|
|
{
|
|
|
|
user_data (const char *desc, const char *fname)
|
|
|
|
: desc (desc), fname (fname), has_printed (false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
const char * const desc;
|
|
|
|
const char * const fname;
|
|
|
|
bool has_printed;
|
|
|
|
};
|
2020-02-27 06:40:49 +08:00
|
|
|
|
2020-09-15 10:28:42 +08:00
|
|
|
/* Deleter for a debuginfod_client. */
|
|
|
|
|
|
|
|
struct debuginfod_client_deleter
|
|
|
|
{
|
|
|
|
void operator() (debuginfod_client *c)
|
|
|
|
{
|
|
|
|
debuginfod_end (c);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using debuginfod_client_up
|
|
|
|
= std::unique_ptr<debuginfod_client, debuginfod_client_deleter>;
|
|
|
|
|
2020-02-27 06:40:49 +08:00
|
|
|
static int
|
|
|
|
progressfn (debuginfod_client *c, long cur, long total)
|
|
|
|
{
|
2020-08-14 05:47:05 +08:00
|
|
|
user_data *data = static_cast<user_data *> (debuginfod_get_user_data (c));
|
|
|
|
|
2020-02-27 06:40:49 +08:00
|
|
|
if (check_quit_flag ())
|
|
|
|
{
|
|
|
|
printf_filtered ("Cancelling download of %s %ps...\n",
|
2020-08-14 05:47:05 +08:00
|
|
|
data->desc,
|
|
|
|
styled_string (file_name_style.style (), data->fname));
|
2020-02-27 06:40:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-08-14 05:47:05 +08:00
|
|
|
if (!data->has_printed && total != 0)
|
2020-02-27 06:40:49 +08:00
|
|
|
{
|
|
|
|
/* Print this message only once. */
|
2020-08-14 05:47:05 +08:00
|
|
|
data->has_printed = true;
|
2020-02-27 06:40:49 +08:00
|
|
|
printf_filtered ("Downloading %s %ps...\n",
|
2020-08-14 05:47:05 +08:00
|
|
|
data->desc,
|
|
|
|
styled_string (file_name_style.style (), data->fname));
|
2020-02-27 06:40:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-15 10:28:42 +08:00
|
|
|
static debuginfod_client_up
|
2020-02-27 06:40:49 +08:00
|
|
|
debuginfod_init ()
|
|
|
|
{
|
2020-09-15 10:28:42 +08:00
|
|
|
debuginfod_client_up c (debuginfod_begin ());
|
2020-02-27 06:40:49 +08:00
|
|
|
|
|
|
|
if (c != nullptr)
|
2020-09-15 10:28:42 +08:00
|
|
|
debuginfod_set_progressfn (c.get (), progressfn);
|
2020-02-27 06:40:49 +08:00
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See debuginfod-support.h */
|
|
|
|
|
|
|
|
scoped_fd
|
|
|
|
debuginfod_source_query (const unsigned char *build_id,
|
|
|
|
int build_id_len,
|
|
|
|
const char *srcpath,
|
|
|
|
gdb::unique_xmalloc_ptr<char> *destname)
|
|
|
|
{
|
|
|
|
if (getenv (DEBUGINFOD_URLS_ENV_VAR) == NULL)
|
|
|
|
return scoped_fd (-ENOSYS);
|
|
|
|
|
2020-09-15 10:28:42 +08:00
|
|
|
debuginfod_client_up c = debuginfod_init ();
|
2020-02-27 06:40:49 +08:00
|
|
|
|
|
|
|
if (c == nullptr)
|
|
|
|
return scoped_fd (-ENOMEM);
|
|
|
|
|
2020-08-14 05:47:05 +08:00
|
|
|
user_data data ("source file", srcpath);
|
2020-02-27 06:40:49 +08:00
|
|
|
|
2020-09-15 10:28:42 +08:00
|
|
|
debuginfod_set_user_data (c.get (), &data);
|
|
|
|
scoped_fd fd (debuginfod_find_source (c.get (),
|
2020-02-27 06:40:49 +08:00
|
|
|
build_id,
|
|
|
|
build_id_len,
|
|
|
|
srcpath,
|
|
|
|
nullptr));
|
|
|
|
|
|
|
|
/* TODO: Add 'set debug debuginfod' command to control when error messages are shown. */
|
|
|
|
if (fd.get () < 0 && fd.get () != -ENOENT)
|
|
|
|
printf_filtered (_("Download failed: %s. Continuing without source file %ps.\n"),
|
|
|
|
safe_strerror (-fd.get ()),
|
|
|
|
styled_string (file_name_style.style (), srcpath));
|
|
|
|
else
|
2020-09-15 10:28:25 +08:00
|
|
|
*destname = make_unique_xstrdup (srcpath);
|
2020-02-27 06:40:49 +08:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See debuginfod-support.h */
|
|
|
|
|
|
|
|
scoped_fd
|
|
|
|
debuginfod_debuginfo_query (const unsigned char *build_id,
|
|
|
|
int build_id_len,
|
|
|
|
const char *filename,
|
|
|
|
gdb::unique_xmalloc_ptr<char> *destname)
|
|
|
|
{
|
|
|
|
if (getenv (DEBUGINFOD_URLS_ENV_VAR) == NULL)
|
|
|
|
return scoped_fd (-ENOSYS);
|
|
|
|
|
2020-09-15 10:28:42 +08:00
|
|
|
debuginfod_client_up c = debuginfod_init ();
|
2020-02-27 06:40:49 +08:00
|
|
|
|
|
|
|
if (c == nullptr)
|
|
|
|
return scoped_fd (-ENOMEM);
|
|
|
|
|
|
|
|
char *dname = nullptr;
|
2020-08-14 05:47:05 +08:00
|
|
|
user_data data ("separate debug info for", filename);
|
2020-02-27 06:40:49 +08:00
|
|
|
|
2020-09-15 10:28:42 +08:00
|
|
|
debuginfod_set_user_data (c.get (), &data);
|
|
|
|
scoped_fd fd (debuginfod_find_debuginfo (c.get (), build_id, build_id_len,
|
|
|
|
&dname));
|
2020-02-27 06:40:49 +08:00
|
|
|
|
|
|
|
if (fd.get () < 0 && fd.get () != -ENOENT)
|
|
|
|
printf_filtered (_("Download failed: %s. Continuing without debug info for %ps.\n"),
|
|
|
|
safe_strerror (-fd.get ()),
|
|
|
|
styled_string (file_name_style.style (), filename));
|
|
|
|
|
|
|
|
destname->reset (dname);
|
2020-09-15 10:28:42 +08:00
|
|
|
|
2020-02-27 06:40:49 +08:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
#endif
|