retrieve OutputDebug text

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

retrieve OutputDebug text

19 Oct 2018, 22:14

tl;dr help improve my mostly-finished script that receives OutputDebug (OutputDebugString) text

- I've been using #Warn in my scripts, and adding 'local' etc, to make my functions as usable as possible for other people.
- A MsgBox for each of #Warn's warning is too unwieldy. #Warn's OutputDebug option, however, is quite practical. I can receive notifications via an external program such as DebugView, copy the text, and parse it. However, DebugView can receive notifications from other programs, which then has to be filtered.
- What would be best, would be to have a script that received notifications from a specific script or scripts, that would then append text to a file, and beep each time a new variable was seen.

- Here is a proof of concept script for handling OutputDebug, it's basically got everything, but it could be improved a little.
- The loop continues even when the script it's watching is closed.
- Sometimes the script shows the OutputDebug message twice (two MsgBoxes).
- Sometimes the MsgBox shows no text. (But it appears that ReadProcessMemory never fails.)

- Btw people are quite welcome to take the script and adapt/expand the code, using their own style, for use in debugging scripts.

Code: Select all

;==================================================

;get OutputDebug text (intro)

;programs can send debug text via:
;- OutputDebug (AutoHotkey function)
;- OutputDebugString (Winapi function)
;programs can retrieve debug text via:
;- DebugActiveProcess/WaitForDebugEvent/ContinueDebugEvent
;programs that can retrieve debug text:
;- e.g. DebugView
;- e.g. this script

;re. DebugView
;download DebugView, run Dbgview.exe,
;any time an OutputDebug line is executed,
;the text appears in the DebugView window
;e.g. OutputDebug, my text ;AHK v1
;e.g. OutputDebug("my text") ;AHK v2
;e.g. DllCall("kernel32\OutputDebugString", Str,"my text")

;note: Unicode/ANSI text
;the text in each of these lines ended up being converted
;to ANSI text (with best-fit characters)
;when received by DebugView
;OutputDebug, abc√√√def ;AHK v1
;OutputDebug("abc√√√def") ;AHK v2
;DllCall("kernel32\OutputDebugString", Str,"abc√√√def")
;DllCall("kernel32\OutputDebugStringW", WStr,"abc√√√def")

;==================================================

;get OutputDebug text
;this script retrieves debug events from AutoHotkey.ahk
;note: this script currently remains open even when AutoHotkey.ahk is closed

;example code for AutoHotkey.ahk
;q::OutputDebug, my text ;AHK v1
;q::OutputDebug("my text") ;AHK v2

DetectHiddenWindows, On
SetTitleMatchMode, 2 ;Contains
WinGet, vPID, PID, AutoHotkey.ahk
DllCall("kernel32\DebugActiveProcess", UInt,vPID)

Loop
{
	VarSetCapacity(DEBUG_EVENT, A_PtrSize=8?176:96, 0)
	;INFINITE := 0xFFFFFFFF ;source: WinBase.h
	if !DllCall("kernel32\WaitForDebugEvent", Ptr,&DEBUG_EVENT, UInt,-1)
		break

	;not working (tested on Windows 7):
	;if !DllCall("kernel32\WaitForDebugEventEx", Ptr,&DEBUG_EVENT, UInt,-1)
	;	break

	;source: minwinbase.h
	;EXCEPTION_DEBUG_EVENT := 1
	;CREATE_THREAD_DEBUG_EVENT := 2
	;CREATE_PROCESS_DEBUG_EVENT := 3
	;EXIT_THREAD_DEBUG_EVENT := 4
	;EXIT_PROCESS_DEBUG_EVENT := 5
	;LOAD_DLL_DEBUG_EVENT := 6
	;UNLOAD_DLL_DEBUG_EVENT := 7
	;OUTPUT_DEBUG_STRING_EVENT := 8
	;RIP_EVENT := 9

	vEventCode := NumGet(&DEBUG_EVENT, 0, "UInt") ;dwDebugEventCode
	vPID := NumGet(&DEBUG_EVENT, 4, "UInt") ;dwProcessId
 	vTID := NumGet(&DEBUG_EVENT, 8, "UInt") ;dwThreadId
	;DBG_EXCEPTION_NOT_HANDLED := 0x80010001
	if !DllCall("kernel32\ContinueDebugEvent", UInt,vPID, UInt,vTID, UInt,0x80010001)
		break
	if (vEventCode = 8) ;OUTPUT_DEBUG_STRING_EVENT := 8
	{
		pText := NumGet(&DEBUG_EVENT, 12, "Ptr") ;lpDebugStringData
		vIsUnicode := NumGet(&DEBUG_EVENT, A_PtrSize=8?20:16, "UInt") ;fUnicode ;not working
		vLen := NumGet(&DEBUG_EVENT, A_PtrSize=8?22:18, "UInt") ;nDebugStringLength

		;PROCESS_QUERY_INFORMATION := 0x400 ;PROCESS_VM_READ := 0x10
		if !hProc := DllCall("kernel32\OpenProcess", UInt,0x410, Int,0, UInt,vPID, Ptr)
			continue
		VarSetCapacity(vText, vLen*2, 0)
		DllCall("kernel32\ReadProcessMemory", Ptr,hProc, Ptr,pText, Ptr,&vText, UPtr,vLen*2, Ptr,0)
		DllCall("kernel32\CloseHandle", Ptr,hProc)
		VarSetCapacity(vText, -1)
		MsgBox, % StrGet(&vText, "CP0")
		MsgBox, % Format("{} {} {} {} {}", vPID, vEventCode, pText, vIsUnicode, vLen)
	}
}

;==================================================

;note: debug event structs and their x64/x32 sizes
;struct=x64size:x32size
;DEBUG_EVENT=176:96
;CREATE_PROCESS_DEBUG_INFO=72:40
;CREATE_THREAD_DEBUG_INFO=24:12
;EXCEPTION_DEBUG_INFO=160:84
;EXIT_PROCESS_DEBUG_INFO=4:4
;EXIT_THREAD_DEBUG_INFO=4:4
;LOAD_DLL_DEBUG_INFO=40:24
;OUTPUT_DEBUG_STRING_INFO=16:8
;RIP_INFO=8:8
;UNLOAD_DLL_DEBUG_INFO=8:4

;==================================================
Links:
Using the Windows Debugging API
http://www.howzatt.demon.co.uk/articles ... ugger.html

[contained 'AutoHotkey' and 'DebugActiveProcess']
Memory Process reading/Writing & Pattern Scans (Array of bytes) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=1177
Files/Debug.ahk at master · Lighnat0r/Files · GitHub
https://github.com/Lighnat0r/Files/blob ... /Debug.ahk
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: retrieve OutputDebug text

10 Nov 2018, 20:14

FYI, dbgp can also be used for this, though it's probably only worthwhile if you make use of its other capabilities as well. You can see it in action with dbgp_console.ahk:
  • Run dbgp_console.ahk.
  • Start a script with /debug (or post the AHK_ATTACH_DEBUGGER message to a script).
  • Execute stderr -c 1 (copy) or stderr -c 2 (redirect) at the console to enable OutputDebug copy/redirection.
  • Execute run to start the script.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: dipahk and 243 guests