-
-
[原创]复杂程序中的常用调试代码
-
发表于:
2012-12-1 14:44
5406
-
在我参加实际项目开发中,对于一些大型复杂的项目的调试往往感到力不从心。即使vs或者gdb已经足够强大,在一些并发的程序或者让人头痛的多线程程序中,你要去设置个断点来调试往往是噩梦的开始。实践告知我们,这个时候用print往往返璞归真成为最有效的调试手段,但限于其功能的局限型,我们可以单独写一些工具宏(函数),给我们调试带来便利。
例如在我们一个大型gis平台中(win32、wince)常用下列工具来调试:
#ifndef DEBUG_INFO
#ifdef _DEBUG
#ifdef WINCE
#define DEBUG_INFO(info) /
{ /
ConsoleInfoOut(info) /
} /
#define ConsoleInfoOut(info) /
{ /
wstring m_output_info; /
wostringstream iss; /
iss << info; /
m_output_info = iss.str(); /
OutputDebugStringW(m_output_info.c_str()); /
} /
//OutputDebugStringW(L"/n"); /
#define DEBUG_INFO_MEMORY(info) /
{ /
DEBUG_INFO(info); /
MEMORYSTATUS mst; /
mst.dwLength = sizeof(MEMORYSTATUS); /
GlobalMemoryStatus(&mst); /
OutputDebugStringW(L"total phys: "); /
DEBUG_INFO(mst.dwTotalPhys); /
OutputDebugStringW(L" avail phys:"); /
DEBUG_INFO(mst.dwAvailPhys); /
OutputDebugStringW(L" total virtual:"); /
DEBUG_INFO(mst.dwTotalVirtual); /
OutputDebugStringW(L" avail virtual:"); /
DEBUG_INFO(mst.dwAvailVirtual); /
} /
#else // WINCE
#define DEBUG_INFO(info) std::cerr << info << endl;
#endif // DEBUG
#else
#define DEBUG_INFO(info)
#endif // #ifdef _DEBUG
还有一些调试内存等信息的代码:
#ifdef _LOG_INFO
#ifndef LOG_INFO
#define LOG_INFO(info) /
{ /
ostringstream oss; /
oss.precision(12); /
oss << info; /
error_out.log(oss.str()); /
oss.str(""); /
}
#endif
#ifndef LOG_INFO_ENDL
#define LOG_INFO_ENDL(info) /
{ /
LOG_INFO(info) /
LOG_INFO("/n") /
}
#endif
#ifndef LOG_FLUSH
#define LOG_FLUSH() error_out.flush()
#endif
#ifndef LOG_INFO_CUR_TIME
#define LOG_INFO_CUR_TIME() /
{ /
SYSTEMTIME sys_time; /
GetLocalTime(&sys_time); /
char str_time[30]; /
_snprintf(str_time,30,"%u-%u-%u, %u:%u:%u", sys_time.wYear,sys_time.wMonth, sys_time.wDay, /
sys_time.wHour,sys_time.wMinute, sys_time.wSecond); /
LOG_INFO_ENDL(str_time); /
}
#endif
#ifndef LOG_TIME_START
#define LOG_TIME_START(info) /
LOG_INFO(info); /
DWORD s_clock = GetTickCount();
#endif
#ifndef LOG_TIME_START_2
#define LOG_TIME_START_2(info) /
LOG_INFO(info); /
s_clock = GetTickCount();
#endif
#ifndef LOG_TIME_END
#define LOG_TIME_END() /
DWORD e_clock = GetTickCount(); /
double duration = (e_clock - s_clock) / (double)1000; /
LOG_INFO_ENDL(duration);
#endif
#ifndef LOG_TIME_END_2
#define LOG_TIME_END_2() /
e_clock = GetTickCount(); /
duration = (e_clock - s_clock) / (double)1000; /
LOG_INFO_ENDL(duration);
#endif
#ifndef LOG_MEMORY
#define LOG_MEMORY(hint) /
{ /
LOG_INFO(hint); /
MEMORYSTATUS mst; /
mst.dwLength = sizeof(MEMORYSTATUS); /
GlobalMemoryStatus(&mst); /
error_out.logMem( mst); /
}
#endif
如果大家有什么更好的可以共享出来探讨探讨哈~
[课程]Linux pwn 探索篇!