Skip to content

Commit 259ad41

Browse files
authored
Maintenace + CI: Enable debug logging on 'Debug' build configs (microsoft#175)
This enables 'Debug' build configs to build the debug output and breakpoints, and fixes all mistakes that slipped in printf format specifiers.
1 parent 7f33ae3 commit 259ad41

File tree

8 files changed

+90
-80
lines changed

8 files changed

+90
-80
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
matrix:
2323
os: [windows-2019, windows-2016]
2424
arch: [x86, x64, x64_arm, x64_arm64]
25+
conf: [Release, Debug]
2526

2627
steps:
2728
- name: Clone Repository
@@ -44,10 +45,13 @@ jobs:
4445
env:
4546
# Tell detours what process to target
4647
DETOURS_TARGET_PROCESSOR: ${{ env.VSCMD_ARG_TGT_ARCH }}
48+
DETOURS_CONFIG: ${{ matrix.conf }}
4749
run: nmake
4850

4951
- name: Run unit tests for ${{ matrix.arch }} on ${{ matrix.os }}
5052
id: run-unit-tests
53+
env:
54+
DETOURS_CONFIG: ${{ matrix.conf }}
5155
run: cd tests && nmake test
5256
if: ${{ matrix.arch == 'x86' || matrix.arch == 'x64' }}
5357

samples/common.mak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ CLIB=/MT
2323
!ENDIF
2424

2525
AFLAGS=/nologo /Zi /c /Fl
26-
CFLAGS=/nologo /Zi $(CLIB) /Gm- /W4 /WX /we4777 /we4800 /Od
26+
CFLAGS=/nologo /Zi $(CLIB) /Gm- /W4 /WX /we4777 /we4800 /Od /DDETOUR_DEBUG=$(DETOURS_DEBUG)
2727

2828
!IF $(DETOURS_SOURCE_BROWSING)==1
2929
CFLAGS=$(CFLAGS) /FR

src/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ DETOURS_SOURCE_BROWSING = 0
1616

1717
#######################/#######################################################
1818
##
19-
CFLAGS=/nologo /W4 /WX /we4777 /we4800 /Zi /MT /Gy /Gm- /Zl /Od
19+
CFLAGS=/nologo /W4 /WX /we4777 /we4800 /Zi /MT /Gy /Gm- /Zl /Od /DDETOUR_DEBUG=$(DETOURS_DEBUG)
2020

2121
!IF $(DETOURS_SOURCE_BROWSING)==1
2222
CFLAGS=$(CFLAGS) /FR

src/creatwth.cpp

Lines changed: 40 additions & 40 deletions
Large diffs are not rendered by default.

src/detours.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ inline void detour_find_jmp_bounds(PBYTE pbCode,
186186
// We have to place trampolines within +/- 2GB of code.
187187
ULONG_PTR lo = detour_2gb_below((ULONG_PTR)pbCode);
188188
ULONG_PTR hi = detour_2gb_above((ULONG_PTR)pbCode);
189-
DETOUR_TRACE(("[%p..%p..%p]\n", lo, pbCode, hi));
189+
DETOUR_TRACE(("[%p..%p..%p]\n", (PVOID)lo, pbCode, (PVOID)hi));
190190

191191
// And, within +/- 2GB of relative jmp targets.
192192
if (pbCode[0] == 0xe9) { // jmp +imm32
@@ -198,7 +198,7 @@ inline void detour_find_jmp_bounds(PBYTE pbCode,
198198
else {
199199
lo = detour_2gb_below((ULONG_PTR)pbNew);
200200
}
201-
DETOUR_TRACE(("[%p..%p..%p] +imm32\n", lo, pbCode, hi));
201+
DETOUR_TRACE(("[%p..%p..%p] +imm32\n", (PVOID)lo, pbCode, (PVOID)hi));
202202
}
203203

204204
*ppLower = (PDETOUR_TRAMPOLINE)lo;
@@ -399,7 +399,7 @@ inline void detour_find_jmp_bounds(PBYTE pbCode,
399399
// We have to place trampolines within +/- 2GB of code.
400400
ULONG_PTR lo = detour_2gb_below((ULONG_PTR)pbCode);
401401
ULONG_PTR hi = detour_2gb_above((ULONG_PTR)pbCode);
402-
DETOUR_TRACE(("[%p..%p..%p]\n", lo, pbCode, hi));
402+
DETOUR_TRACE(("[%p..%p..%p]\n", (PVOID)lo, pbCode, (PVOID)hi));
403403

404404
// And, within +/- 2GB of relative jmp vectors.
405405
if (pbCode[0] == 0xff && pbCode[1] == 0x25) { // jmp [+imm32]
@@ -411,7 +411,7 @@ inline void detour_find_jmp_bounds(PBYTE pbCode,
411411
else {
412412
lo = detour_2gb_below((ULONG_PTR)pbNew);
413413
}
414-
DETOUR_TRACE(("[%p..%p..%p] [+imm32]\n", lo, pbCode, hi));
414+
DETOUR_TRACE(("[%p..%p..%p] [+imm32]\n", (PVOID)lo, pbCode, (PVOID)hi));
415415
}
416416
// And, within +/- 2GB of relative jmp targets.
417417
else if (pbCode[0] == 0xe9) { // jmp +imm32
@@ -423,7 +423,7 @@ inline void detour_find_jmp_bounds(PBYTE pbCode,
423423
else {
424424
lo = detour_2gb_below((ULONG_PTR)pbNew);
425425
}
426-
DETOUR_TRACE(("[%p..%p..%p] +imm32\n", lo, pbCode, hi));
426+
DETOUR_TRACE(("[%p..%p..%p] +imm32\n", (PVOID)lo, pbCode, (PVOID)hi));
427427
}
428428

429429
*ppLower = (PDETOUR_TRAMPOLINE)lo;
@@ -818,7 +818,7 @@ inline void detour_find_jmp_bounds(PBYTE pbCode,
818818
// We have to place trampolines within +/- 2GB of code.
819819
ULONG_PTR lo = detour_2gb_below((ULONG_PTR)pbCode);
820820
ULONG_PTR hi = detour_2gb_above((ULONG_PTR)pbCode);
821-
DETOUR_TRACE(("[%p..%p..%p]\n", lo, pbCode, hi));
821+
DETOUR_TRACE(("[%p..%p..%p]\n", (PVOID)lo, pbCode, (PVOID)hi));
822822

823823
*ppLower = (PDETOUR_TRAMPOLINE)lo;
824824
*ppUpper = (PDETOUR_TRAMPOLINE)hi;
@@ -1124,7 +1124,7 @@ inline void detour_find_jmp_bounds(PBYTE pbCode,
11241124

11251125
ULONG_PTR lo = detour_2gb_below((ULONG_PTR)pbCode);
11261126
ULONG_PTR hi = detour_2gb_above((ULONG_PTR)pbCode);
1127-
DETOUR_TRACE(("[%p..%p..%p]\n", lo, pbCode, hi));
1127+
DETOUR_TRACE(("[%p..%p..%p]\n", (PVOID)lo, pbCode, (PVOID)hi));
11281128

11291129
*ppLower = (PDETOUR_TRAMPOLINE)lo;
11301130
*ppUpper = (PDETOUR_TRAMPOLINE)hi;
@@ -1237,7 +1237,7 @@ static PVOID detour_alloc_region_from_lo(PBYTE pbLo, PBYTE pbHi)
12371237
break;
12381238
}
12391239

1240-
DETOUR_TRACE((" Try %p => %p..%p %6x\n",
1240+
DETOUR_TRACE((" Try %p => %p..%p %6lx\n",
12411241
pbTry,
12421242
mbi.BaseAddress,
12431243
(PBYTE)mbi.BaseAddress + mbi.RegionSize - 1,
@@ -1287,7 +1287,7 @@ static PVOID detour_alloc_region_from_hi(PBYTE pbLo, PBYTE pbHi)
12871287
break;
12881288
}
12891289

1290-
DETOUR_TRACE((" Try %p => %p..%p %6x\n",
1290+
DETOUR_TRACE((" Try %p => %p..%p %6lx\n",
12911291
pbTry,
12921292
mbi.BaseAddress,
12931293
(PBYTE)mbi.BaseAddress + mbi.RegionSize - 1,
@@ -1704,7 +1704,7 @@ LONG WINAPI DetourTransactionCommitEx(_Out_opt_ PVOID **pppFailedPointer)
17041704
#endif // DETOURS_ARM
17051705
}
17061706
else {
1707-
DETOUR_TRACE(("detours: pbTramp =%p, pbRemain=%p, pbDetour=%p, cbRestore=%d\n",
1707+
DETOUR_TRACE(("detours: pbTramp =%p, pbRemain=%p, pbDetour=%p, cbRestore=%x\n",
17081708
o->pTrampoline,
17091709
o->pTrampoline->pbRemain,
17101710
o->pTrampoline->pbDetour,
@@ -1996,13 +1996,13 @@ LONG WINAPI DetourAttachEx(_Inout_ PVOID *ppPointer,
19961996
}
19971997

19981998
if (s_nPendingThreadId != (LONG)GetCurrentThreadId()) {
1999-
DETOUR_TRACE(("transaction conflict with thread id=%d\n", s_nPendingThreadId));
1999+
DETOUR_TRACE(("transaction conflict with thread id=%ld\n", s_nPendingThreadId));
20002000
return ERROR_INVALID_OPERATION;
20012001
}
20022002

20032003
// If any of the pending operations failed, then we don't need to do this.
20042004
if (s_nPendingError != NO_ERROR) {
2005-
DETOUR_TRACE(("pending transaction error=%d\n", s_nPendingError));
2005+
DETOUR_TRACE(("pending transaction error=%ld\n", s_nPendingError));
20062006
return s_nPendingError;
20072007
}
20082008

@@ -2183,7 +2183,7 @@ LONG WINAPI DetourAttachEx(_Inout_ PVOID *ppPointer,
21832183
pTrampoline->rAlign[n].obTrampoline == 0) {
21842184
break;
21852185
}
2186-
DETOUR_TRACE((" %d/%d",
2186+
DETOUR_TRACE((" %x/%x",
21872187
pTrampoline->rAlign[n].obTarget,
21882188
pTrampoline->rAlign[n].obTrampoline
21892189
));

src/modules.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ PVOID WINAPI DetourFindFunction(_In_ LPCSTR pszModule,
160160
DETOUR_TRACE(("DetourFindFunction(%hs, %hs)\n", pszModule, pszFunction));
161161
PDETOUR_SYM_INFO pSymInfo = DetourLoadImageHlp();
162162
if (pSymInfo == NULL) {
163-
DETOUR_TRACE(("DetourLoadImageHlp failed: %d\n",
163+
DETOUR_TRACE(("DetourLoadImageHlp failed: %lx\n",
164164
GetLastError()));
165165
return NULL;
166166
}
@@ -169,7 +169,7 @@ PVOID WINAPI DetourFindFunction(_In_ LPCSTR pszModule,
169169
(PCHAR)pszModule, NULL,
170170
(DWORD64)hModule, 0) == 0) {
171171
if (ERROR_SUCCESS != GetLastError()) {
172-
DETOUR_TRACE(("SymLoadModule64(%p) failed: %d\n",
172+
DETOUR_TRACE(("SymLoadModule64(%p) failed: %lx\n",
173173
pSymInfo->hProcess, GetLastError()));
174174
return NULL;
175175
}
@@ -181,24 +181,24 @@ PVOID WINAPI DetourFindFunction(_In_ LPCSTR pszModule,
181181
ZeroMemory(&modinfo, sizeof(modinfo));
182182
modinfo.SizeOfStruct = sizeof(modinfo);
183183
if (!pSymInfo->pfSymGetModuleInfo64(pSymInfo->hProcess, (DWORD64)hModule, &modinfo)) {
184-
DETOUR_TRACE(("SymGetModuleInfo64(%p, %p) failed: %d\n",
184+
DETOUR_TRACE(("SymGetModuleInfo64(%p, %p) failed: %lx\n",
185185
pSymInfo->hProcess, hModule, GetLastError()));
186186
return NULL;
187187
}
188188

189189
hrRet = StringCchCopyA(szFullName, sizeof(szFullName)/sizeof(CHAR), modinfo.ModuleName);
190190
if (FAILED(hrRet)) {
191-
DETOUR_TRACE(("StringCchCopyA failed: %08x\n", hrRet));
191+
DETOUR_TRACE(("StringCchCopyA failed: %08lx\n", hrRet));
192192
return NULL;
193193
}
194194
hrRet = StringCchCatA(szFullName, sizeof(szFullName)/sizeof(CHAR), "!");
195195
if (FAILED(hrRet)) {
196-
DETOUR_TRACE(("StringCchCatA failed: %08x\n", hrRet));
196+
DETOUR_TRACE(("StringCchCatA failed: %08lx\n", hrRet));
197197
return NULL;
198198
}
199199
hrRet = StringCchCatA(szFullName, sizeof(szFullName)/sizeof(CHAR), pszFunction);
200200
if (FAILED(hrRet)) {
201-
DETOUR_TRACE(("StringCchCatA failed: %08x\n", hrRet));
201+
DETOUR_TRACE(("StringCchCatA failed: %08lx\n", hrRet));
202202
return NULL;
203203
}
204204

@@ -215,7 +215,7 @@ PVOID WINAPI DetourFindFunction(_In_ LPCSTR pszModule,
215215
#endif
216216

217217
if (!pSymInfo->pfSymFromName(pSymInfo->hProcess, szFullName, &symbol)) {
218-
DETOUR_TRACE(("SymFromName(%hs) failed: %d\n", szFullName, GetLastError()));
218+
DETOUR_TRACE(("SymFromName(%hs) failed: %lx\n", szFullName, GetLastError()));
219219
return NULL;
220220
}
221221

src/uimports.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
3535
if (!ReadProcessMemory(hProcess, pbModule, &idh, sizeof(idh), &cbRead)
3636
|| cbRead < sizeof(idh)) {
3737

38-
DETOUR_TRACE(("ReadProcessMemory(idh@%p..%p) failed: %d\n",
38+
DETOUR_TRACE(("ReadProcessMemory(idh@%p..%p) failed: %lx\n",
3939
pbModule, pbModule + sizeof(idh), GetLastError()));
4040

4141
finish:
@@ -51,7 +51,7 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
5151

5252
if (!ReadProcessMemory(hProcess, pbModule + idh.e_lfanew, &inh, sizeof(inh), &cbRead)
5353
|| cbRead < sizeof(inh)) {
54-
DETOUR_TRACE(("ReadProcessMemory(inh@%p..%p) failed: %d\n",
54+
DETOUR_TRACE(("ReadProcessMemory(inh@%p..%p) failed: %lx\n",
5555
pbModule + idh.e_lfanew,
5656
pbModule + idh.e_lfanew + sizeof(inh),
5757
GetLastError()));
@@ -82,14 +82,14 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
8282
sizeof(ish), &cbRead)
8383
|| cbRead < sizeof(ish)) {
8484

85-
DETOUR_TRACE(("ReadProcessMemory(ish@%p..%p) failed: %d\n",
85+
DETOUR_TRACE(("ReadProcessMemory(ish@%p..%p) failed: %lx\n",
8686
pbModule + dwSec + sizeof(ish) * i,
8787
pbModule + dwSec + sizeof(ish) * (i + 1),
8888
GetLastError()));
8989
goto finish;
9090
}
9191

92-
DETOUR_TRACE(("ish[%d] : va=%08x sr=%d\n", i, ish.VirtualAddress, ish.SizeOfRawData));
92+
DETOUR_TRACE(("ish[%lx] : va=%08lx sr=%lx\n", i, ish.VirtualAddress, ish.SizeOfRawData));
9393

9494
// If the linker didn't suggest an IAT in the data directories, the
9595
// loader will look for the section of the import directory to be used
@@ -117,7 +117,7 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
117117
do {
118118
IMAGE_IMPORT_DESCRIPTOR ImageImport;
119119
if (!ReadProcessMemory(hProcess, pImageImport, &ImageImport, sizeof(ImageImport), NULL)) {
120-
DETOUR_TRACE(("ReadProcessMemory failed: %u\n", GetLastError()));
120+
DETOUR_TRACE(("ReadProcessMemory failed: %lx\n", GetLastError()));
121121
goto finish;
122122
}
123123
inh.IMPORT_DIRECTORY.Size += sizeof(IMAGE_IMPORT_DESCRIPTOR);
@@ -136,8 +136,8 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
136136
}
137137

138138
DETOUR_TRACE((" Imports: %p..%p\n",
139-
(DWORD_PTR)pbModule + inh.IMPORT_DIRECTORY.VirtualAddress,
140-
(DWORD_PTR)pbModule + inh.IMPORT_DIRECTORY.VirtualAddress +
139+
pbModule + inh.IMPORT_DIRECTORY.VirtualAddress,
140+
pbModule + inh.IMPORT_DIRECTORY.VirtualAddress +
141141
inh.IMPORT_DIRECTORY.Size));
142142

143143
DWORD nOldDlls = inh.IMPORT_DIRECTORY.Size / sizeof(IMAGE_IMPORT_DESCRIPTOR);
@@ -186,23 +186,23 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
186186

187187
if (inh.IMPORT_DIRECTORY.VirtualAddress != 0) {
188188
// Read the old import directory if it exists.
189-
DETOUR_TRACE(("IMPORT_DIRECTORY perms=%x\n", dwProtect));
189+
DETOUR_TRACE(("IMPORT_DIRECTORY perms=%lx\n", dwProtect));
190190

191191
if (!ReadProcessMemory(hProcess,
192192
pbModule + inh.IMPORT_DIRECTORY.VirtualAddress,
193193
&piid[nDlls],
194194
nOldDlls * sizeof(IMAGE_IMPORT_DESCRIPTOR), &cbRead)
195195
|| cbRead < nOldDlls * sizeof(IMAGE_IMPORT_DESCRIPTOR)) {
196196

197-
DETOUR_TRACE(("ReadProcessMemory(imports) failed: %d\n", GetLastError()));
197+
DETOUR_TRACE(("ReadProcessMemory(imports) failed: %lx\n", GetLastError()));
198198
goto finish;
199199
}
200200
}
201201

202202
for (n = 0; n < nDlls; n++) {
203203
HRESULT hrRet = StringCchCopyA((char*)pbNew + obStr, cbNew - obStr, plpDlls[n]);
204204
if (FAILED(hrRet)) {
205-
DETOUR_TRACE(("StringCchCopyA failed: %d\n", GetLastError()));
205+
DETOUR_TRACE(("StringCchCopyA failed: %lx\n", GetLastError()));
206206
goto finish;
207207
}
208208

@@ -211,7 +211,7 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
211211
cbNew - obStr,
212212
DETOURS_STRINGIFY(DETOURS_BITS_XX));
213213
if (FAILED(hrRet)) {
214-
DETOUR_TRACE(("ReplaceOptionalSizeA failed: %d\n", GetLastError()));
214+
DETOUR_TRACE(("ReplaceOptionalSizeA failed: %lx\n", GetLastError()));
215215
goto finish;
216216
}
217217

@@ -253,14 +253,14 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
253253
#endif
254254

255255
if (!WriteProcessMemory(hProcess, pbNewIid, pbNew, obStr, NULL)) {
256-
DETOUR_TRACE(("WriteProcessMemory(iid) failed: %d\n", GetLastError()));
256+
DETOUR_TRACE(("WriteProcessMemory(iid) failed: %lx\n", GetLastError()));
257257
goto finish;
258258
}
259259

260-
DETOUR_TRACE(("obBaseBef = %08x..%08x\n",
260+
DETOUR_TRACE(("obBaseBef = %08lx..%08lx\n",
261261
inh.IMPORT_DIRECTORY.VirtualAddress,
262262
inh.IMPORT_DIRECTORY.VirtualAddress + inh.IMPORT_DIRECTORY.Size));
263-
DETOUR_TRACE(("obBaseAft = %08x..%08x\n", obBase, obBase + obStr));
263+
DETOUR_TRACE(("obBaseAft = %08lx..%08lx\n", obBase, obBase + obStr));
264264

265265
// In this case the file didn't have an import directory in first place,
266266
// so we couldn't fix the missing IAT above. We still need to explicitly
@@ -278,20 +278,20 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
278278
//
279279
if (!DetourVirtualProtectSameExecuteEx(hProcess, pbModule, inh.OptionalHeader.SizeOfHeaders,
280280
PAGE_EXECUTE_READWRITE, &dwProtect)) {
281-
DETOUR_TRACE(("VirtualProtectEx(inh) write failed: %d\n", GetLastError()));
281+
DETOUR_TRACE(("VirtualProtectEx(inh) write failed: %lx\n", GetLastError()));
282282
goto finish;
283283
}
284284

285285
inh.OptionalHeader.CheckSum = 0;
286286

287287
if (!WriteProcessMemory(hProcess, pbModule, &idh, sizeof(idh), NULL)) {
288-
DETOUR_TRACE(("WriteProcessMemory(idh) failed: %d\n", GetLastError()));
288+
DETOUR_TRACE(("WriteProcessMemory(idh) failed: %lx\n", GetLastError()));
289289
goto finish;
290290
}
291291
DETOUR_TRACE(("WriteProcessMemory(idh:%p..%p)\n", pbModule, pbModule + sizeof(idh)));
292292

293293
if (!WriteProcessMemory(hProcess, pbModule + idh.e_lfanew, &inh, sizeof(inh), NULL)) {
294-
DETOUR_TRACE(("WriteProcessMemory(inh) failed: %d\n", GetLastError()));
294+
DETOUR_TRACE(("WriteProcessMemory(inh) failed: %lx\n", GetLastError()));
295295
goto finish;
296296
}
297297
DETOUR_TRACE(("WriteProcessMemory(inh:%p..%p)\n",
@@ -300,7 +300,7 @@ static BOOL UPDATE_IMPORTS_XX(HANDLE hProcess,
300300

301301
if (!VirtualProtectEx(hProcess, pbModule, inh.OptionalHeader.SizeOfHeaders,
302302
dwProtect, &dwProtect)) {
303-
DETOUR_TRACE(("VirtualProtectEx(idh) restore failed: %d\n", GetLastError()));
303+
DETOUR_TRACE(("VirtualProtectEx(idh) restore failed: %lx\n", GetLastError()));
304304
goto finish;
305305
}
306306

system.mak

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ DETOURS_OPTION_BITS=64
100100

101101
##############################################################################
102102
##
103+
!IF "$(DETOURS_CONFIG)" == "Debug"
104+
DETOURS_DEBUG=1
105+
!ELSE
106+
DETOURS_DEBUG=0
107+
!ENDIF
108+
103109
INCD = $(ROOT)\include
104110
LIBD = $(ROOT)\lib.$(DETOURS_TARGET_PROCESSOR)$(DETOURS_CONFIG)
105111
BIND = $(ROOT)\bin.$(DETOURS_TARGET_PROCESSOR)$(DETOURS_CONFIG)

0 commit comments

Comments
 (0)