Lua Debugging

From Cheat Engine
Jump to navigation Jump to search

Lua Debugging[edit]

There are functions exposed to LUA that enable scripts to control the debugger:

  • openProcess(name) - Open a running process
  • debugProcess() - Start debugging the open process
  • debug_setBreakpoint(address) - Add a breakpoint at an address, optional parameters are size and type
  • debug_continueFromBreakpoint(method) - Continue execution, methods are "co_run", "co_stepinto" and "co_stepover"

("co_runtill" is in TContinueOption but not shown in main.lua, probably because it needs to know what address to run to).

In addition, you can create the event function debugger_onBreakpoint() that will be called by Cheat Engine whenever a breakpoint is hit.

Here's an example for Space Pirates and Zomies that tracks ships that have their armor decreased. By debugging I found that at 0x406002, ECX has a pointer to a structure with the armor float at 0x60. This code will track those values in the armors table with the key being the address and the value being the actual armor value:

if not armors then
   armors = { }

function debugger_onBreakpoint()
         if (EIP ~= 0x00406002) then return 0 end -- ignore user-set breakpoints
         armors[ECX] = readFloat(ECX+0x60) -- store armor in table
         debug_continueFromBreakpoint(co_run) -- continue execution
         return 1 -- let CE know we handled breakpoint, no need to update debugger form



Add a breakpoint at an address, optional parameters are size and type.

  • size: number of bytes to break for counting from the address (default: 1, not used if type is bptExecute)
  • type: type of breakpoint (default bptExecute)
    • bptExecute: break when the instruction pointer EIP is in the area
    • bptAccess: break when memory in the area is accessed
    • bptWrite: break when memory in the area is written to

Types from debuggertypedefinitions.pas[1]:

  TContinueOption = (co_run=0, co_stepinto=1, co_stepover=2, co_runtill=3);  

  TBreakpointTrigger = (bptExecute=0, bptAccess=1, bptWrite=2); 

Available variables[edit]

These variables can be used in your debugger_onBreakpoint() event function.

Changes to these variables will only take effect if the process is break'd and there's no (other) current debug_setBreakpoint method running.

Assembly registers:[source] Holds the current value of the registers.

  • (All CE versions:) EAX, EBX, ECX, EDX, EDI, ESI, EBP, ESP, EIP
  • (CE 64-bit only:) RAX, RBX, RCX, RDX, RDI, RSI, RBP, RSP, RIP, R8, R9, R10, R11, R12, R13, R14, R15
  • If extraregs(?):
    • Floating point registers: FP0, FP1, FP2, FP3, FP4, FP5, FP6, FP7
    • XMM registers: XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7
    • If CE 64-bit: XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15