View Issue Details

IDProjectCategoryView StatusLast Update
0038739FPCRTLpublic2021-04-17 10:43
ReporterC Western Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Product Version3.3.1 
Summary0038739: Patch: fallback stack trace information (mainly for Darwin)
DescriptionThe attached patch is based on that from my submitted 38483. (The patch I suggested there has been applied to trunk, with the exception of this part; I am adding it here so the information is not lost.) It provides a fall back method information for a stack trace in case where the standard debug information is not present. It is most useful under Mac OSX, as the debug information is typically kept in separate files, and if (say) the executable is copied to a different machine, trace back information is only numeric unless special care is taken. With this patch, routine name and an address offset is given. It should also work under Linux in general but:
1. It is less useful, as the standard debug information is normally in the executable file itself for linux.
2. For reasons I don't understand I can't build the rtl if this patch is applied, though the patched file will compile outside the RTL.
I would suggest applying with {$IFDEF DARWIN} as a minimum (for which the rtl does build as normal).
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Relationships

related to 0038483 resolvedJonas Maebe Intial implementation of line info information on AARCH64/Darwin 

Activities

C Western

2021-04-11 22:10

reporter  

lnfodwarf.patch (1,055 bytes)   
Index: rtl/inc/lnfodwrf.pp
===================================================================
--- rtl/inc/lnfodwrf.pp	(revision 49177)
+++ rtl/inc/lnfodwrf.pp	(working copy)
@@ -46,7 +46,7 @@
 implementation
 
 uses
-  exeinfo;
+  dl, exeinfo;
 
 { Current issues:
 
@@ -1423,6 +1423,7 @@
   line   : longint;
   Store  : TBackTraceStrFunc;
   Success : boolean;
+  dlinfo: dl_info;
 begin
   {$ifdef DEBUG_LINEINFO}
   writeln(stderr,'DwarfBackTraceStr called');
@@ -1449,7 +1450,18 @@
       end;
       DwarfBackTraceStr := DwarfBackTraceStr + ' of ' + source;
     end;
-  end;
+  end
+  else
+    { Fall back on dlinfo - gives mangled names, but better than nothing }
+    if dladdr(addr, @dlinfo) <> 0 then
+    begin
+      if dlinfo.dli_sname <> nil then
+        DwarfBackTraceStr := DwarfBackTraceStr + '  ' + StrPas(dlinfo.dli_sname);
+      if dlinfo.dli_saddr <> nil then begin
+        Str(addr-dlinfo.dli_saddr, hs);
+        DwarfBackTraceStr := DwarfBackTraceStr + ' + ' + hs;
+      end;
+    end;
   BackTraceStrFunc := Store;
 end;
 
lnfodwarf.patch (1,055 bytes)   

Jonas Maebe

2021-04-11 22:22

manager   ~0130291

> 2. For reasons I don't understand I can't build the rtl if this patch is applied, though the patched file will compile outside the RTL.
You probably didn't update the unit dependencies in the makefile (you're adding a dependency on the dl unit). Also, it probably won't be accepted for Linux as is because a dependency on the dl unit would result in an unconditional dependency on libc. It has to be implemented via a callback, like UnixGetModuleByAddrHook

I disagree that the fact that on macOS the debug information has to be in a separate file (when it's DWARF) is a good argument to implement this (if you need line number information, you need line number information; mangled function names and offsets are no substitute for that). However, this can be useful in case crashes occur in external libraries without debug information (and in that case, the platform is irrelevant).

C Western

2021-04-12 22:19

reporter   ~0130325

Given the issues with Linux it might make sense to restrict this patch to Darwin only. I still think it makes sense there - while it may be possible for the debug information to all be ine exaceutable file, I think the platform default is for it to be in a separate file. (If fpc defaulted to putting all the debug information in the main excutable I would hvae rather less use for this patch.)

Jonas Maebe

2021-04-12 22:32

manager   ~0130327

> Given the issues with Linux it might make sense to restrict this patch to Darwin only.
No. Just like UnixGetModuleByAddrHook, it needs to be implemented via a callback (that one is also hooked by the dl unit, and also works that way on Linux).

> (If fpc defaulted to putting all the debug information in the main excutable I would hvae rather less use for this patch.)
It is not FPC that places the debug information inside/outside the executable, that's the linker. And DWARF debug information cannot be stored in the executable by the linker on Darwin. This choice was made by Apple to speed up the linking process. Instead, the binary contains references to the original object files (which contain their local debug information), and
a) either you follow those references and build your own debug information map at run time (which almost no one does or supports any more), or
b) you run dsymutil in advance; which collects all of that information and collects it in a single file (and you probably pay back the speed you gained while linking)

I still don't understand what the problem is with requiring the .dSYM bundle to be present to have line number information. Yes, you need to copy it in addition to the binary. Once you know that, the dladdr fallback becomes useless (except in cases where it's always useful, also on other platforms: when simply no debug information is available).

C Western

2021-04-17 10:43

reporter   ~0130421

I would say the patch provides a useful fall back if the full debug information is not available for some reason. I would rather work with mangled names without line numbers than a stack trace with only numbers. I am particularly thinking of working with, for example, a separate user of my program where the debug information has not been distributed (or has been mis-configured).

Issue History

Date Modified Username Field Change
2021-04-11 22:10 C Western New Issue
2021-04-11 22:10 C Western File Added: lnfodwarf.patch
2021-04-11 22:16 Jonas Maebe Relationship added related to 0038483
2021-04-11 22:22 Jonas Maebe Note Added: 0130291
2021-04-12 22:19 C Western Note Added: 0130325
2021-04-12 22:32 Jonas Maebe Note Added: 0130327
2021-04-17 10:43 C Western Note Added: 0130421