libbacktrace: call GetModuleFileNameA on Windows

Patch from Björn Schäpers.

	* fileline.c: Include <windows.h> if available.
	(windows_get_executable_path): New static function.
	(fileline_initialize): Call windows_get_executable_path.
	* configure.ac: Checked for windows.h
	* configure: Regenerate.
	* config.h.in: Regenerate.
This commit is contained in:
Ian Lance Taylor 2023-11-29 14:01:49 -08:00
parent 0b242afffd
commit 8b2e510ca3
4 changed files with 66 additions and 2 deletions

View file

@ -104,6 +104,9 @@
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H
/* Define to 1 if you have the <windows.h> header file. */
#undef HAVE_WINDOWS_H
/* Define if -lz is available. */
#undef HAVE_ZLIB

View file

@ -13509,6 +13509,19 @@ $as_echo "#define HAVE_LOADQUERY 1" >>confdefs.h
fi
for ac_header in windows.h
do :
ac_fn_c_check_header_mongrel "$LINENO" "windows.h" "ac_cv_header_windows_h" "$ac_includes_default"
if test "x$ac_cv_header_windows_h" = xyes; then :
cat >>confdefs.h <<_ACEOF
#define HAVE_WINDOWS_H 1
_ACEOF
fi
done
# Check for the fcntl function.
if test -n "${with_target_subdir}"; then
case "${host}" in

View file

@ -379,6 +379,8 @@ if test "$have_loadquery" = "yes"; then
AC_DEFINE(HAVE_LOADQUERY, 1, [Define if AIX loadquery is available.])
fi
AC_CHECK_HEADERS(windows.h)
# Check for the fcntl function.
if test -n "${with_target_subdir}"; then
case "${host}" in

View file

@ -47,6 +47,18 @@ POSSIBILITY OF SUCH DAMAGE. */
#include <mach-o/dyld.h>
#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.h"
#include "internal.h"
@ -165,6 +177,37 @@ macho_get_executable_path (struct backtrace_state *state,
#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
on success, 0 on failure. */
@ -178,7 +221,7 @@ fileline_initialize (struct backtrace_state *state,
int called_error_callback;
int descriptor;
const char *filename;
char buf[64];
char buf[FILENAME_BUF_SIZE];
if (!state->threaded)
failed = state->fileline_initialization_failed;
@ -202,7 +245,7 @@ fileline_initialize (struct backtrace_state *state,
descriptor = -1;
called_error_callback = 0;
for (pass = 0; pass < 9; ++pass)
for (pass = 0; pass < 10; ++pass)
{
int does_not_exist;
@ -239,6 +282,9 @@ fileline_initialize (struct backtrace_state *state,
case 8:
filename = macho_get_executable_path (state, error_callback, data);
break;
case 9:
filename = windows_get_executable_path (buf, error_callback, data);
break;
default:
abort ();
}