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

ST: Add support to save RBP register that allows stack backtrace #3987

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion trunk/3rdparty/st-srs/md.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,17 @@ extern void _st_md_cxt_restore(_st_jmp_buf_t env, int val);

#if defined(__amd64__) || defined(__x86_64__)
#define MD_GET_SP(_t) *((long *)&((_t)->context[0].__jmpbuf[6]))
#define MD_GET_BP(_t) *((long *)&((_t)->context[0].__jmpbuf[1]))
#else
#error Unknown CPU architecture
#endif

#define MD_INIT_CONTEXT(_thread, _sp, _main) \
#define MD_INIT_CONTEXT(_thread, _sp, _bp, _main) \
ST_BEGIN_MACRO \
if (MD_SETJMP((_thread)->context)) \
_main(); \
MD_GET_SP(_thread) = (long) (_sp); \
MD_GET_BP(_thread) = (long) (_bp); \
ST_END_MACRO

#define MD_GET_UTIME() \
Expand Down
36 changes: 34 additions & 2 deletions trunk/3rdparty/st-srs/sched.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,16 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl
_st_stack_t *stack;
void **ptds;
char *sp;


void *stack_bp; /* Frame pointer for this function */
void *stack_pbp; /* Frame pointer for parent function */
unsigned long crbp; /* Current RBP register value */
unsigned long crsp; /* Current RSP register value */
unsigned long prbp; /* Parent RBP register value */
int home_space_size;
asm volatile("mov %%rbp, %0" : "=r"(crbp));
asm volatile("mov %%rsp, %0" : "=r"(crsp));

/* Adjust stack size */
if (stk_size == 0)
stk_size = ST_DEFAULT_STACK_SIZE;
Expand Down Expand Up @@ -663,7 +672,30 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl
thread->start = start;
thread->arg = arg;

_ST_INIT_CONTEXT(thread, stack->sp, _st_thread_main);
/* Initialize RBP and return address for parent coroutine. */
prbp = *(unsigned long*)crbp;
stack->sp = (char*)stack->sp - 0x10; /* For ra and rbp. */
*(((unsigned long*)stack->sp) + 1) = *(((unsigned long*)prbp) + 1); /* Save parent RA(return address) */
*(unsigned long*)stack->sp = *(unsigned long*)prbp; /* Save parent RBP */

stack_pbp = stack->sp;
home_space_size = (int)(prbp - crbp - 0x10);
stack->sp = (char*)stack->sp - home_space_size; /* Save parent home space */
memcpy(stack_pbp - home_space_size, (void*)(prbp - home_space_size), home_space_size);

/* Initialize RBP and return address for new coroutine. */
stack->sp = (char*)stack->sp - 0x10; /* For ra and rbp. */
*(((unsigned long*)stack->sp) + 1) = *(((unsigned long*)crbp)+1); /* Simulate instruction call, store RA(return address) */
*(unsigned long*)stack->sp = *(unsigned long*)crbp; /* Save RBP: push rbp */
*(unsigned long*)stack->sp = (unsigned long)stack_pbp; /* Overwrite parent RBP */

stack_bp = stack->sp; /* Save RSP as RBP: mov rbp, rsp */
home_space_size = (int)(crbp - crsp);
stack->sp = (char*)stack->sp - home_space_size; /* For home space: sub rsp, 0x20 */
memcpy(stack_bp - home_space_size, (void*)(crbp - home_space_size), home_space_size);

/* Initialize thread context. */
_ST_INIT_CONTEXT(thread, stack->sp, stack_bp, _st_thread_main);

/* If thread is joinable, allocate a termination condition variable */
if (joinable) {
Expand Down
4 changes: 2 additions & 2 deletions trunk/auto/depends.sh
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ cp -rf $SRS_WORKDIR/research/players ${SRS_OBJS}/nginx/html/ &&

# for favicon.ico
rm -rf ${SRS_OBJS}/nginx/html/favicon.ico &&
cp -f $SRS_WORKDIR/research/api-server/static-dir/favicon.ico ${SRS_OBJS}/nginx/html/favicon.ico &&
cp -f $SRS_WORKDIR/research/favicon.ico ${SRS_OBJS}/nginx/html/favicon.ico &&

# For srs-console.
rm -rf ${SRS_OBJS}/nginx/html/console &&
Expand All @@ -335,7 +335,7 @@ cp -rf $SRS_WORKDIR/3rdparty/signaling/www/demos ${SRS_OBJS}/nginx/html/ &&

# For home page index.html
rm -rf ${SRS_OBJS}/nginx/html/index.html &&
cp -f $SRS_WORKDIR/research/api-server/static-dir/index.html ${SRS_OBJS}/nginx/html/index.html &&
cp -f $SRS_WORKDIR/research/index.html ${SRS_OBJS}/nginx/html/index.html &&

# nginx.html to detect whether nginx is alive
echo "Nginx is ok." > ${SRS_OBJS}/nginx/html/nginx.html
Expand Down
29 changes: 29 additions & 0 deletions trunk/research/st/backtrace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
g++ backtrace.cpp ../../objs/st/libst.a -g -O0 -o backtrace && ./backtrace
*/
#include <stdio.h>
#include "../../objs/st/st.h"

void* pfn(void* arg) {
for (;;) {
printf("Hello, coroutine\n");
st_sleep(1);
}
return NULL;
}

int bar(int argc) {
st_thread_create(pfn, NULL, 0, 0);
return argc + 1;
}

int foo(int argc) {
return bar(argc);
}

int main(int argc, char** argv) {
st_init();
foo(argc);
st_thread_exit(NULL);
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

st_destroy() missing?

}