Skip to content

Commit 4ae8d98

Browse files
libbacktrace: fetch executable path on macOS
PR libbacktrace/96973 * fileline.c (macho_get_executable_path): New static function. (fileline_initialize): Call macho_get_executable_path.
1 parent 430dc8b commit 4ae8d98

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

fileline.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ POSSIBILITY OF SUCH DAMAGE. */
4343
#include <sys/sysctl.h>
4444
#endif
4545

46+
#ifdef HAVE_MACH_O_DYLD_H
47+
#include <mach-o/dyld.h>
48+
#endif
49+
4650
#include "backtrace.h"
4751
#include "internal.h"
4852

@@ -122,6 +126,35 @@ sysctl_exec_name2 (struct backtrace_state *state,
122126

123127
#endif /* defined (HAVE_KERN_PROC_ARGS) || |defined (HAVE_KERN_PROC) */
124128

129+
#ifdef HAVE_MACH_O_DYLD_H
130+
131+
static char *
132+
macho_get_executable_path (struct backtrace_state *state,
133+
backtrace_error_callback error_callback, void *data)
134+
{
135+
uint32_t len;
136+
char *name;
137+
138+
len = 0;
139+
if (_NSGetExecutablePath (NULL, &len) == 0)
140+
return NULL;
141+
name = (char *) backtrace_alloc (state, len, error_callback, data);
142+
if (name == NULL)
143+
return NULL;
144+
if (_NSGetExecutablePath (name, &len) != 0)
145+
{
146+
backtrace_free (state, name, len, error_callback, data);
147+
return NULL;
148+
}
149+
return name;
150+
}
151+
152+
#else /* !defined (HAVE_MACH_O_DYLD_H) */
153+
154+
#define macho_get_executable_path(state, error_callback, data) NULL
155+
156+
#endif /* !defined (HAVE_MACH_O_DYLD_H) */
157+
125158
/* Initialize the fileline information from the executable. Returns 1
126159
on success, 0 on failure. */
127160

@@ -159,7 +192,7 @@ fileline_initialize (struct backtrace_state *state,
159192

160193
descriptor = -1;
161194
called_error_callback = 0;
162-
for (pass = 0; pass < 7; ++pass)
195+
for (pass = 0; pass < 8; ++pass)
163196
{
164197
int does_not_exist;
165198

@@ -188,6 +221,9 @@ fileline_initialize (struct backtrace_state *state,
188221
case 6:
189222
filename = sysctl_exec_name2 (state, error_callback, data);
190223
break;
224+
case 7:
225+
filename = macho_get_executable_path (state, error_callback, data);
226+
break;
191227
default:
192228
abort ();
193229
}

0 commit comments

Comments
 (0)