1
0
Fork 0

DBG: fixed some bugs wirh printing the debug string + thread name + fixed SetThreadName

This commit is contained in:
Mr. eXoDia 2014-11-09 00:44:25 +01:00
parent 9c1e22800e
commit e7c15d2596
4 changed files with 58 additions and 56 deletions

View File

@ -115,6 +115,7 @@ void dbginit()
exceptionNames.insert(std::make_pair(0x04242420, "CLRDBG_NOTIFICATION_EXCEPTION_CODE"));
exceptionNames.insert(std::make_pair(0xE0434352, "CLR_EXCEPTION"));
exceptionNames.insert(std::make_pair(0xE06D7363, "CPP_EH_EXCEPTION"));
exceptionNames.insert(std::make_pair(MS_VC_EXCEPTION, "MS_VC_EXCEPTION"));
CloseHandle(CreateThread(0, 0, memMapThread, 0, 0, 0));
}
@ -1005,7 +1006,7 @@ static void cbOutputDebugString(OUTPUT_DEBUG_STRING_INFO* DebugString)
break;
}
}
dprintf("DebugString: \"%s\"\n", DebugTextEscaped);
dprintf("DebugString: \"%s\"\n", DebugTextEscaped());
}
}
@ -1066,63 +1067,58 @@ static void cbException(EXCEPTION_DEBUG_INFO* ExceptionData)
}
SetContextDataEx(hActiveThread, UE_CIP, (uint)ExceptionData->ExceptionRecord.ExceptionAddress);
}
else if(ExceptionData->ExceptionRecord.ExceptionCode == 0x406D1388) //SetThreadName exception
else if(ExceptionData->ExceptionRecord.ExceptionCode == MS_VC_EXCEPTION) //SetThreadName exception
{
if(ExceptionData->ExceptionRecord.NumberParameters == sizeof(THREADNAME_INFO) / sizeof(uint))
THREADNAME_INFO nameInfo;
memcpy(&nameInfo, ExceptionData->ExceptionRecord.ExceptionInformation, sizeof(THREADNAME_INFO));
if(nameInfo.dwThreadID == -1) //current thread
nameInfo.dwThreadID = ((DEBUG_EVENT*)GetDebugData())->dwThreadId;
if(nameInfo.dwType == 0x1000 and nameInfo.dwFlags == 0 and threadisvalid(nameInfo.dwThreadID)) //passed basic checks
{
THREADNAME_INFO nameInfo;
memcpy(&nameInfo, ExceptionData->ExceptionRecord.ExceptionInformation, sizeof(THREADNAME_INFO));
if(nameInfo.dwThreadID == -1) //current thread
nameInfo.dwThreadID = ((DEBUG_EVENT*)GetDebugData())->dwThreadId;
if(nameInfo.dwType == 0x1000 and nameInfo.dwFlags == 0 and threadisvalid(nameInfo.dwThreadID)) //passed basic checks
Memory<char*> ThreadName(MAX_THREAD_NAME_SIZE, "cbException:ThreadName");
if(memread(fdProcessInfo->hProcess, nameInfo.szName, ThreadName, MAX_THREAD_NAME_SIZE - 1, 0))
{
Memory<char*> ThreadName(MAX_THREAD_NAME_SIZE, "cbException:ThreadName");
memset(ThreadName, 0, MAX_THREAD_NAME_SIZE);
if(memread(fdProcessInfo->hProcess, nameInfo.szName, ThreadName, MAX_THREAD_NAME_SIZE - 1, 0))
int len = (int)strlen(ThreadName);
int escape_count = 0;
for(int i = 0; i < len; i++)
if(ThreadName[i] == '\\' or ThreadName[i] == '\"' or !isprint(ThreadName[i]))
escape_count++;
Memory<char*> ThreadNameEscaped(len + escape_count * 3 + 1, "cbException:ThreadNameEscaped");
for(int i = 0, j = 0; i < len; i++)
{
int len = (int)strlen(ThreadName);
int escape_count = 0;
for(int i = 0; i < len; i++)
if(ThreadName[i] == '\\' or ThreadName[i] == '\"' or !isprint(ThreadName[i]))
escape_count++;
Memory<char*> ThreadNameEscaped(len + escape_count * 3 + 1, "cbException:ThreadNameEscaped");
memset(ThreadNameEscaped, 0, len + escape_count * 3 + 1);
for(int i = 0, j = 0; i < len; i++)
switch(ThreadName[i])
{
switch(ThreadName[i])
{
case '\t':
j += sprintf(ThreadNameEscaped + j, "\\t");
break;
case '\f':
j += sprintf(ThreadNameEscaped + j, "\\f");
break;
case '\v':
j += sprintf(ThreadNameEscaped + j, "\\v");
break;
case '\n':
j += sprintf(ThreadNameEscaped + j, "\\n");
break;
case '\r':
j += sprintf(ThreadNameEscaped + j, "\\r");
break;
case '\\':
j += sprintf(ThreadNameEscaped + j, "\\\\");
break;
case '\"':
j += sprintf(ThreadNameEscaped + j, "\\\"");
break;
default:
if(!isprint(ThreadName[i])) //unknown unprintable character
j += sprintf(ThreadNameEscaped + j, "\\%.2x", ThreadName[i]);
else
j += sprintf(ThreadNameEscaped + j, "%c", ThreadName[i]);
break;
}
case '\t':
j += sprintf(ThreadNameEscaped + j, "\\t");
break;
case '\f':
j += sprintf(ThreadNameEscaped + j, "\\f");
break;
case '\v':
j += sprintf(ThreadNameEscaped + j, "\\v");
break;
case '\n':
j += sprintf(ThreadNameEscaped + j, "\\n");
break;
case '\r':
j += sprintf(ThreadNameEscaped + j, "\\r");
break;
case '\\':
j += sprintf(ThreadNameEscaped + j, "\\\\");
break;
case '\"':
j += sprintf(ThreadNameEscaped + j, "\\\"");
break;
default:
if(!isprint(ThreadName[i])) //unknown unprintable character
j += sprintf(ThreadNameEscaped + j, "\\%.2x", ThreadName[i]);
else
j += sprintf(ThreadNameEscaped + j, "%c", ThreadName[i]);
break;
}
dprintf("SetThreadName(%X, \"%s\")\n", nameInfo.dwThreadID, ThreadNameEscaped);
threadsetname(nameInfo.dwThreadID, ThreadNameEscaped);
}
dprintf("SetThreadName(%X, \"%s\")\n", nameInfo.dwThreadID, ThreadNameEscaped());
threadsetname(nameInfo.dwThreadID, ThreadNameEscaped);
}
}
}

View File

@ -12,6 +12,7 @@
#define JIT_ENTRY_DEF_SIZE (MAX_PATH + sizeof(ATTACH_CMD_LINE) + 2)
#define JIT_ENTRY_MAX_SIZE 512
#define JIT_REG_KEY TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug")
#define MS_VC_EXCEPTION 0x406D1388
typedef enum
{

View File

@ -199,17 +199,17 @@ CMDRESULT cbDebugSetBPX(int argc, char* argv[]) //bp addr [,name [,type]]
}
if(IsBPXEnabled(addr))
{
dprintf("error setting breakpoint at "fhex"!\n (IsBPXEnabled)", addr);
dprintf("error setting breakpoint at "fhex"! (IsBPXEnabled)\n", addr);
return STATUS_ERROR;
}
else if(!memread(fdProcessInfo->hProcess, (void*)addr, &oldbytes, sizeof(short), 0))
{
dprintf("error setting breakpoint at "fhex"!\n (memread)", addr);
dprintf("error setting breakpoint at "fhex"! (memread)\n", addr);
return STATUS_ERROR;
}
else if(!bpnew(addr, true, singleshoot, oldbytes, BPNORMAL, type, bpname))
{
dprintf("error setting breakpoint at "fhex"!\n (bpnew)", addr);
dprintf("error setting breakpoint at "fhex"! (bpnew)\n", addr);
return STATUS_ERROR;
}
else if(!SetBPX(addr, type, (void*)cbUserBreakpoint))

View File

@ -1,7 +1,7 @@
#ifndef _DYNAMICMEM_H
#define _DYNAMICMEM_H
template<class T>
template<typename T>
class Memory
{
public:
@ -34,7 +34,7 @@ public:
return mPtr;
}
template<class U>
template<typename U>
operator U()
{
return (U)mPtr;
@ -45,6 +45,11 @@ public:
return mPtr;
}
T operator()()
{
return mPtr;
}
size_t size()
{
return mSize;