Debian Codegraph @

find-dbgsym-packages

raw
1 #!/usr/bin/perl
2
3 # Get list of debug symbol packages relevant for a core file or ELF
4 # program/library.
5 #
6 # Copyright (C) 2017 Stefan Fritsch <[email protected]>
7 # Copyright (C) 2017 Paul Wise <[email protected]>
8 # Copyright (C) 2017-2020 Axel Beckert <[email protected]>
9 # Copyright (C) 2018 Jakub Wilk <[email protected]>
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 use strict;
26 use warnings FATAL => 'all';
27 use autodie qw(:all);
28 use v5.14;
29 use IPC::System::Simple qw(capturex $EXITVAL);
30 use File::Which;
31 use File::Temp qw(tempdir);
32 use File::Slurper qw(read_text write_text);
33 use POSIX qw(strftime);
34 use File::Copy qw(move);
35 use File::Basename qw(fileparse);
36
37 $ENV{LC_ALL} = 'C';
38
39 if (scalar @ARGV == 0 or $ARGV[0] eq '--help' or $ARGV[0] eq '-h') {
40 usage();
41 }
42
43 my $vdso_regexp = qr/^linux-(gate|vdso\d*)[.]so[.]/;
44 my $call_apt = 0;
45 my $use_ssh = 0;
46 my $show_all = 0;
47 my $gen_deb = 0;
48
49 my %pkgs;
50 my @programs = ();
51 my @out_of_date_files;
52 foreach my $arg (@ARGV) {
53 if ($arg eq '--install') {
54 $call_apt = 1;
55 next;
56 }
57 if ($arg eq '--deb') {
58 die "Package equivs needs to be installed to use the option '--deb'.\n"
59 unless which('equivs-build');
60 $gen_deb = 1;
61 next;
62 }
63 if ($arg eq '--ssh') {
64 die "Package openssh-client needs to be installed to use the option '--ssh'.\n"
65 unless which('ssh');
66 $use_ssh = 1;
67 next;
68 }
69 if ($arg eq '--all') {
70 $show_all = 1;
71 next;
72 }
73
74 my %build_ids;
75 if ($arg =~ /^\d+$/) {
76 %build_ids = get_build_ids_from_pid($arg);
77
78 # Document which file was looked for
79 push(@programs, readlink("/proc/$arg/exe"));
80 } else {
81 %build_ids = get_build_ids_from_file($arg);
82 }
83
84 my %debs_from_id = get_debs_from_ids(keys %build_ids);
85 foreach my $id (keys %build_ids) {
86 my ($path, $name) = @{$build_ids{$id}};
87
88 next if $name =~ $vdso_regexp;
89
90 my @p = @{$debs_from_id{$id} // []};
91 if (scalar @p == 0) {
92 @p = get_debs_from_path($path);
93 if (scalar @p == 0) {
94 warn "W: Cannot find debug package for $name ($id)\n";
95 }
96 } elsif (scalar @p > 1) {
97 warn "W: Multiple packages for $name ($id): @p\n";
98 }
99 foreach my $p (@p) {
100 $pkgs{$p} = 1;
101 }
102 }
103 }
104
105 if (@out_of_date_files) {
106 warn
107 "W: The following files were reported by eu-unstrip as \"deleted\":\n".
108 "W: ".join("\nW: ", @out_of_date_files)."\n".
109 "W: If $0 reports already installed dbgsym packages as necessary,\n".
110 "W: they are not at the expected (usually older) package version.\n";
111 }
112
113 my @pkgs = sort keys %pkgs;
114
115 # Is anything to do anyway?
116 if (@pkgs) {
117
118 # Shall we install the needed packages directly?
119 if ($call_apt) {
120 my @cmd = (qw(apt install --no-install-recommends), @pkgs);
121
122 # Shall we generate a metapackage and install that one?
123 if ($gen_deb) {
124 my $pkg = gen_pkg(@pkgs);
125 @cmd = (qw(apt install), $pkg);
126 }
127
128 # Are we root?
129 unless ($> == 0) {
130
131 # Shall we use ssh to obtain root privileges?
132 if ($use_ssh) {
133 @cmd = (qw(ssh root@localhost -t), @cmd);
134 } else {
135
136 # Is sudo installed? If so, use it.
137 if (which('sudo')) {
138 unshift(@cmd, 'sudo');
139
140 # Else use plain old su.
141 } else {
142 @cmd = (qw(su - -c), join(' ', @cmd));
143 }
144 }
145 }
146
147 # Print the command that is being executed so
148 # that it is clear what password is needed
149 say("\$ @cmd");
150
151 # Finally execute the command constructed above
152 exec(@cmd);
153
154 # Shall we just generate a .deb, but not install it?
155 } elsif ($gen_deb) {
156 my $pkg = gen_pkg(@pkgs);
157 my $target = $ENV{TMPDIR} // '/tmp';
158 move($pkg, $target);
159 my $filename = $target.'/'.fileparse($pkg);
160 say "Metapackage has been generated at $filename.";
161 say "Install it like this with root privileges:";
162 say "";
163 say "apt install $filename";
164
165 # No options, so just display the list of still needed packages.
166 } else {
167 say join(" ", @pkgs);
168 }
169
170 # If not, tell the user on STDERR.
171 } else {
172 warn "I: All needed dbgsym packages are already installed.\n";
173 }
174
175 exit 0;
176
177 #### sub routines ####
178
179 sub parse_eu_unstrip
180 {
181 my ($output) = @_;
182
183 my %ids;
184
185 foreach my $line (split(/\n/, $output)) {
186 # 0x7fa9b8017000+0x39e9a0 79450f6e36287865d093ea209b85a222209925ff@0x7fa9b8017280 /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/debug/.build-id/79/450f6e36287865d093ea209b85a222209925ff.debug libc.so.6
187 # 0x7f7f7235e000+0x17000 - /usr/share/locale/de/LC_MESSAGES/bash.mo - /usr/share/locale/de/LC_MESSAGES/bash.mo
188 # 0x7ffd4098a000+0x2000 de7dac2df9f596f46fa94a387858ef25170603ec@0x7ffd4098a7d0 . - [vdso: 1740]
189 # 0x7f37090fb000+0x2a000 dc5cb16f5e644116cac64a4c3f5da4d081b81a4f@0x7f37090fb248 - - /lib/x86_64-linux-gnu/ld-2.27.so (deleted)
190 # 0x562f3d01b000+0xa725000 9b43003ffd70d8db@0x562f3d01b34c /usr/lib/chromium/chromium /usr/lib/debug/.build-id/9b/43003ffd70d8db.debug /usr/lib/chromium/chromium
191 if ($line =~ m{
192 ^(?: 0 | 0x[[:xdigit:]]+ )
193 [+]
194 0x[[:xdigit:]]+
195 \s+
196 ( [[:xdigit:]]+ [@] 0x[[:xdigit:]]+ | - )
197 \s+
198 ( \S+ )
199 \s+
200 ( \S+ )
201 \s+
202 (?: ( \S+ ) | ( \[vdso: \s+ \d+\] ) )?
203 ( \s+ \(deleted\) )?
204 $
205 }ix) {
206 my $id = $1;
207 my $path = $2;
208 my $debug = $3;
209 my $name = $4 // $path;
210 my $vdso = $5;
211 my $deleted = $6;
212 if ($debug ne '-' and not $show_all) {
213 next;
214 }
215 if (defined $vdso) {
216 next;
217 }
218 if ($id eq '-') {
219 warn "W: No build-ID for $name\n";
220 next;
221 } elsif ($id =~ /^([[:xdigit:]]+)[@]/) {
222 $id = $1;
223 } else {
224 die "BUG: id='$id'";
225 }
226 if ($path eq '-' || $path eq '.') {
227 $path = $name;
228 $path =~ s{ \(deleted\)$}{};
229 }
230 if (defined $deleted) {
231 push(@out_of_date_files, $path);
232 }
233 $ids{$id} = [$path, $name];
234 } else {
235 warn "W: Cannot parse eu-unstrip output: '$line'\n";
236 }
237 }
238 return (%ids);
239 }
240
241 sub get_files_from_elf
242 {
243 my ($filename) = @_;
244 my @libs = ($filename);
245 my $output = capturex(qw(ldd --), $filename);
246
247 foreach my $line (split(/\n/, $output)) {
248 chomp $line;
249 my ($name, $path);
250 if ($line =~ /^\t.+ => (.+) \(0x[0-9a-f]+\)$/) {
251 push @libs, $1;
252 } elsif ($line =~ /^\t(.+) \(0x[0-9a-f]+\)$/) {
253 push @libs, $1;
254 } else {
255 warn "W: Cannot parse ldd output: '$line'\n";
256 }
257 }
258
259 return @libs;
260 }
261
262 sub get_build_ids_from_file
263 {
264 my ($filename) = @_;
265 if ($filename !~ m(/) and not -f $filename) {
266 my $oldfilename = $filename;
267 $filename = which($filename);
268 if (defined($filename)) {
269 warn "I: ./$oldfilename not found, using $filename instead\n";
270 } else {
271 warn "W: ./$oldfilename not found ".
272 "and no '$oldfilename' found in path either, skipping\n";
273 return qw();
274 }
275 }
276
277 unless (-f $filename) {
278 warn "W: $filename not found, skipping\n";
279 return qw();
280 }
281
282 if (is_core_file($filename)) {
283 return get_build_ids_from_core($filename);
284 } else {
285 # Document which file was looked for
286 push(@programs, $filename);
287
288 my @filenames = get_files_from_elf($filename);
289 my %build_ids;
290 foreach my $filename (@filenames) {
291 next if $filename =~ $vdso_regexp;
292 %build_ids = (%build_ids, get_build_ids_from_elf($filename));
293 }
294 return %build_ids;
295 }
296 }
297
298 sub get_build_ids_from_elf
299 {
300 my ($filename) = @_;
301 my $output = capturex(qw(eu-unstrip --list-only --executable), $filename);
302
303 return parse_eu_unstrip($output);
304 }
305
306 sub get_build_ids_from_core
307 {
308 my ($filename) = @_;
309 my $output = capturex(qw(eu-unstrip --list-only --core), $filename);
310
311 # Document which file was looked for (fifth field in first line)
312 my $program = (split(' ', (split(/\n/, $output))[0]))[4];
313 push(@programs, $program);
314
315 return parse_eu_unstrip($output);
316 }
317
318 sub get_build_ids_from_pid
319 {
320 my ($pid) = @_;
321 my $output = capturex(qw(eu-unstrip --list-only --pid), $pid);
322 chomp $output;
323
324 return parse_eu_unstrip($output);
325 }
326
327 sub get_debs_from_ids
328 {
329 my $id_regexp = join('|', @_);
330 my %map;
331 my $output;
332 $output = capturex([0, 1], qw(grep-aptavail --no-field-names --show-field Package --show-field Build-IDs --field Build-IDs --eregex --pattern), $id_regexp);
333 while ($output =~ /\G(\S+)\n(\S+(?: \S+)*)\n\n/gc) {
334 my $pkg = $1;
335 my $ids = $2;
336 while ($ids =~ m/\b($id_regexp)\b/g) {
337 push @{$map{$1}}, $pkg;
338 }
339 }
340 if (length $output != (pos $output // 0)) {
341 die "Cannot parse grep-aptavail output";
342 }
343 return %map;
344 }
345
346 sub get_debs_from_path
347 {
348 my ($path) = @_;
349
350 my $output;
351 eval {
352 ($output, undef) = capturex(qw(dpkg-query --search --), $path);
353 };
354 if ($@) {
355 return;
356 }
357
358 my %pkgs = ();
359 foreach my $line (split(/\n/, $output)) {
360 if ($line =~ /^(.*): /) {
361 $pkgs{$1} = 1;
362 } else {
363 warn "W: Cannot parse dpkg-query output: '$line'\n";
364 }
365 }
366 my @pkgs = sort keys %pkgs;
367 my @strip_pkgs = map { s{:.*}{}; s{\d.*$}{}r } @pkgs;
368
369 eval {
370 ($output, undef) = capturex(qw(dpkg-query --showformat ${source:Package}\n --show --), @pkgs);
371 };
372 if ($@) {
373 return;
374 }
375
376 my %dbg_pkgs = ();
377 foreach my $src_pkg (split(/\n/, $output)) {
378 my $output;
379 $output = capturex([0, 1], qw(grep-aptavail --no-field-names --show-field Package --field Package --pattern -dbg --and --whole-pkg --field Source:Package --pattern), $src_pkg);
380 if ($EXITVAL) {
381 warn "W: No dbg package for source '$src_pkg'\n";
382 next;
383 }
384 my %src_dbg_pkgs = map { $_ => 1 } split(/\n/, $output);
385 my @src_dbg_pkgs = keys %src_dbg_pkgs;
386 my @src_strip_pkgs = map { my $pkg = $_; grep { /^$pkg-dbg/ } @src_dbg_pkgs } @pkgs;
387 @src_strip_pkgs = map { my $pkg = $_; grep { /^$pkg.*-dbg/ } @src_dbg_pkgs } @pkgs unless @src_strip_pkgs;
388 @src_strip_pkgs = map { my $pkg = $_; grep { /^$pkg-dbg/ } @src_dbg_pkgs } @strip_pkgs unless @src_strip_pkgs;
389 @src_strip_pkgs = map { my $pkg = $_; grep { /^$pkg.*-dbg/ } @src_dbg_pkgs } @strip_pkgs unless @src_strip_pkgs;
390 @src_dbg_pkgs = @src_strip_pkgs if @src_strip_pkgs;
391 map { $dbg_pkgs{$_} = 1; } @src_dbg_pkgs;
392 };
393
394 return sort keys %dbg_pkgs;
395 }
396
397 sub is_core_file
398 {
399 my ($filename) = (@_);
400 my $output = capturex(qw(eu-readelf --file-header --), $filename);
401 if ($output =~ /^\s*Type:\s*CORE/m) {
402 return 1;
403 }
404 return;
405 }
406
407 sub gen_pkg {
408 my @pkgs = @_;
409 my $tempdir = tempdir( ( $ENV{TMPDIR} ? $ENV{TMPDIR} : '/tmp' ).
410 '/find-dbgsym-packages-equivs_XXXXX',
411 CLEANUP => 1 );
412
413 # Generating the necessary strings for generating the metapackage
414 my %replacements = ();
415
416 $replacements{CALL} = "$0 ".join(' ', @ARGV);
417 $replacements{DATE} = strftime('%a, %d %b %Y %T %z', localtime());
418 $replacements{DEPENDS} = join(', ', @pkgs);
419 $replacements{USER} = getlogin();
420 $replacements{HOST} = `hostname`;
421 $replacements{AOM} = $show_all ? 'all' : 'currently missing';
422
423 chomp($replacements{USER});
424 chomp($replacements{HOST});
425
426 my $dg_version = `dpkg-query -f '\${Version}' -W debian-goodies`;
427 $replacements{DGVERSION} = $dg_version;
428
429 my @now = localtime;
430 my $pkg_version = strftime(
431 '%Y.%m.%dT%H.%M.%S+dg'.$dg_version,
432 localtime()
433 );
434 $replacements{PKGVERSION} = $pkg_version;
435
436 my %programs = map { $_ => 1 } @programs;
437 $replacements{PROGRAMS} = ' * ' .
438 join("\n * ", sort keys %programs);
439
440 $replacements{PACKAGE} = 'dbgsym-pkgs-for-' .
441 join('-',
442 map {
443 s(^.*/)();
444 s/\W//g;
445 s/[-_]//g;
446 lc($_);
447 }
448 sort keys %programs
449 );
450
451 # Replace placeholders with actual data
452 foreach my $file (qw(changelog control readme)) {
453 gen_file_from_template(
454 ( -d './find-dbgsym-packages-templates/' ?
455 './find-dbgsym-packages-templates/' :
456 '/usr/share/debian-goodies/find-dbgsym-packages-templates/' )
457 . $file,
458 "$tempdir/$file",
459 \%replacements
460 );
461 }
462
463 # Generate the .deb
464 chdir($tempdir);
465 #system("head -100 *");
466 # Instruct equivs-build to use the current directory
467 my $tmpdir = $ENV{TMPDIR};
468 delete $ENV{TMPDIR};
469 my $pkgbuild = capturex(qw(equivs-build control));
470 $ENV{TMPDIR} = $tmpdir;
471 #system("dpkg-deb --info *.deb");
472
473 my @debs = glob('*.deb');
474 # Sanity check
475 die "$0: BUG: Not exactly one .deb file generated" unless $#debs == 0;
476
477 return $tempdir.'/'.$debs[0];
478 }
479
480 sub gen_file_from_template {
481 my ($input, $output, $replacements) = @_;
482
483 my $text = read_text($input);
484
485 foreach my $key (%$replacements) {
486 $text =~ s/__${key}__/$replacements->{$key}/g;
487 }
488
489 write_text($output, $text);
490 }
491
492 sub usage
493 {
494 print << "EOF";
495 usage:
496 $0 [--install] [--ssh] [--deb] [--all] \
497 <core file or PID or program> [ ... ]
498
499 You must already have the correct debug lines in your sources.list and have
500 executed 'apt update'.
501
502 $0 requires the elfutils and dctrl-tools packages to be installed.
503 EOF
504
505 exit 1;
506 }
507
508 # vim: syntax=perl sw=4 sts=4 sr et