36
36
#include <sys/stat.h>
37
37
#include <sys/types.h>
38
38
#include <unistd.h>
39
+ #include <execinfo.h>
40
+
39
41
40
42
#ifndef Py_BUILD_CORE_BUILTIN
41
43
# define Py_BUILD_CORE_MODULE 1
@@ -152,7 +154,7 @@ pid_to_task(pid_t pid)
152
154
153
155
result = task_for_pid (mach_task_self (), pid , & task );
154
156
if (result != KERN_SUCCESS ) {
155
- printf ("Call to task_for_pid failed on PID %d: %s" , pid , mach_error_string (result ));
157
+ printf ("Call to task_for_pid failed on PID %d: %s\n " , pid , mach_error_string (result ));
156
158
PyErr_SetString (PyExc_PermissionError , "Cannot get task for PID" );
157
159
return 0 ;
158
160
}
@@ -272,14 +274,14 @@ void*
272
274
get_py_runtime (pid_t pid )
273
275
{
274
276
char elf_file [256 ];
275
- unsigned long start_address = find_python_map_start_address (pid , elf_file );
277
+ void * start_address = ( void * ) find_python_map_start_address (pid , elf_file );
276
278
277
279
if (start_address == 0 ) {
278
280
PyErr_SetString (PyExc_RuntimeError , "No memory map associated with python or libpython found" );
279
281
return NULL ;
280
282
}
281
283
282
- unsigned long result = 0 ;
284
+ void * result = NULL ;
283
285
284
286
int fd = open (elf_file , O_RDONLY );
285
287
if (fd < 0 ) {
@@ -315,7 +317,7 @@ get_py_runtime(pid_t pid)
315
317
}
316
318
317
319
if (py_runtime_section != NULL ) {
318
- result = start_address + ( unsigned long ) py_runtime_section -> sh_addr ;
320
+ result = start_address + py_runtime_section -> sh_addr ;
319
321
}
320
322
321
323
close (fd );
@@ -324,6 +326,39 @@ get_py_runtime(pid_t pid)
324
326
}
325
327
326
328
#endif
329
+ static void full_write (int fd , const char * buf , size_t len )
330
+ {
331
+ while (len > 0 ) {
332
+ ssize_t ret = write (fd , buf , len );
333
+
334
+ if ((ret == -1 ) && (errno != EINTR ))
335
+ break ;
336
+
337
+ buf += (size_t ) ret ;
338
+ len -= (size_t ) ret ;
339
+ }
340
+ }
341
+ void print_backtrace (void )
342
+ {
343
+ static const char start [] = "BACKTRACE ------------\n" ;
344
+ static const char end [] = "----------------------\n" ;
345
+
346
+ void * bt [1024 ];
347
+ int bt_size ;
348
+ char * * bt_syms ;
349
+ int i ;
350
+
351
+ bt_size = backtrace (bt , 1024 );
352
+ bt_syms = backtrace_symbols (bt , bt_size );
353
+ full_write (STDERR_FILENO , start , strlen (start ));
354
+ for (i = 1 ; i < bt_size ; i ++ ) {
355
+ size_t len = strlen (bt_syms [i ]);
356
+ full_write (STDERR_FILENO , bt_syms [i ], len );
357
+ full_write (STDERR_FILENO , "\n" , 1 );
358
+ }
359
+ full_write (STDERR_FILENO , end , strlen (end ));
360
+ free (bt_syms );
361
+ }
327
362
328
363
ssize_t
329
364
read_memory (pid_t pid , void * remote_address , ssize_t size , void * local_address )
@@ -337,7 +372,9 @@ read_memory(pid_t pid, void* remote_address, ssize_t size, void* local_address)
337
372
338
373
bytes_read = process_vm_readv (pid , & local_iov , 1 , & remote_iov , 1 , 0 );
339
374
if (bytes_read == -1 ) {
375
+
340
376
PyErr_SetFromErrno (PyExc_OSError );
377
+ print_backtrace ();
341
378
return -1 ;
342
379
}
343
380
0 commit comments