1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; 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.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (test-debug-link)
20 #:use-module (guix elf)
21 #:use-module (guix build utils)
22 #:use-module (guix build debug-link)
23 #:use-module (guix gexp)
24 #:use-module (guix store)
25 #:use-module (guix tests)
26 #:use-module (guix monads)
27 #:use-module (guix derivations)
28 #:use-module (gnu packages bootstrap)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-26)
31 #:use-module (srfi srfi-64)
32 #:use-module (rnrs io ports)
33 #:use-module (ice-9 match))
35 (define %guile-executable
36 (match (false-if-exception (readlink "/proc/self/exe"))
38 (and (file-exists? program) (elf-file? program)
44 (compose parse-elf get-bytevector-all))
47 (test-begin "debug-link")
49 (unless %guile-executable (test-skip 1))
50 (test-assert "elf-debuglink"
51 (let ((elf (call-with-input-file %guile-executable read-elf)))
52 (match (call-with-values (lambda () (elf-debuglink elf)) list)
53 ((#f #f) ;no '.gnu_debuglink' section
54 (pk 'no-debuglink #t))
55 (((? string? file) (? integer? crc))
56 (string-suffix? ".debug" file)))))
58 ;; Since we need %BOOTSTRAP-GCC and co., we have to skip the following tests
59 ;; when networking is unreachable because we'd fail to download it.
60 (unless (network-reachable?) (test-skip 1))
61 (test-assertm "elf-debuglink"
62 ;; Check whether we can compute the CRC just like objcopy, and whether we
64 (let* ((code (plain-file "test.c" "int main () { return 42; }"))
65 (exp (with-imported-modules '((guix build utils)
66 (guix build debug-link)
69 (use-modules (guix build utils)
70 (guix build debug-link)
75 (compose parse-elf get-bytevector-all))
77 (setenv "PATH" (string-join '(#$%bootstrap-gcc
78 #$%bootstrap-binutils)
80 (invoke "gcc" "-O0" "-g" #$code "-o" "exe")
81 (copy-file "exe" "exe.debug")
82 (invoke "strip" "--only-keep-debug" "exe.debug")
83 (invoke "strip" "--strip-debug" "exe")
84 (invoke "objcopy" "--add-gnu-debuglink=exe.debug"
86 (call-with-values (lambda ()
88 (call-with-input-file "exe"
91 (call-with-output-file #$output
93 (let ((expected (call-with-input-file "exe.debug"
95 (write (list file (= crc expected))
97 (mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
98 (x (built-derivations (list drv))))
99 (call-with-input-file (derivation->output-path drv)
101 (return (match (read port)
102 (("exe.debug" #t) #t)
103 (x (pk 'fail x #f)))))))))
105 (unless (network-reachable?) (test-skip 1))
106 (test-assertm "set-debuglink-crc"
107 ;; Check whether 'set-debuglink-crc' successfully updates the CRC.
108 (let* ((code (plain-file "test.c" "int main () { return 42; }"))
109 (debug (plain-file "exe.debug" "a"))
110 (exp (with-imported-modules '((guix build utils)
111 (guix build debug-link)
114 (use-modules (guix build utils)
115 (guix build debug-link)
120 (compose parse-elf get-bytevector-all))
122 (setenv "PATH" (string-join '(#$%bootstrap-gcc
123 #$%bootstrap-binutils)
125 (invoke "gcc" "-O0" "-g" #$code "-o" "exe")
126 (copy-file "exe" "exe.debug")
127 (invoke "strip" "--only-keep-debug" "exe.debug")
128 (invoke "strip" "--strip-debug" "exe")
129 (invoke "objcopy" "--add-gnu-debuglink=exe.debug"
131 (set-debuglink-crc "exe" #$debug)
132 (call-with-values (lambda ()
134 (call-with-input-file "exe"
137 (call-with-output-file #$output
139 (write (list file crc) port)))))))))
140 (mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
141 (x (built-derivations (list drv))))
142 (call-with-input-file (derivation->output-path drv)
144 (return (match (read port)
146 (= crc (debuglink-crc32 (open-input-string "a"))))
148 (pk 'fail x #f)))))))))
150 (test-end "debug-link")