Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1b13cd9

Browse filesBrowse files
committed
Fixes
1 parent 0de2132 commit 1b13cd9
Copy full SHA for 1b13cd9

File tree

Expand file treeCollapse file tree

1 file changed

+41
-4
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+41
-4
lines changed

‎Modules/_testexternalinspection.c

Copy file name to clipboardExpand all lines: Modules/_testexternalinspection.c
+41-4Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include <sys/stat.h>
3737
#include <sys/types.h>
3838
#include <unistd.h>
39+
#include <execinfo.h>
40+
3941

4042
#ifndef Py_BUILD_CORE_BUILTIN
4143
# define Py_BUILD_CORE_MODULE 1
@@ -152,7 +154,7 @@ pid_to_task(pid_t pid)
152154

153155
result = task_for_pid(mach_task_self(), pid, &task);
154156
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));
156158
PyErr_SetString(PyExc_PermissionError, "Cannot get task for PID");
157159
return 0;
158160
}
@@ -272,14 +274,14 @@ void*
272274
get_py_runtime(pid_t pid)
273275
{
274276
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);
276278

277279
if (start_address == 0) {
278280
PyErr_SetString(PyExc_RuntimeError, "No memory map associated with python or libpython found");
279281
return NULL;
280282
}
281283

282-
unsigned long result = 0;
284+
void* result = NULL;
283285

284286
int fd = open(elf_file, O_RDONLY);
285287
if (fd < 0) {
@@ -315,7 +317,7 @@ get_py_runtime(pid_t pid)
315317
}
316318

317319
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;
319321
}
320322

321323
close(fd);
@@ -324,6 +326,39 @@ get_py_runtime(pid_t pid)
324326
}
325327

326328
#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+
}
327362

328363
ssize_t
329364
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)
337372

338373
bytes_read = process_vm_readv(pid, &local_iov, 1, &remote_iov, 1, 0);
339374
if (bytes_read == -1) {
375+
340376
PyErr_SetFromErrno(PyExc_OSError);
377+
print_backtrace();
341378
return -1;
342379
}
343380

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.