libbacktrace: call GetModuleFileNameA on Windows (25889d2)

This commit is contained in:
Bartosz Taudul 2024-03-13 00:18:14 +01:00
parent 11bf33c32c
commit a4eecd508d
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3

View File

@ -47,6 +47,18 @@ POSSIBILITY OF SUCH DAMAGE. */
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#endif #endif
#ifdef HAVE_WINDOWS_H
#ifndef WIN32_MEAN_AND_LEAN
#define WIN32_MEAN_AND_LEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif
#include "backtrace.hpp" #include "backtrace.hpp"
#include "internal.hpp" #include "internal.hpp"
@ -168,6 +180,37 @@ macho_get_executable_path (struct backtrace_state *state,
#endif /* !HAVE_DECL__PGMPTR */ #endif /* !HAVE_DECL__PGMPTR */
#ifdef HAVE_WINDOWS_H
#define FILENAME_BUF_SIZE (MAX_PATH)
static char *
windows_get_executable_path (char *buf, backtrace_error_callback error_callback,
void *data)
{
size_t got;
int error;
got = GetModuleFileNameA (NULL, buf, FILENAME_BUF_SIZE - 1);
error = GetLastError ();
if (got == 0
|| (got == FILENAME_BUF_SIZE - 1 && error == ERROR_INSUFFICIENT_BUFFER))
{
error_callback (data,
"could not get the filename of the current executable",
error);
return NULL;
}
return buf;
}
#else /* !defined (HAVE_WINDOWS_H) */
#define windows_get_executable_path(buf, error_callback, data) NULL
#define FILENAME_BUF_SIZE 64
#endif /* !defined (HAVE_WINDOWS_H) */
/* Initialize the fileline information from the executable. Returns 1 /* Initialize the fileline information from the executable. Returns 1
on success, 0 on failure. */ on success, 0 on failure. */
@ -181,7 +224,7 @@ fileline_initialize (struct backtrace_state *state,
int called_error_callback; int called_error_callback;
int descriptor; int descriptor;
const char *filename; const char *filename;
char buf[64]; char buf[FILENAME_BUF_SIZE];
if (!state->threaded) if (!state->threaded)
failed = state->fileline_initialization_failed; failed = state->fileline_initialization_failed;
@ -205,7 +248,7 @@ fileline_initialize (struct backtrace_state *state,
descriptor = -1; descriptor = -1;
called_error_callback = 0; called_error_callback = 0;
for (pass = 0; pass < 9; ++pass) for (pass = 0; pass < 10; ++pass)
{ {
int does_not_exist; int does_not_exist;
@ -242,6 +285,9 @@ fileline_initialize (struct backtrace_state *state,
case 8: case 8:
filename = macho_get_executable_path (state, error_callback, data); filename = macho_get_executable_path (state, error_callback, data);
break; break;
case 9:
filename = windows_get_executable_path (buf, error_callback, data);
break;
default: default:
abort (); abort ();
} }