Commit | Line | Data |
---|---|---|
0d79cdc4 AM |
1 | /* debuginfod utilities for GDB. |
2 | Copyright (C) 2020 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
18 | ||
0d79cdc4 | 19 | #include "defs.h" |
a84bb2a0 | 20 | #include <errno.h> |
0d79cdc4 AM |
21 | #include "cli/cli-style.h" |
22 | #include "gdbsupport/scoped_fd.h" | |
23 | #include "debuginfod-support.h" | |
24 | ||
25 | #ifndef HAVE_LIBDEBUGINFOD | |
26 | scoped_fd | |
27 | debuginfod_source_query (const unsigned char *build_id, | |
28 | int build_id_len, | |
29 | const char *srcpath, | |
30 | gdb::unique_xmalloc_ptr<char> *destname) | |
31 | { | |
32 | return scoped_fd (-ENOSYS); | |
33 | } | |
34 | ||
35 | scoped_fd | |
36 | debuginfod_debuginfo_query (const unsigned char *build_id, | |
37 | int build_id_len, | |
38 | const char *filename, | |
39 | gdb::unique_xmalloc_ptr<char> *destname) | |
40 | { | |
41 | return scoped_fd (-ENOSYS); | |
42 | } | |
43 | #else | |
44 | #include <elfutils/debuginfod.h> | |
45 | ||
002a3166 AM |
46 | struct user_data |
47 | { | |
48 | user_data (const char *desc, const char *fname) | |
49 | : desc (desc), fname (fname), has_printed (false) | |
50 | { } | |
51 | ||
52 | const char * const desc; | |
53 | const char * const fname; | |
54 | bool has_printed; | |
55 | }; | |
0d79cdc4 | 56 | |
d2b31b67 SM |
57 | /* Deleter for a debuginfod_client. */ |
58 | ||
59 | struct debuginfod_client_deleter | |
60 | { | |
61 | void operator() (debuginfod_client *c) | |
62 | { | |
63 | debuginfod_end (c); | |
64 | } | |
65 | }; | |
66 | ||
67 | using debuginfod_client_up | |
68 | = std::unique_ptr<debuginfod_client, debuginfod_client_deleter>; | |
69 | ||
0d79cdc4 AM |
70 | static int |
71 | progressfn (debuginfod_client *c, long cur, long total) | |
72 | { | |
002a3166 AM |
73 | user_data *data = static_cast<user_data *> (debuginfod_get_user_data (c)); |
74 | ||
0d79cdc4 AM |
75 | if (check_quit_flag ()) |
76 | { | |
77 | printf_filtered ("Cancelling download of %s %ps...\n", | |
002a3166 AM |
78 | data->desc, |
79 | styled_string (file_name_style.style (), data->fname)); | |
0d79cdc4 AM |
80 | return 1; |
81 | } | |
82 | ||
002a3166 | 83 | if (!data->has_printed && total != 0) |
0d79cdc4 AM |
84 | { |
85 | /* Print this message only once. */ | |
002a3166 | 86 | data->has_printed = true; |
0d79cdc4 | 87 | printf_filtered ("Downloading %s %ps...\n", |
002a3166 AM |
88 | data->desc, |
89 | styled_string (file_name_style.style (), data->fname)); | |
0d79cdc4 AM |
90 | } |
91 | ||
92 | return 0; | |
93 | } | |
94 | ||
d2b31b67 | 95 | static debuginfod_client_up |
0d79cdc4 AM |
96 | debuginfod_init () |
97 | { | |
d2b31b67 | 98 | debuginfod_client_up c (debuginfod_begin ()); |
0d79cdc4 AM |
99 | |
100 | if (c != nullptr) | |
d2b31b67 | 101 | debuginfod_set_progressfn (c.get (), progressfn); |
0d79cdc4 AM |
102 | |
103 | return c; | |
104 | } | |
105 | ||
106 | /* See debuginfod-support.h */ | |
107 | ||
108 | scoped_fd | |
109 | debuginfod_source_query (const unsigned char *build_id, | |
110 | int build_id_len, | |
111 | const char *srcpath, | |
112 | gdb::unique_xmalloc_ptr<char> *destname) | |
113 | { | |
114 | if (getenv (DEBUGINFOD_URLS_ENV_VAR) == NULL) | |
115 | return scoped_fd (-ENOSYS); | |
116 | ||
d2b31b67 | 117 | debuginfod_client_up c = debuginfod_init (); |
0d79cdc4 AM |
118 | |
119 | if (c == nullptr) | |
120 | return scoped_fd (-ENOMEM); | |
121 | ||
002a3166 | 122 | user_data data ("source file", srcpath); |
0d79cdc4 | 123 | |
d2b31b67 SM |
124 | debuginfod_set_user_data (c.get (), &data); |
125 | scoped_fd fd (debuginfod_find_source (c.get (), | |
0d79cdc4 AM |
126 | build_id, |
127 | build_id_len, | |
128 | srcpath, | |
129 | nullptr)); | |
130 | ||
131 | /* TODO: Add 'set debug debuginfod' command to control when error messages are shown. */ | |
132 | if (fd.get () < 0 && fd.get () != -ENOENT) | |
133 | printf_filtered (_("Download failed: %s. Continuing without source file %ps.\n"), | |
134 | safe_strerror (-fd.get ()), | |
135 | styled_string (file_name_style.style (), srcpath)); | |
136 | else | |
3246bd8e | 137 | *destname = make_unique_xstrdup (srcpath); |
0d79cdc4 | 138 | |
0d79cdc4 AM |
139 | return fd; |
140 | } | |
141 | ||
142 | /* See debuginfod-support.h */ | |
143 | ||
144 | scoped_fd | |
145 | debuginfod_debuginfo_query (const unsigned char *build_id, | |
146 | int build_id_len, | |
147 | const char *filename, | |
148 | gdb::unique_xmalloc_ptr<char> *destname) | |
149 | { | |
150 | if (getenv (DEBUGINFOD_URLS_ENV_VAR) == NULL) | |
151 | return scoped_fd (-ENOSYS); | |
152 | ||
d2b31b67 | 153 | debuginfod_client_up c = debuginfod_init (); |
0d79cdc4 AM |
154 | |
155 | if (c == nullptr) | |
156 | return scoped_fd (-ENOMEM); | |
157 | ||
0d79cdc4 | 158 | char *dname = nullptr; |
002a3166 | 159 | user_data data ("separate debug info for", filename); |
0d79cdc4 | 160 | |
d2b31b67 SM |
161 | debuginfod_set_user_data (c.get (), &data); |
162 | scoped_fd fd (debuginfod_find_debuginfo (c.get (), build_id, build_id_len, | |
163 | &dname)); | |
0d79cdc4 AM |
164 | |
165 | if (fd.get () < 0 && fd.get () != -ENOENT) | |
166 | printf_filtered (_("Download failed: %s. Continuing without debug info for %ps.\n"), | |
167 | safe_strerror (-fd.get ()), | |
168 | styled_string (file_name_style.style (), filename)); | |
169 | ||
170 | destname->reset (dname); | |
d2b31b67 | 171 | |
0d79cdc4 AM |
172 | return fd; |
173 | } | |
174 | #endif |