@@ -44,6 +44,19 @@ static int (*unw_getcontext)(unw_context_t *) = NULL;
4444#include <dlfcn.h>
4545#endif
4646
47+ int _per_loop (void ) {
48+ // how many void* are written to the stack trace per loop iterations?
49+ #ifdef RPYTHON_VMPROF
50+ return 2 ;
51+ #else
52+ if (vmp_profiles_python_lines ()) {
53+ return 2 ;
54+ }
55+ return 1 ;
56+ #endif
57+ }
58+
59+
4760#ifdef PY_TEST
4861// for testing only!
4962PY_EVAL_RETURN_T * vmprof_eval (PY_STACK_FRAME_T * f , int throwflag ) { return NULL ; }
@@ -130,19 +143,27 @@ static PY_STACK_FRAME_T * _write_python_stack_entry(PY_STACK_FRAME_T * frame, vo
130143int vmp_walk_and_record_python_stack_only (PY_STACK_FRAME_T * frame , void * * result ,
131144 int max_depth , int depth , intptr_t pc )
132145{
133- while (depth < max_depth && frame ) {
146+ while (( depth + _per_loop ()) <= max_depth && frame ) {
134147 frame = _write_python_stack_entry (frame , result , & depth , max_depth );
135148 }
136149 return depth ;
137150}
138151
139152#ifdef VMP_SUPPORTS_NATIVE_PROFILING
140- int _write_native_stack (void * addr , void * * result , int depth ) {
153+ int _write_native_stack (void * addr , void * * result , int depth , int max_depth ) {
141154#ifdef RPYTHON_VMPROF
155+ if (depth + 2 >= max_depth ) {
156+ // bail, do not write to unknown memory
157+ return depth ;
158+ }
142159 result [depth ++ ] = (void * )VMPROF_NATIVE_TAG ;
143160#else
144161 if (vmp_profiles_python_lines ()) {
145- // even if we do not log a python stack frame,
162+ if (depth + 2 >= max_depth ) {
163+ // bail, do not write to unknown memory
164+ return depth ;
165+ }
166+ // even if we do not log a python line number,
146167 // we must keep the profile readable
147168 result [depth ++ ] = 0 ;
148169 }
@@ -227,7 +248,7 @@ int vmp_walk_and_record_stack(PY_STACK_FRAME_T *frame, void ** result,
227248
228249 int depth = 0 ;
229250 PY_STACK_FRAME_T * top_most_frame = frame ;
230- while (depth < max_depth ) {
251+ while (( depth + _per_loop ()) <= max_depth ) {
231252 unw_get_proc_info (& cursor , & pip );
232253
233254 func_addr = pip .start_ip ;
@@ -269,7 +290,7 @@ int vmp_walk_and_record_stack(PY_STACK_FRAME_T *frame, void ** result,
269290 // this is possible because compiler align to 8 bytes.
270291 //
271292 if (func_addr != 0x0 ) {
272- depth = _write_native_stack ((void * )(func_addr | 0x1 ), result , depth );
293+ depth = _write_native_stack ((void * )(func_addr | 0x1 ), result , depth , max_depth );
273294 }
274295 }
275296
0 commit comments