Skip to content

Instantly share code, notes, and snippets.

@kulp
Created September 28, 2012 20:38
Show Gist options
  • Save kulp/4cf0592154f5ac563bfe to your computer and use it in GitHub Desktop.
Save kulp/4cf0592154f5ac563bfe to your computer and use it in GitHub Desktop.
TF-flag management on x86 for tracing individual function calls
// clobbers %rax ; this could cause issues with variadic functions
#define trace_wrap(Ret,Func,...) \
({ \
long long temp; \
void flag_wrap(); \
/* Write the function pointer into the red zone on the stack \
* and hope that the function we call doesn't use up that much \
* stack for its arguments */ \
asm volatile("movq %0,-120(%%rsp)" : : "a"(Func)); \
flag_wrap(__VA_ARGS__); \
/* This ends up being `mov %rax, %rax` but is necessary to \
* tell GCC that we need to use the %rax that flag_wrap \
* just produced (indirectly). */ \
asm volatile("mov %%rax, %0" : "=a"(temp)); \
*(Ret*)&temp; \
}) \
//
.globl flag_wrap
#ifdef __ELF__
.type flag_wrap,@function
#endif
.balign 16
flag_wrap:
pushf
orw $0x0100, (%rsp)
popf
callq *-112(%rsp)
pushf
andw $0xfeff, (%rsp)
popf
ret
#ifdef __ELF__
.size flag_wrap, . - WRAP_NAME
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment