diff options
author | Ian Lance Taylor <iant@golang.org> | 2023-11-29 14:01:49 -0800 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2023-11-29 14:04:36 -0800 |
commit | 8b2e510ca3158bac5553e90cd9d856a5db5dafb7 (patch) | |
tree | a6284dc2b2ec4ec6a4e270421dc19e16f22657f2 | |
parent | 0b242afffdddc251eca96b37c59802aa66197be7 (diff) | |
download | gcc-8b2e510ca3158bac5553e90cd9d856a5db5dafb7.zip gcc-8b2e510ca3158bac5553e90cd9d856a5db5dafb7.tar.gz gcc-8b2e510ca3158bac5553e90cd9d856a5db5dafb7.tar.bz2 |
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.
-rw-r--r-- | libbacktrace/config.h.in | 3 | ||||
-rwxr-xr-x | libbacktrace/configure | 13 | ||||
-rw-r--r-- | libbacktrace/configure.ac | 2 | ||||
-rw-r--r-- | libbacktrace/fileline.c | 50 |
4 files changed, 66 insertions, 2 deletions
diff --git a/libbacktrace/config.h.in b/libbacktrace/config.h.in index a4f5bdd..ee26163 100644 --- a/libbacktrace/config.h.in +++ b/libbacktrace/config.h.in @@ -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 diff --git a/libbacktrace/configure b/libbacktrace/configure index 0ccc060..7ade966 100755 --- a/libbacktrace/configure +++ b/libbacktrace/configure @@ -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 diff --git a/libbacktrace/configure.ac b/libbacktrace/configure.ac index 71cd50f..00acb42 100644 --- a/libbacktrace/configure.ac +++ b/libbacktrace/configure.ac @@ -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 diff --git a/libbacktrace/fileline.c b/libbacktrace/fileline.c index 0e560b4..773f3a9 100644 --- a/libbacktrace/fileline.c +++ b/libbacktrace/fileline.c @@ -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 (); } |