Subject: Re: bug#71292: 30.0.50; macOS: .elns have no debug info

From: Gerd Möllmann <[email-masked]>
Date: Sun, 02 Jun 2024 07:41:13 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: Gerd Möllmann <[email-masked]>
>> Cc: [email-masked]
>> Date: Sat, 01 Jun 2024 08:15:39 +0200
>> 
>> When debugging a native-compiled function (speed = 1) an macOS/arm64, I
>> find that the debug info that libgccjit 14.1 generates is apparently
>> incorrect.
>> 
>> For example, I see car_safe being called with some argument, but when I
>> look in the caller in the generated C file and print what it purportedly
>> passes as an argumebnt, it's something completely different.
>
> This should be reported to the GCC and libgccjit folks.  The most
> probable cause is incorrect DWARF debug info emitted by the compiler.
> Alternatively, it could be that LLDB interprets the debug info
> incorrectly.  I sometimes see similar problems when debugging Emacs,
> especially in optimized versions: arguments in function calls are
> shown in reverse order, which of course is bogus, since the code
> works as expected.

After reading the GCC "how to report a bug" advice, I don't think I can
produce something useful for them in any reasonable time frame.

The jit people will also not be of great help I'm afraid. There seems to
be no one in the know using macOS :-).

But there have been some arm64 related commits in GCC during the last
weeks, so there's hope.

BTW, I'm now using the attached change in my Emacs, which allows me to
set native-comp-debug, -speed, and -driver-options via environment
variables. Maybe that's something for Andrea (CC'd).

>From ec514e5e81034f5524bba552c56f7f8caa9fff71 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Gerd=20M=C3=B6llmann?= <[email-masked]>
Date: Sun, 2 Jun 2024 07:25:19 +0200
Subject: [PATCH] Allow overriding comp settings for speed, debug, driver

---
 lisp/emacs-lisp/comp.el | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/lisp/emacs-lisp/comp.el b/lisp/emacs-lisp/comp.el
index 4c76f95a0e9..2cf9b43dddb 100644
--- a/lisp/emacs-lisp/comp.el
+++ b/lisp/emacs-lisp/comp.el
@@ -3585,17 +3585,7 @@ native-compile
 return the compiled function."
   (comp--native-compile function-or-file nil output))
 
-;;;###autoload
-(defun batch-native-compile (&optional for-tarball)
-  "Perform batch native compilation of remaining command-line arguments.
-
-Native compilation equivalent of `batch-byte-compile'.
-Use this from the command line, with `-batch'; it won't work
-in an interactive Emacs session.
-Optional argument FOR-TARBALL non-nil means the file being compiled
-as part of building the source tarball, in which case the .eln file
-will be placed under the native-lisp/ directory (actually, in the
-last directory in `native-comp-eln-load-path')."
+(defun batch-native-compile-1 (&optional for-tarball)
   (comp-ensure-native-compiler)
   (let ((comp-running-batch-compilation t)
         (native-compile-target-directory
@@ -3610,6 +3600,35 @@ batch-native-compile
              else
              collect (byte-compile-file file))))
 
+;;;###autoload
+(cl-defun batch-native-compile (&optional for-tarball)
+  "Perform batch native compilation of remaining command-line arguments.
+
+Native compilation equivalent of `batch-byte-compile'.
+Use this from the command line, with `-batch'; it won't work
+in an interactive Emacs session.
+Optional argument FOR-TARBALL non-nil means the file being compiled
+as part of building the source tarball, in which case the .eln file
+will be placed under the native-lisp/ directory (actually, in the
+last directory in `native-comp-eln-load-path')."
+  (cl-macrolet
+      ((with-env ((&rest vars) &body body)
+         (let (clauses)
+           (cl-with-gensyms (val)
+             (dolist (var vars)
+	       (let ((env (string-replace
+		           "-" "_"
+                           (upcase (symbol-name var)))))
+	         (push `(,var (let ((,val (getenv ,env)))
+			        (if ,val
+			            (car (read-from-string ,val))
+			          ,var)))
+		       clauses))))
+           `(let (,@(nreverse clauses)) ,@body))))
+    (with-env (native-comp-speed native-comp-debug
+                                 native-comp-driver-options)
+      (batch-native-compile-1 for-tarball))))
+
 ;; In use by elisp-mode.el
 (defun comp--write-bytecode-file (eln-file)
   "After native compilation write the bytecode file for ELN-FILE.
-- 
2.45.2