Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化主线程获取栈顶/栈底耗时较大的问题 #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 50 additions & 92 deletions library/src/main/unwind32/ptrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ b * limitations under the License.
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <inttypes.h>

#include "ptrace-arch.h"
#include "libudf_unwind_p.h"
Expand Down Expand Up @@ -105,111 +106,68 @@ static pid_t ubrd_pid;

static int ubrd_get_main_thread_stack(pid_t pid)
{
char c, line[1024];
int i = 0, j = 0, fd = -1;
void* lib = NULL;
int (*close_fptr)(int) = NULL; //use func ptr to avoid open/close rehook deadlock
int (*open_fptr)(const char *, int, ...) = NULL;

lib = dlopen("libc.so", RTLD_NOW|RTLD_GLOBAL);
if (lib == NULL) {
LIBUDF_LOG("Could not open libc.so: %s\n", dlerror());
return -1;
}

close_fptr = (int (*)(int))dlsym(lib, "close");
open_fptr = (int (*)(const char *, int, ...))dlsym(lib, "open");
if (!close_fptr || !open_fptr) {
LIBUDF_LOG("invalid open/close: %s\n", dlerror());
dlclose(lib);
return -1;
}

char line[1024];
snprintf(line, sizeof(line), "/proc/self/task/%d/maps", pid);

fd = open_fptr(line, O_RDONLY);
FILE* fd = fopen(line, "r");
if (fd < 0) {
LIBUDF_LOG("/proc/%d/task/%d/maps open fail\n", pid, pid);
dlclose(lib);
return -1;
}

while (1) {
if (!read(fd, &c, 1)) break;
if (c == '\n') {
line[i] = '\0';
if (strncmp((const char *)&line[i-7], "[stack]", 7) == 0) {
const char* stack_start;
LIBUDF_LOG("stack:%s\n", line);
#if defined(__LP64__)
//64bit main stack format
//7ff1bf1000-7ff1c12000 rw-p 00000000 00:00 0 [stack]
line[9] = '0';
line[10] = 'x';
line[21] = '\0'; //line[11~20] stack start
stack_start = (const char*)&line[9];
#else
//32bit main stack format
//becd7000-becf8000 rw-p 00000000 00:00 0 [stack]
line[7] = '0';
line[8] = 'x';
line[17] = '\0'; //line[9~16] 32bit main thread stack start
stack_start = (const char*)&line[7];
#endif
_main_thread_stack_start = strtoul(stack_start, (char **)NULL, 16);
_main_thread_stack_size = 8 * 1024 * 1024; //bypass RLIMIT check for simple handling
LIBUDF_LOG("[LCH_DEBUG]main_thread_stack_start:%p, main_thread_stack_size:%p\n",
while (fgets(line, sizeof(line), fd)) {
if (strstr(line, "[stack]") != NULL) {
LIBUDF_LOG("stack:%s\n", line);
const char* stack_start;
#if defined(__LP64__)
//64bit main stack format
//7ff1bf1000-7ff1c12000 rw-p 00000000 00:00 0 [stack]
line[9] = '0';
line[10] = 'x';
line[21] = '\0'; //line[11~20] stack start
stack_start = (const char*)&line[9];
#else
//32bit main stack format
//becd7000-becf8000 rw-p 00000000 00:00 0 [stack]
line[7] = '0';
line[8] = 'x';
line[17] = '\0'; //line[9~16] 32bit main thread stack start
stack_start = (const char*)&line[7];
#endif
_main_thread_stack_start = strtoul(stack_start, (char **)NULL, 16);
_main_thread_stack_size = 8 * 1024 * 1024; //bypass RLIMIT check for simple handling
LIBUDF_LOG("[LCH_DEBUG]main_thread_stack_start:%p, main_thread_stack_size:%p\n",
(void *)_main_thread_stack_start, (void *)_main_thread_stack_size);
close_fptr(fd);
dlclose(lib);
return 0;
}
else {
i = 0;
}
}
else {
line[i++] = c;
fclose(fd);
return 0;
}
}
close_fptr(fd);

// stack is 28th parameter from /proc/self/stat
fd = open_fptr("/proc/self/stat", O_RDONLY);
if (fd < 0) {
LIBUDF_LOG("/proc/self/stat open fail\n");
dlclose(lib);
return -1;
}
fclose(fd);

i = j = 0;
line[0] = '\0';
while (1) {
if (!read(fd, &c, 1)) break;
if (c == ' ') {
j++;
if (j == 28) {
line[i] = '\0';
break;
}
}
else if (j == 27) {
line[i++] = c;
}
}
// stack is 28th parameter from /proc/self/stat
fd = fopen("/proc/self/stat", "re");
if (fd == NULL) {
LIBUDF_LOG("/proc/self/stat open fail\n");
return -1;
}

if (line[0] != '\0') {
_main_thread_stack_start = strtoul(line, (char **)NULL, 10);
_main_thread_stack_size = 8 * 1024 * 1024; // bypass RLIMIT check for simple handling
LIBUDF_LOG("[LCH_DEBUG]main_thread_stack_start:%p, main_thread_stack_size:%p\n",
(void *)_main_thread_stack_start, (void *)_main_thread_stack_size);
close_fptr(fd);
dlclose(lib);
return 0;
}
if (fgets(line, sizeof(line), fd)) {
const char *end_of_comm = strrchr((const char *)line, (int)')');
size_t stack_start = 0;
if (1 == sscanf(end_of_comm + 1,
" %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %*u %*d %*d %*d %*d %*d %*d %*u %*u "
"%*d %*u %*u %*u %" SCNuPTR,
&stack_start)) {
_main_thread_stack_start = stack_start;
_main_thread_stack_size = 8 * 1024 * 1024; // bypass RLIMIT check for simple handling
LIBUDF_LOG("[LCH_DEBUG]main_thread_stack_start:%p, main_thread_stack_size:%p\n",
(void *)_main_thread_stack_start, (void *)_main_thread_stack_size);
fclose(fd);
return 0;
}
}

close_fptr(fd);
dlclose(lib);
fclose(fd);
return -1;
}

Expand Down