• 调试小技巧


    “Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
    — Brian W. Kernighan


    1. 中断进入调试器
    1.1 c++
    _asm { int 3 }

    一个宏( macro )

    #ifdef DEBUG
    #define BREAKPOINT __asm int3
    #else
    #define BREAKPOINT
    #endif

    1.2 也可使用 _CrtDbgBreak() ,在crtdbg.h中定义

    1.3 所有平台(x86、x64均可用)  __debugbreak() ,定义于intrin.h

    1.4 csharp
    System.Diagnostics.Debugger.Break();

    1.5 其他
    可用NULL指针、除零操作等。

    2. 定义自己的 assert 宏

    void __declspec(noreturn) __assertNoReturn(char *, int, char *);

    #if defined(_PREFAST_)
    // For PREfast runs
    //
       #define myAssert(x) __assume(x)

    #elif defined(DBG)
    //
    // For checked builds with a simple assert mechanism
    // (where DBG is the checked build symbol)
    //
    // The following function call will not cause false-positive
    // results because __assertNoReturn will not return. More complex
    // macros might return. In that case, use __assume.
    //
       #define myAssert(x) ((!(x))?__assertNoReturn(__FILE__, __LINE__, #x):1)
    #else
    // Free build
       #define myAssert(x)   // Nothing
    #endif

    3. 调试常用快捷键(VS)
    F5 开始程序并进行调试
    F10 step over, 逐行调试
    F11 step into
    shift + F11 step out
    ctrl + shift + F10 ,set next statement
    CTRL +* : SHOW NEWXT STATEMENT
    条件断点:一个例子 name.Equals("Name3")
    设置条件断点后,断点符号中间会有一个加号
    在VS2010中,断点可以导入、输出
    使用 Breakpoint Hit Count

    4. 得到当前文件名称和当前行
    (.net charp)c# 中的 __File__ __line__
    string currentFile=new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName();
    int currentLine = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileLineNumber(); 

    String MethodName = new System.Diagnostics.StackTrace().GetFrame(0).GetMethod().Name;
    

    MethodBase.GetCurrentMethod().Name
    
    
    MethodInfo.GetCurrentMethod().Name
    
    
    [Conditional("DEBUG")]
    void DebugFunc()
    {
    } 


    调试时一些有用的快捷键(VS2010)

    Shortcut Keys

    Descriptions

    Ctrl-Alt-V, A

    Displays the Auto window

    Ctrl-Alt-B

    Displays the Breakpoints dialog

    Ctrl-Alt-C

    Displays the Call Stack

    Ctrl-Shift-F9

    Clears all of the breakpoints in the project

    Ctrl-F9

    Enables or disables the breakpoint on the current line of code

    Ctrl-Alt-E

    Displays the Exceptions dialog

    Ctrl-Alt-I

    Displays the Immediate window

    Ctrl-Alt-V, L

    Displays the Locals window

    Ctrl-Alt-Q

    Displays the Quick Watch dialog

    Ctrl-Shift-F5

    Terminates the current debugging session, rebuilds if necessary, and starts a new debugging session.

    Ctrl-F10

    Starts or resumes execution of your code and then halts execution when it reaches the selected statement.

    Ctrl-Shift-F10

    Sets the execution point to the line of code you choose

    Alt-NUM *

    Highlights the next statement

    F5

    If not currently debugging, this runs the startup project or projects and attaches the debugger.

    Ctrl-F5

    Runs the code without invoking the debugger

    F11

    Step Into

    Shift-F11

    Executes the remaining lines out from procedure

    F10

    Executes the next line of code but does not step into any function calls

    Shift-F5

    Available in break and run modes, this terminates the debugging session

    Ctrl-Alt-H

    Displays the Threads window to view all of the threads for the current process

    F9

    Sets or removes a breakpoint at the current line

    Ctrl-Alt-W, 1

    Displays the Watch 1 window to view the values of variables or watch expressions

    Ctrl-Alt-P

    Displays the Processes dialog, which allows you to attach or detach the debugger to one or more running processes

    Ctrl-D,V

    IntelliTrace Event


    6. 使用OutputDebugString
    vc中使用 OutputDebugString
    c#可用:
    debug中可见
    System.Diagnostics.Debug.WriteLine("I am using dot net debugging");
    release 和 debug中均可见
    System.Diagnostics.Trace.WriteLine("I am using dot net tracing");


    对话框
    System.Windows.Forms.MessageBox.Show("Exception at File:Line:\n" + ExceptionToStr(e));

    将输出重定向到文件:
    System.Diagnostics.Trace.Listeners.Clear();
    System.Diagnostics.Trace.AutoFlush=true;
    System.Diagnostics.Trace.Listeners.Add(new System.Diagnostics.TextWriterTraceListener("app.log"));

    //z 2013-04-28 10:23:21 IS2120@BG57IV3.T3865622795.K[T7,L117,R5,V108]
    7. vbscript 调试
    Dim objIEDebugWindow
    
    Debug "This is a great way to display intermediate results in a separate window."
    
    
    Sub Debug( myText )
      ' Uncomment the next line to turn off debugging
      ' Exit Sub
    
      If Not IsObject( objIEDebugWindow ) Then
        Set objIEDebugWindow = CreateObject( "InternetExplorer.Application" )
        objIEDebugWindow.Navigate "about:blank"
        objIEDebugWindow.Visible = True
        objIEDebugWindow.ToolBar = False
        objIEDebugWindow.Width   = 200
        objIEDebugWindow.Height  = 300
        objIEDebugWindow.Left    = 10
        objIEDebugWindow.Top     = 10
        Do While objIEDebugWindow.Busy
          WScript.Sleep 100
        Loop
        objIEDebugWindow.Document.Title = "IE Debug Window"
        objIEDebugWindow.Document.Body.InnerHTML = _
                     "<b>" & Now & "</b></br>"
      End If
    
      objIEDebugWindow.Document.Body.InnerHTML = _
                       objIEDebugWindow.Document.Body.InnerHTML _
                       & myText & "<br>" & vbCrLf
    End Sub
    Notes: (1) objIEDebugWindow must be declared in the main script body, not in the subroutine (must be global)!
      (2) Do not discard the objIEDebugWindow object at the end of the script, or your debug window will vanish!

  • 相关阅读:
    使用log4j在javaweb中实现日志管理
    android 开发:使用SwipeRefreshLayout实现下拉刷新
    android 开发:保存图片到SD卡上
    android 开发:网页爬虫获取腾讯财经滚动新闻
    android 开发:Json的发送和接收
    安卓学习笔记之新浪微博开发(一)
    android之webview使用
    Android 使WebView支持HTML5 Video(全屏)播放的方法
    委托
    八月的第一场雨
  • 原文地址:https://www.cnblogs.com/IS2120/p/6746005.html
Copyright © 2020-2023  润新知