search
HomeBackend DevelopmentC#.Net TutorialShare 10 debugging skills for Visual Studio native development (2)

The previous article about Visual Studio debugging skills aroused so much interest that I decided to share more debugging knowledge. In the following list you can see debugging tips for writing native development (numbered following the previous article). These tips can be applied to VS2005 or newer versions (of course some can be applied to older versions). If you continue, you can learn the details of each tip.

Tip 11: Data Breakpoint

When the memory location of the data changes, the debugger will break. However, it is only possible to create 4 data breakpoints at one time with such hardware. Data breakpoints can only be added during compilation, and can be added through the menu (Compile>New Breakpoint>New Data Breakpoint) or through the breakpoint window.

分享Visual Studio原生开发的10个调试技巧(2)

You can use a memory address or an address expression. Even though you can see both values ​​on the stack, I think this feature is usually only useful when the value on the heap has been changed. This is a great help in identifying memory corruption.

 In the following example, the value of the pointer has been changed to the value of the object pointed to. In order to find out where the changes were made, I set a breakpoint where the pointer value is stored, such as &ptr (note that this happens after the pointer is initialized). When the data changes, assuming someone changed the value of the pointer, the debugger terminates and can discover what code caused the change.

分享Visual Studio原生开发的10个调试技巧(2)

Tip 12: Thread Renaming

When you debug a multi-threaded application, the Threads window will show which threads were created and the current thread. The more threads there are, the harder it is for you to find the thread you are looking for (especially when the same program is executed by multiple threads at the same time, you don’t know which thread instance is currently executing)

分享Visual Studio原生开发的10个调试技巧(2)

The debugger allows you to rename threads. Right-click a thread and rename it.

分享Visual Studio原生开发的10个调试技巧(2)

It is also possible to name threads programmatically, although this is a bit tricky and must be done after the thread has been started, otherwise the debugger will rename it with its default naming convention. Initialization, the following function shows how to define and use a thread.

typedef struct tagTHREADNAME_INFO
{
  DWORD dwType;    // 必须是两个字节的长度
  LPCSTR szName;    // 指针指向命名 (同一个地址空间)
  DWORD dwThreadID;  // 线程ID(-1调用线程)
  DWORD dwFlags;    // 保留待用,多数情况下为0
} THREADNAME_INFO;
  
void SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
{
  THREADNAME_INFO info;
  info.dwType = 0x1000;
  info.szName = szThreadName;
  info.dwThreadID = dwThreadID;
  info.dwFlags = 0;
  
  __try
  {
    RaiseException(0x406D1388, 0, sizeof(info)/sizeof(DWORD), (DWORD*)&info);
  }
  __except (EXCEPTION_CONTINUE_EXECUTION)
  {
  }
}

Tip 13: Set breakpoints on specific threads

For multi-threaded applications, another useful tip is to set breakpoints on specific threads, processes or even computers. You can use the breakpoint Filer command to achieve this function.

分享Visual Studio原生开发的10个调试技巧(2)

The debugger allows you to use different combinations of thread name, thread ID, process name, process ID, and machine name (using AND, OR, NOT connections). Knowing how to set thread names also makes this filtering technique easier to operate.

Tip 14: (Inaccurate) Timing Execution

In my previous article I mentioned using dummy variables in the Watch window. One that is not mentioned is @clk, which can display the value of a counter to obtain the approximate time required for code execution between two breakpoints. The unit of this value is milliseconds (ms). However, this method cannot be used to configure program execution. You should use Visual Studio Profiler or Performance Timer to complete these configurations.

Reset the timer by adding @clk=0 in the Watch window or Immediate window. Therefore, if you need to calculate the time required to execute the last section of code, do the following:

Set a breakpoint at the beginning of the code block

Set a breakpoint at the end of the code block

Add @clk in the Watch window

When the first breakpoint is triggered, enter @clk=0 in the Intermediate window

Run the program until the end of the code block is encountered Breakpoint at the location and view the value of @clk in the Watch window

Note that there are tips on the Internet that you need to add two expressions in the Watch window: @clk and @clk=0. It is said that you can The timer is reset at the point where the breakpoint is executed. This technique can only be used in lower versions of Visual Studio, but cannot be used in higher versions of VS, such as VS2005 (the author has tested it, and vs2005 does not support this technique) and higher versions.

Tip 15: Formatting Numbers

  当你使用Watch或者Quick Watch窗口查看变量时,显示这些数值是用默认的预定义可视化格式。当变量是数字时,显示形式按照他们的类型(int、float、double)来的,并且使用十进制显示。然而,你可以设置调试器在显示数字的使用使用不同的类型,或者使用不同的进制。

  改变变量显示类型可以在变量前添加以下前缀:

by —— unsigned char(unsigned byte)

wo —— unsigned short(unsigned word)

dw —— unsigned long(unsigned double word)

  改变变量显示的进制可以在变量前添加以下前缀:

d或i —— 有符号十进制数

u     —— 无符号十进制数

o     —— 无符号八进制数

x     —— 小写十六进制数

X     —— 大写十六进制数

分享Visual Studio原生开发的10个调试技巧(2)

技巧16:格式化内存数据

  除了数字,debugger还可以在Watch窗口中显示格式化的内存数据,最长为64字节。你可以在表达式(变量或者内存地址)后面添加下面的后缀来格式化数据:

mb或m —— 十六进制显示的16字节数据,后面跟着16个ASCII字符

mw —— 8字(WORD,通常1 WORD = 2 BYTE)数据

md —— 4个双字(DWORD,通常1 DWORD = 4 BYTE)数据

mq —— 2个四字(Quad WORD)数据

ma —— 64个ASCII字符

mu —— 2字节UNICODE字符


分享Visual Studio原生开发的10个调试技巧(2)

技巧17:在系统DLL调用处暂停

  有时在DLL的某个函数被调用时暂停是很有用,特别是系统DLL(比如kernel32.dll、user32.dll)。实现这种暂停需要使用原生debugger提供的上下文运算符。你可以设定断点位置、变量名或者表达式:

{[函数],[源代码],[模块]}断点位置

{[函数],[源代码],[模块]}变量名

{[函数],[源代码],[模块]}表达式

  大括号内可以是函数名、源代码及模块的任意组合,但是逗号不能省略。

  举个例子如果我们需要在CreateThread函数调用时暂停。这个函数是从kernel32.dll导出的,因此上下文运算符应该是这样子的:{,,kernel32.dll}CreateThread。然而,这样并不行,因为该运算符需要CreateThread修饰之后的名字。可以使用  DBH.exe来获得一个特定函数的修饰名(编译器编译生成)。

  下面是如何获得CreateThread的修饰名的方法:

C:\Program Files (x86)\Debugging Tools for Windows (x86)>dbh.exe -s:srv*C:\Symbo
ls*http://msdl.microsoft.com/Download/Symbols -d C:\Windows\SysWOW64\kernel32.dl
l enum *CreateThread*
Symbol Search Path: srv*C:\Symbols*http://msdl.microsoft.com/Download/Symbols
  
 index      address   name
   1      10b4f65 :  _BaseCreateThreadPoolThread@12
   2      102e6b7 :  _CreateThreadpoolWork@12
   3      103234c :  _CreateThreadpoolStub@4
   4      1011ea8 :  _CreateThreadStub@24
   5      1019d40 :  _NtWow64CsrBasepCreateThread@12
   6      1019464 :  ??_C@_0BC@PKLIFPAJ@SHCreateThreadRef?$AA@
   7      107309c :  ??_C@_0BD@CIEDBPNA@TF_CreateThreadMgr?$AA@
   8      102ce87 :  _CreateThreadpoolCleanupGroupStub@0
   9      1038fe3 :  _CreateThreadpoolIoStub@16
   a      102e6f0 :  _CreateThreadpoolTimer@12
   b      102e759 :  _CreateThreadpoolWaitStub@12
   c      102ce8e :  _CreateThreadpoolCleanupGroup@0
   d      102e6e3 :  _CreateThreadpoolTimerStub@12
   e      1038ff0 :  _CreateThreadpoolIo@16
   f      102e766 :  _CreateThreadpoolWait@12
  10      102e6aa :  _CreateThreadpoolWorkStub@12
  11      1032359 :  _CreateThreadpool@4

  看起来真实的名字是_CreateThreadStub@24。因此我们可以创建断点,{,,kernel32.dll}_CreateThreadStub@24。

分享Visual Studio原生开发的10个调试技巧(2)

运行程序,当遇到暂停时,直接忽略关于在断点位置无相关源代码的消息提示。

分享Visual Studio原生开发的10个调试技巧(2)

使用调用堆栈窗口来查看调用这个函数的代码。

分享Visual Studio原生开发的10个调试技巧(2)

技巧18:载入符号

  当你调试程序的时候,调用堆栈窗口有可能不会显示全部的调用堆栈,其中忽略系统DLL(例如kernel32.dll, user32.dll)的信息。

分享Visual Studio原生开发的10个调试技巧(2)

  通过加载这些DLL的符号信息,可以获得全部调用堆栈信息,并且在调用堆栈窗口,使用上下文菜单(右键菜单),直接设置这种效果。你可以从预定义的符号路径或者微软的符号服务器(针对系统DLL)下载这些符号。在这些符号下载并导入到debugger中之后,调用堆栈更新如下:

分享Visual Studio原生开发的10个调试技巧(2)

这些符号也可以从Module窗口导入。

分享Visual Studio原生开发的10个调试技巧(2)

一旦载入之后,这些符号会保存在缓存中,并且可以在Tools>Options>Debugging>Symbols中配置。

分享Visual Studio原生开发的10个调试技巧(2)

 技巧19:在MFC中报告内存泄露

  如果你想在MFC应用程序中监测内存泄露,你可以使用宏DEBUG_NEW来重定义new运算符,这是new运算符的一个修改版本,可以记录其分配内存的文件名及行数。在Release版中构建的DEBUG_NEW会解析成原始的new运算符。

  MFC向导产生的源代码中在#include后米娜包含如下预处理指令:

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

上面代码就是如何重定义new运算符的方法。

  很多STL头文件和这里定义的new运算符不兼容。如果你在重新定义运算符new之后包含了等头文件,会有如下错误(以为例):

1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(43) : error C2665: 'operator new' : none of the 5 overloads could convert all the argument types
1>    c:\program files\microsoft visual studio 9.0\vc\include\new.h(85): could be 'void *operator new(size_t,const std::nothrow_t &) throw()'
1>    c:\program files\microsoft visual studio 9.0\vc\include\new.h(93): or    'void *operator new(size_t,void *)'
1>    while trying to match the argument list '(const char [70], int)'
1>    c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(145) : see reference to function template instantiation &#39;_Ty *std::_Allocate<char>(size_t,_Ty *)&#39; being compiled
1>    with
1>    [
1>      _Ty=char
1>    ]
1>    c:\program files (x86)\microsoft visual studio 9.0\vc\include\xmemory(144) : while compiling class template member function &#39;char *std::allocator<_Ty>::allocate(std::allocator<_Ty>::size_type)&#39;
1>    with
1>    [
1>      _Ty=char
1>    ]
1>    c:\program files (x86)\microsoft visual studio 9.0\vc\include\xstring(2216) : see reference to class template instantiation &#39;std::allocator<_Ty>&#39; being compiled
1>    with
1>    [
1>      _Ty=char
1>    ]

   

解决方法是在包含这些STL文件之后再使用DEBUG_NEW重定义new运算符。

技巧20:调试ATL

  当你开发ATL COM组件时你可以在debugger中查看你所开发的COM对象的QueryInterface、AddRef和Release的调用情况。默认情况下并不支持这些调用的产看,你需要在预处理定义或者预编译头文件中定义两个宏。这两个宏定义之后,关于这些函数的调用会显示在输出(Output)窗口中。

这两个宏是:

_ATL_DEBUG_QI,显示每个被查询接口的名字。必须在atlcom.h头文件被包含之前定义。
_ATL_DEBUG_INTERFACES,每当AddRef或Release被调用时显示当前接口的引用次数以及类名、接口名等信息。必须在atlbase.h包含之前定义。

以上就是本文的全部内容,希望大家结合之前分享过的文章进行学习,熟练掌握Visual Studio调试技巧。

更多分享Visual Studio原生开发的10个调试技巧(2)相关文章请关注PHP中文网!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Mastering C# .NET Design Patterns: From Singleton to Dependency InjectionMastering C# .NET Design Patterns: From Singleton to Dependency InjectionMay 09, 2025 am 12:15 AM

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C# .NET in the Modern World: Applications and IndustriesC# .NET in the Modern World: Applications and IndustriesMay 08, 2025 am 12:08 AM

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

C# .NET Framework vs. .NET Core/5/6: What's the Difference?C# .NET Framework vs. .NET Core/5/6: What's the Difference?May 07, 2025 am 12:06 AM

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The Community of C# .NET Developers: Resources and SupportThe Community of C# .NET Developers: Resources and SupportMay 06, 2025 am 12:11 AM

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The C# .NET Advantage: Features, Benefits, and Use CasesThe C# .NET Advantage: Features, Benefits, and Use CasesMay 05, 2025 am 12:01 AM

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

Is C# Always Associated with .NET? Exploring AlternativesIs C# Always Associated with .NET? Exploring AlternativesMay 04, 2025 am 12:06 AM

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

The .NET Ecosystem: C#'s Role and BeyondThe .NET Ecosystem: C#'s Role and BeyondMay 03, 2025 am 12:04 AM

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# as a .NET Language: The Foundation of the EcosystemC# as a .NET Language: The Foundation of the EcosystemMay 02, 2025 am 12:01 AM

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version