debug-link.scm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  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.
  10. ;;;
  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.
  15. ;;;
  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/>.
  18. (define-module (test-debug-link)
  19. #:use-module (guix elf)
  20. #:use-module (guix build utils)
  21. #:use-module (guix build debug-link)
  22. #:use-module (guix gexp)
  23. #:use-module (guix store)
  24. #:use-module (guix tests)
  25. #:use-module (guix monads)
  26. #:use-module (guix derivations)
  27. #:use-module (gnu packages bootstrap)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-26)
  30. #:use-module (srfi srfi-64)
  31. #:use-module (rnrs io ports)
  32. #:use-module (ice-9 match))
  33. (define %guile-executable
  34. (match (false-if-exception (readlink "/proc/self/exe"))
  35. ((? string? program)
  36. (and (file-exists? program) (elf-file? program)
  37. program))
  38. (_
  39. #f)))
  40. (define read-elf
  41. (compose parse-elf get-bytevector-all))
  42. (test-begin "debug-link")
  43. (unless %guile-executable (test-skip 1))
  44. (test-assert "elf-debuglink"
  45. (let ((elf (call-with-input-file %guile-executable read-elf)))
  46. (match (call-with-values (lambda () (elf-debuglink elf)) list)
  47. ((#f #f) ;no '.gnu_debuglink' section
  48. (pk 'no-debuglink #t))
  49. (((? string? file) (? integer? crc))
  50. (string-suffix? ".debug" file)))))
  51. ;; Since we need %BOOTSTRAP-GCC and co., we have to skip the following tests
  52. ;; when networking is unreachable because we'd fail to download it.
  53. (unless (network-reachable?) (test-skip 1))
  54. (test-assertm "elf-debuglink"
  55. ;; Check whether we can compute the CRC just like objcopy, and whether we
  56. ;; can retrieve it.
  57. (let* ((code (plain-file "test.c" "int main () { return 42; }"))
  58. (exp (with-imported-modules '((guix build utils)
  59. (guix build debug-link)
  60. (guix elf))
  61. #~(begin
  62. (use-modules (guix build utils)
  63. (guix build debug-link)
  64. (guix elf)
  65. (rnrs io ports))
  66. (define read-elf
  67. (compose parse-elf get-bytevector-all))
  68. (setenv "PATH" (string-join '(#$%bootstrap-gcc
  69. #$%bootstrap-binutils)
  70. "/bin:" 'suffix))
  71. (invoke "gcc" "-O0" "-g" #$code "-o" "exe")
  72. (copy-file "exe" "exe.debug")
  73. (invoke "strip" "--only-keep-debug" "exe.debug")
  74. (invoke "strip" "--strip-debug" "exe")
  75. (invoke "objcopy" "--add-gnu-debuglink=exe.debug"
  76. "exe")
  77. (call-with-values (lambda ()
  78. (elf-debuglink
  79. (call-with-input-file "exe"
  80. read-elf)))
  81. (lambda (file crc)
  82. (call-with-output-file #$output
  83. (lambda (port)
  84. (let ((expected (call-with-input-file "exe.debug"
  85. debuglink-crc32)))
  86. (write (list file (= crc expected))
  87. port))))))))))
  88. (mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
  89. (x (built-derivations (list drv))))
  90. (call-with-input-file (derivation->output-path drv)
  91. (lambda (port)
  92. (return (match (read port)
  93. (("exe.debug" #t) #t)
  94. (x (pk 'fail x #f)))))))))
  95. (unless (network-reachable?) (test-skip 1))
  96. (test-assertm "set-debuglink-crc"
  97. ;; Check whether 'set-debuglink-crc' successfully updates the CRC.
  98. (let* ((code (plain-file "test.c" "int main () { return 42; }"))
  99. (debug (plain-file "exe.debug" "a"))
  100. (exp (with-imported-modules '((guix build utils)
  101. (guix build debug-link)
  102. (guix elf))
  103. #~(begin
  104. (use-modules (guix build utils)
  105. (guix build debug-link)
  106. (guix elf)
  107. (rnrs io ports))
  108. (define read-elf
  109. (compose parse-elf get-bytevector-all))
  110. (setenv "PATH" (string-join '(#$%bootstrap-gcc
  111. #$%bootstrap-binutils)
  112. "/bin:" 'suffix))
  113. (invoke "gcc" "-O0" "-g" #$code "-o" "exe")
  114. (copy-file "exe" "exe.debug")
  115. (invoke "strip" "--only-keep-debug" "exe.debug")
  116. (invoke "strip" "--strip-debug" "exe")
  117. (invoke "objcopy" "--add-gnu-debuglink=exe.debug"
  118. "exe")
  119. (set-debuglink-crc "exe" #$debug)
  120. (call-with-values (lambda ()
  121. (elf-debuglink
  122. (call-with-input-file "exe"
  123. read-elf)))
  124. (lambda (file crc)
  125. (call-with-output-file #$output
  126. (lambda (port)
  127. (write (list file crc) port)))))))))
  128. (mlet* %store-monad ((drv (gexp->derivation "debuglink" exp))
  129. (x (built-derivations (list drv))))
  130. (call-with-input-file (derivation->output-path drv)
  131. (lambda (port)
  132. (return (match (read port)
  133. (("exe.debug" crc)
  134. (= crc (debuglink-crc32 (open-input-string "a"))))
  135. (x
  136. (pk 'fail x #f)))))))))
  137. (test-end "debug-link")