Skip to content

Instantly share code, notes, and snippets.

@sredna
Created May 21, 2023 18:42
Show Gist options
  • Save sredna/7bdb2e90dbbb0f5bba2a4a2ad8d8b1c4 to your computer and use it in GitHub Desktop.
Save sredna/7bdb2e90dbbb0f5bba2a4a2ad8d8b1c4 to your computer and use it in GitHub Desktop.
NSIS DbgCheckRegisters.nsh
/*
** DbgCheckRegisters.nsh
** =====================
** A header for validating that functions have not touched the registers.
*/
!macro _DbgRegisters_For pre pos list
!if "${list}" == ""
!define /ReDef list "$0$1$2$3$4$5$6$7$8$9$R0$R1$R2$R3$R4$R5$R6$R7$R8$R9"
!endif
!searchparse /NoErrors "${list}" '$' _DbgRegisters_For '$' list
!if "${_DbgRegisters_For}" != ""
${pre} ${_DbgRegisters_For}
!insertmacro ${__MACRO__} `${pre}` `${pos}` "$${list}"
${pos} ${_DbgRegisters_For}
!else
!undef _DbgRegisters_For
!endif
!macroend
!macro _DbgCheckRegisters_Begin mode this list
!searchparse /NoErrors "${list}" '$' this '$' list
!if "${this}" != ""
!if ${mode} == Push
Push $${this}
!endif
!insertmacro ${__MACRO__} ${mode} "" "$${list}"
!if ${mode} == Pop
Pop $${this}
!endif
!endif
!macroend
!macro _DbgCheckRegisters_Fill reg
StrCpy $${reg} "DBG:<<<Reg$$${reg}>>>${DbgCheckRegisters}"
!macroend
!macro DbgCheckRegisters_Begin ignorelist
!verbose push 3
!ifndef NODEBUG & NoDbgCheckRegisters
!searchreplace /IgnoreCase ignorelist "${ignorelist}" ' ' ""
!define /ReDef /Math DbgCheckRegisters "${DbgCheckRegisters}" + 1
!define DbgCheckRegisters_${DbgCheckRegisters} "${ignorelist}"
!insertmacro _DbgCheckRegisters_Begin Push "" "${ignorelist}"
!insertmacro _DbgRegisters_For '!insertmacro _DbgCheckRegisters_Fill' ';' ""
!insertmacro _DbgCheckRegisters_Begin Pop "" "${ignorelist}"
!verbose pop
!endif
!macroend
!macro _DbgCheckRegisters_Validate reg
!searchreplace /IgnoreCase _DbgCheckRegisters_V "${DbgCheckRegisters_${DbgCheckRegisters}}" "$${reg}" ""
!if "${_DbgCheckRegisters_V}" == "${DbgCheckRegisters_${DbgCheckRegisters}}"
StrCmp $${reg} "DBG:<<<Reg$$${reg}>>>${DbgCheckRegisters}" +2
DetailPrint "DEBUG: $$${reg} corrupted! (Nesting=${DbgCheckRegisters} Value=$${reg})"
!endif
!undef _DbgCheckRegisters_V
!macroend
!macro DbgCheckRegisters_Validate
!verbose push 3
!ifndef NODEBUG & NoDbgCheckRegisters
!if "${DbgCheckRegisters}" < 1
!error "Missing call to DbgCheckRegisters_Begin?"
!endif
!insertmacro _DbgRegisters_For '!insertmacro _DbgCheckRegisters_Validate' ';' ""
!undef DbgCheckRegisters_${DbgCheckRegisters}
!define /ReDef /Math DbgCheckRegisters "${DbgCheckRegisters}" - 1
!if "${DbgCheckRegisters}" < 1
!undef DbgCheckRegisters
!endif
!endif
!verbose pop
!macroend
; 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8< 8<
Section "Example"
!insertmacro DbgCheckRegisters_Begin ""
Call Func_Good
!insertmacro DbgCheckRegisters_Validate
!insertmacro DbgCheckRegisters_Begin ""
Call Func_Bad
!insertmacro DbgCheckRegisters_Validate
StrCpy $R8 "Hi, I'm the register $$R8 input value"
!insertmacro DbgCheckRegisters_Begin "$0 $3 $R8"
Call Func_InpRegR8_RetReg0Reg3
!insertmacro DbgCheckRegisters_Validate
DetailPrint "Return=$0:$3"
SectionEnd
Function Func_Good
Push $1
StrCpy $1 "Only visible to me"
Pop $1
FunctionEnd
Function Func_Bad
StrCpy $1 "Ooops, I'm bad. Sorry about $$1"
FunctionEnd
Function Func_InpRegR8_RetReg0Reg3
DetailPrint "Input=$R8"
StrCpy $0 "Valid return value"
StrCpy $3 "And one more"
FunctionEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment