mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-10-31 08:51:55 +00:00 
			
		
		
		
	ggml : Fix missing backtrace on Linux (ggml/1228)
* Modern Linux defaults /proc/sys/kernel/yama/ptrace_scope to 1 * Fixed lldb attach * Simplify by having the child do ggml_print_backtrace_symbols
This commit is contained in:
		 Daniel Tang
					Daniel Tang
				
			
				
					committed by
					
						 Georgi Gerganov
						Georgi Gerganov
					
				
			
			
				
	
			
			
			 Georgi Gerganov
						Georgi Gerganov
					
				
			
						parent
						
							9c55e5c5c2
						
					
				
				
					commit
					60aea028b5
				
			| @@ -70,6 +70,9 @@ float ggml_table_f32_f16[1 << 16]; | |||||||
| #include <sys/types.h> | #include <sys/types.h> | ||||||
| #include <sys/stat.h> | #include <sys/stat.h> | ||||||
| #include <sys/wait.h> | #include <sys/wait.h> | ||||||
|  | #if defined(__linux__) | ||||||
|  | #include <sys/prctl.h> | ||||||
|  | #endif | ||||||
|  |  | ||||||
| #if defined(__ANDROID__) | #if defined(__ANDROID__) | ||||||
| #include <unwind.h> | #include <unwind.h> | ||||||
| @@ -133,10 +136,36 @@ static void ggml_print_backtrace(void) { | |||||||
|     if (GGML_NO_BACKTRACE) { |     if (GGML_NO_BACKTRACE) { | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|     char attach[32]; | #if defined(__linux__) | ||||||
|     snprintf(attach, sizeof(attach), "attach %d", getpid()); |     FILE * f = fopen("/proc/self/status", "r"); | ||||||
|     int pid = fork(); |     size_t size = 0; | ||||||
|     if (pid == 0) { |     char * line = NULL; | ||||||
|  |     ssize_t length = 0; | ||||||
|  |     while ((length = getline(&line, &size, f)) > 0) { | ||||||
|  |         if (!strncmp(line, "TracerPid:", sizeof("TracerPid:") - 1) && | ||||||
|  |             (length != sizeof("TracerPid:\t0\n") - 1 || line[length - 2] != '0')) { | ||||||
|  |             // Already being debugged, and the breakpoint is the later abort() | ||||||
|  |             free(line); | ||||||
|  |             fclose(f); | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     free(line); | ||||||
|  |     fclose(f); | ||||||
|  |     int lock[2] = { -1, -1 }; | ||||||
|  |     (void) !pipe(lock); // Don't start gdb until after PR_SET_PTRACER | ||||||
|  | #endif | ||||||
|  |     const int parent_pid = getpid(); | ||||||
|  |     const int child_pid = fork(); | ||||||
|  |     if (child_pid < 0) { // error | ||||||
|  |         return; | ||||||
|  |     } else if (child_pid == 0) { // child | ||||||
|  |         char attach[32]; | ||||||
|  |         snprintf(attach, sizeof(attach), "attach %d", parent_pid); | ||||||
|  | #if defined(__linux__) | ||||||
|  |         close(lock[1]); | ||||||
|  |         (void) !read(lock[0], lock, 1); | ||||||
|  | #endif | ||||||
|         // try gdb |         // try gdb | ||||||
|         execlp("gdb", "gdb", "--batch", |         execlp("gdb", "gdb", "--batch", | ||||||
|             "-ex", "set style enabled on", |             "-ex", "set style enabled on", | ||||||
| @@ -149,18 +178,18 @@ static void ggml_print_backtrace(void) { | |||||||
|         execlp("lldb", "lldb", "--batch", |         execlp("lldb", "lldb", "--batch", | ||||||
|             "-o", "bt", |             "-o", "bt", | ||||||
|             "-o", "quit", |             "-o", "quit", | ||||||
|             "-p", attach, |             "-p", &attach[sizeof("attach ") - 1], | ||||||
|             (char *) NULL); |             (char *) NULL); | ||||||
|         exit(EXIT_FAILURE); |         // gdb failed, fallback to backtrace_symbols | ||||||
|     } else { |         ggml_print_backtrace_symbols(); | ||||||
|         int wstatus; |         _Exit(0); | ||||||
|         waitpid(pid, &wstatus, 0); |     } else { // parent | ||||||
|         if (WIFEXITED(wstatus)) { | #if defined(__linux__) | ||||||
|             if (WEXITSTATUS(wstatus) == EXIT_FAILURE) { |         prctl(PR_SET_PTRACER, child_pid); | ||||||
|                 // gdb failed, fallback to backtrace_symbols |         close(lock[1]); | ||||||
|                 ggml_print_backtrace_symbols(); |         close(lock[0]); | ||||||
|             } | #endif | ||||||
|         } |         waitpid(child_pid, NULL, 0); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| #else | #else | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user