Skip to content

Commit 29dd634

Browse files
committed
dbug: correct trace for DBUG_RETURN(func()); -- gcc only
when func1 calls func2 from DBUG_RETURN, dbug shows the trace as | > func1 | < func1 | > func2 | < func2 because DBUG_LEAVE happens before func2(). Change that to invoke DBUG_LEAVE when the local variable goes out of scope. This uses gcc specific __attribute__((cleanup)).
1 parent 8f60656 commit 29dd634

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

cmake/os/WindowsCache.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ IF(MSVC_VERSION GREATER 1310)
320320
SET(HAVE_VSNPRINTF 1 CACHE INTERNAL "")
321321
ENDIF()
322322
SET(HAVE_WEAK_SYMBOL CACHE INTERNAL "")
323+
SET(HAVE_ATTRIBUTE_CLEANUP CACHE INTERNAL "")
323324
SET(HAVE_WORDS_BIGENDIAN TRUE CACHE INTERNAL "")
324325
SET(WORDS_BIGENDIAN CACHE INTERNAL "")
325326
SET(HAVE__S_IFIFO 1 CACHE INTERNAL "")

config.h.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@
465465

466466
#cmakedefine HAVE_WEAK_SYMBOL 1
467467
#cmakedefine HAVE_ABI_CXA_DEMANGLE 1
468-
468+
#cmakedefine HAVE_ATTRIBUTE_CLEANUP 1
469469

470470
#cmakedefine HAVE_POSIX_SIGNALS 1
471471
#cmakedefine HAVE_BSD_SIGNALS 1

configure.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,14 @@ CHECK_C_SOURCE_COMPILES("
910910
HAVE_WEAK_SYMBOL
911911
)
912912

913+
CHECK_C_SOURCE_COMPILES("
914+
void foo(int *x) { }
915+
int main() {
916+
int a __attribute__((cleanup(foo)));
917+
return 0;
918+
}"
919+
HAVE_ATTRIBUTE_CLEANUP
920+
)
913921

914922
CHECK_CXX_SOURCE_COMPILES("
915923
#include <new>

dbug/dbug.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@ void _db_enter_(const char *_func_, const char *_file_,
11061106
}
11071107
save_errno= errno;
11081108

1109+
_stack_frame_->line= -1;
11091110
_stack_frame_->func= cs->func;
11101111
_stack_frame_->file= cs->file;
11111112
cs->func= _func_;
@@ -1161,14 +1162,17 @@ void _db_enter_(const char *_func_, const char *_file_,
11611162
*
11621163
*/
11631164

1164-
void _db_return_(uint _line_, struct _db_stack_frame_ *_stack_frame_)
1165+
void _db_return_(struct _db_stack_frame_ *_stack_frame_)
11651166
{
11661167
int save_errno=errno;
11671168
uint _slevel_= _stack_frame_->level & ~TRACE_ON;
11681169
CODE_STATE *cs;
11691170
get_code_state_or_return;
11701171

1171-
if (cs->framep != _stack_frame_)
1172+
if (_stack_frame_->line == 0)
1173+
return;
1174+
1175+
if (_stack_frame_->line == -1 || cs->framep != _stack_frame_)
11721176
{
11731177
char buf[512];
11741178
my_snprintf(buf, sizeof(buf), ERR_MISSING_RETURN, cs->func);
@@ -1183,7 +1187,7 @@ void _db_return_(uint _line_, struct _db_stack_frame_ *_stack_frame_)
11831187
{
11841188
if (!cs->locked)
11851189
pthread_mutex_lock(&THR_LOCK_dbug);
1186-
DoPrefix(cs, _line_);
1190+
DoPrefix(cs, _stack_frame_->line);
11871191
Indent(cs, cs->level);
11881192
(void) fprintf(cs->stack->out_file->file, "<%s\n", cs->func);
11891193
DbugFlush(cs);

include/my_dbug.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct _db_stack_frame_ {
3030
const char *func; /* function name of the previous stack frame */
3131
const char *file; /* filename of the function of previous frame */
3232
uint level; /* this nesting level, highest bit enables tracing */
33+
int line; /* line of DBUG_RETURN */
3334
struct _db_stack_frame_ *prev; /* pointer to the previous frame */
3435
};
3536

@@ -48,7 +49,7 @@ extern void _db_set_(const char *control);
4849
extern void _db_set_init_(const char *control);
4950
extern void _db_enter_(const char *_func_, const char *_file_, uint _line_,
5051
struct _db_stack_frame_ *_stack_frame_);
51-
extern void _db_return_(uint _line_, struct _db_stack_frame_ *_stack_frame_);
52+
extern void _db_return_(struct _db_stack_frame_ *_stack_frame_);
5253
extern void _db_pargs_(uint _line_,const char *keyword);
5354
extern void _db_doprnt_(const char *format,...)
5455
ATTRIBUTE_FORMAT(printf, 1, 2);
@@ -63,12 +64,24 @@ extern void dbug_swap_code_state(void **code_state_store);
6364
extern void dbug_free_code_state(void **code_state_store);
6465
extern const char* _db_get_func_(void);
6566

67+
#define DBUG_LEAVE do { \
68+
_db_stack_frame_.line= __LINE__; \
69+
_db_return_ (&_db_stack_frame_); \
70+
_db_stack_frame_.line= 0; \
71+
} while(0)
72+
73+
#ifdef HAVE_ATTRIBUTE_CLEANUP
74+
#define DBUG_ENTER(a) struct _db_stack_frame_ _db_stack_frame_ __attribute__((cleanup(_db_return_))); \
75+
_db_enter_ (a,__FILE__,__LINE__,&_db_stack_frame_)
76+
#define DBUG_RETURN(a1) do { _db_stack_frame_.line=__LINE__; return(a1);} while(0)
77+
#define DBUG_VOID_RETURN do { _db_stack_frame_.line=__LINE__; return;} while(0)
78+
#else
6679
#define DBUG_ENTER(a) struct _db_stack_frame_ _db_stack_frame_; \
6780
_db_enter_ (a,__FILE__,__LINE__,&_db_stack_frame_)
68-
#define DBUG_LEAVE _db_return_ (__LINE__, &_db_stack_frame_)
69-
7081
#define DBUG_RETURN(a1) do {DBUG_LEAVE; return(a1);} while(0)
7182
#define DBUG_VOID_RETURN do {DBUG_LEAVE; return;} while(0)
83+
#endif
84+
7285
#define DBUG_EXECUTE(keyword,a1) \
7386
do {if (_db_keyword_(0, (keyword), 0)) { a1 }} while(0)
7487
#define DBUG_EXECUTE_IF(keyword,a1) \

0 commit comments

Comments
 (0)