程序调试

 

思路:

 

      1. 模块分类

      2. 即时调试:调试时改变错误变量的值,继续调试,完成后,再修改代码

      3. 使用高级断点

      4.使用打印、error log输出

 

具体方法:

 

1.         使用Assert(原则:尽量简单)

assert只在debug下生效,release下不会被编译。

 

2.         使用Trace

以下的例子只能在debug中显示,

例子:

a)         TRACE

CString csTest test”;

TRACE(“CString is s/n”,csTest);

b)        ATLTRACE

c)        afxDump

CTime time = CTime::GetCurrentTime();

#ifdef _DEBUG

afxDump << time << “/n”;

#endif

 

3.         GetLastError:来检测返回值,通过得到错误代码来分析错误原因

4.     Error log:把错误信息记录到文件中

 

 

另:VC watch窗口变量查看技巧(选自MSDN)

 

Symbols for Watch Variables

 

You can change the display format of variables in the QuickWatch dialog box or in the Watch window using the formatting symbols in the following table.

SymbolFormatValueDisplays
d,isigned decimal integer0xF000F065-268373915
uunsigned decimal integer0x0065101
ounsigned octal integer0xF0650170145
x,XHexadecimal integer61541 (decimal)0x0000F065
l,hlong or short prefix for: d, i, u, o, x, X00406042,hx0x0c22
fsigned floating-point3./2.1.500000
esigned scientific notation3./2.1.500000e+000
gsigned floating-point or signed scientific notation, whichever is shorter3./2.1.5
cSingle character0x0065'e'
sString0x0012fde8“Hello world”
suUnicode string“Hello world”
stUnicode string or ANSI string, depending on Unicode Strings setting in autoexp.dat.
hrHRESULT or Win32 error code.0x00000000LS_OK
wcWindow class flag.0x00000040 WC_DEFAULTCHAR
wmWindows message numbers0x0010WM_CLOSE

To use a formatting symbol, type the variable name, followed by a comma and the appropriate symbol. For example, if var has a value of 0x0065, and you want to see the value in character form, type var,c in the Name column on the tab of the Watch window. When you press ENTER, the character-format value appears:

var,c = 'e'

You can use the formatting symbols shown in the following table to format the contents of memory locations.

SymbolFormatDisplays
ma64 ASCII characters0x0012ffac .4...0...".0W&.......1W&.0.:W..1...."..1.JO&.1.2.."..1...0y....1
m16 bytes in hexadecimal, followed by 16 ASCII characters 0x0012ffac B3 34 CB 00 84 30 94 80 FF 22 8A 30 57 26 00 00 .4...0...".0W&..
mb16 bytes in hexadecimal, followed by 16 ASCII characters 0x0012ffac B3 34 CB 00 84 30 94 80 FF 22 8A 30 57 26 00 00 .4...0...".0W&..
mw8 words0x0012ffac 34B3 00CB 3084 8094 22FF 308A 2657 0000
md4 doublewords0x0012ffac 00CB34B3 80943084 308A22FF 00002657
mq4 quadwords0x0012ffac 7ffdf00000000000 5f441a790012fdd4
mu2-byte characters (Unicode)0x0012fc60 8478 77f4 ffff ffff 0000 0000 0000 0000

With the memory location formatting symbols, you can type any value or expression that evaluates to a location.

To display the value of a character array as a string, precede the array name with an ampersand (&):

&yourname

A formatting character can follow an expression also:

rep+1,x
alps[0],mb
xloc,g
count,d

To watch the value at an address or the value pointed to by a register, use the BY, WO, or DW operator.

  • BY returns the contents of the byte pointed to.

  • WO returns the contents of the word pointed to.

  • DW returns the contents of the doubleword pointed to.

Follow the operator with a variable, register, or constant. If the BY, WO, or DW operator is followed by a variable, then the environment watches the byte, word, or doubleword at the address contained in the variable.

You can also use the context operator { } to display the contents of any location.

To display a Unicode string in the Watch window or the QuickWatch dialog box, use the su format specifier. To display data bytes with Unicode characters in the Watch window or the QuickWatch dialog box, use the mu format specifier.

Note   You can apply formatting symbols to structures, arrays, pointers, and objects as unexpanded variables only. If you expand the variable, the specified formatting affects all members. You cannot apply formatting symbols to individual members.

 

 

 

 

比如代码里多写一些断言和日志,可以辅助你更快的定位问题,不是所有bug都需要跟踪调试才能解决的,应该大部分bug是可以通过读代码和其他辅助线索帮助下定位的,还有就是解决问题的思路问题,跟踪调试只是山穷水尽的最后一步,其实之前还是有很多方法的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值