Skip to content

Commit

Permalink
write a minidump if ziti-edge-tunnel crashes on windows (#1084)
Browse files Browse the repository at this point in the history
* write a minidump if ziti-edge-tunnel crashes on windows

* windows only, just use MAX_PATH which is what we define PATH_MAX to when needed
  • Loading branch information
dovholuknf authored Jan 10, 2025
1 parent 44f6c0d commit 5ef54ac
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
5 changes: 4 additions & 1 deletion programs/ziti-edge-tunnel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ if (WIN32)
include/windows/windows-scripts.h
windows-scripts.c
windows/log_utils.c
include/service-utils.h)
include/service-utils.h
windows/minidump.c
windows/minidump.h
)
endif ()
if (LINUX)
set(ZITI_INSTANCE_OS linux/diverter.c include/linux/diverter.h)
Expand Down
2 changes: 1 addition & 1 deletion programs/ziti-edge-tunnel/windows/log_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static uint8_t mkdir_p(const char *path) {
return mkdir(cur_path, NULL);
}

static char* get_log_path() {
char* get_log_path() {
char process_dir[PATH_MAX]; //create string buffer to hold path
char process_full_path[PATH_MAX];
char drive[_MAX_DRIVE];
Expand Down
40 changes: 40 additions & 0 deletions programs/ziti-edge-tunnel/windows/minidump.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <windows.h>
#include <dbghelp.h>
#include <stdio.h>

extern char* get_log_path();
LONG WINAPI CrashFilter(EXCEPTION_POINTERS *pExceptionInfo) {
char* mini_dump_path = calloc(MAX_PATH, sizeof(char));
snprintf(mini_dump_path, MAX_PATH, "%s%cziti-edge-tunnel.crash.dmp", get_log_path(), PATH_SEP);

printf("minidump created at: %s", mini_dump_path);
HANDLE hFile = CreateFile(mini_dump_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile && hFile != INVALID_HANDLE_VALUE) {
MINIDUMP_EXCEPTION_INFORMATION mei = {
GetCurrentThreadId(),
pExceptionInfo,
TRUE
};
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, MiniDumpWithThreadInfo, &mei, NULL, NULL);
CloseHandle(hFile);
}
free(mini_dump_path);
mini_dump_path = NULL;
return EXCEPTION_EXECUTE_HANDLER;
}
23 changes: 23 additions & 0 deletions programs/ziti-edge-tunnel/windows/minidump.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef ZITI_TUNNEL_SDK_C_MINIDUMP_H
#define ZITI_TUNNEL_SDK_C_MINIDUMP_H

#include <windows.h>
LONG WINAPI CrashFilter(EXCEPTION_POINTERS *exceptionPointers);

#endif //ZITI_TUNNEL_SDK_C_MINIDUMP_H
6 changes: 6 additions & 0 deletions programs/ziti-edge-tunnel/ziti-edge-tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "netif_driver/windows/tun.h"
#include "windows/windows-service.h"
#include "windows/windows-scripts.h"
#include "windows/minidump.h"

#endif

Expand Down Expand Up @@ -2749,6 +2750,11 @@ static bool is_host_only() {
}

int main(int argc, char *argv[]) {
#if _WIN32
//register a crash handler for Windows
SetUnhandledExceptionFilter(CrashFilter);
#endif

const char *name = strrchr(argv[0], '/');
if (name == NULL) {
name = argv[0];
Expand Down

0 comments on commit 5ef54ac

Please sign in to comment.