#define argp 16 #define entry 0 #define entrycode 168 #define exit 4 #define exitcode 40 #define func 8 #define orig1 24 #define orig2 28 #define probe1 32 #define probe2 36 #define rval 20 #define saveret 12 #define ax 0 #define cx 1 #define dx 2 #define bx 3 #define bp 4 #define si 5 #define di 6 #define sp 7 #define call0 BYTE $0xe8; BYTE $0x00; BYTE $0x00; BYTE $0x00; BYTE $0x00 #define pop(reg) BYTE $(0x58 + (reg)) #define popa BYTE $0x61 #define popf BYTE $0x9d #define push(reg) BYTE $(0x50 + (reg)) #define pusha BYTE $0x60 #define pushf BYTE $0x9c TEXT pentrytmpl(SB), 1, $0 /* 11(sp) first arg to func */ /* 10(sp) call func somewhere */ /* 9(sp) synthetized call at func start */ pushf /* 8(sp) */ pusha /* 0(sp) ⋯ 7(sp) */ call0 pop(ax) /* pushf + pusha + call0 = 7 bytes */ SUBL $(7 + entrycode), AX /* save argp */ LEAL 44(SP), BX MOVL BX, argp(AX) /* save return address of probed function */ MOVL 40(SP), BX MOVL BX, saveret(AX) /* trap function return to probe exit */ LEAL exitcode(AX), BX MOVL BX, 40(SP) /* uninstall probe */ MOVL func(AX), BX MOVL orig1(AX), CX MOVL orig2(AX), DX MOVL CX, 0(BX) MOVL DX, 4(BX) /* patch up return address so we return to start of function */ MOVL BX, 36(SP) /* call the hook last */ PUSHL AX CALL entry(AX) POPL AX popa popf RET END TEXT pexittmpl(SB), 1, $0 push(ax) /* 9(sp) */ pushf /* 8(sp) */ pusha /* 0(sp) ⋯ 7(sp) */ call0 pop(ax) /* pushf + pushf + pusha + call0 = 8 bytes */ SUBL $(8 + exitcode), AX /* save return value */ MOVL 36(SP), BX MOVL BX, rval(AX) /* call the hook first */ PUSHL AX PUSHL AX CALL exit(AX) POPL AX POPL AX /* reinstall probe */ MOVL func(AX), BX MOVL probe1(AX), CX MOVL probe2(AX), DX MOVL CX, 0(BX) MOVL DX, 4(BX) MOVL saveret(AX), BX MOVL BX, 36(SP) popa popf RET END