当前位置:网站首页>C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running
C #, introductory tutorial -- debugging skills and logical error probe technology and source code when the program is running
2022-07-28 08:47:00 【Deep confusion】

One 、 An overview of program errors
programming level , Like the constitution of the same person , Mainly from the foundation , Mathematics and algorithms ;
But programmed Skill , For example, one's martial arts or sports performance , It mainly comes from debugging 、 Testing and tracking .
Program errors can be roughly divided into : Grammar mistakes 、 Data error 、 Logic error
We all know , If the program is on which line ? Which column appears Grammar mistakes ,Visual Studio compiler Will point it out directly , It's easy to see , Very convenient .
The compiler is first of all a tool designed to solve syntax errors .
Data error It mainly appears in local , or Interface part . Local data error , By setting breakpoints , Then press F10, F11 Wait for shortcut keys Step by step debugging Is a good way .
But many cannot be debugged in one step Interface type data error , It's more troublesome . We can only use the following methods .
Logic error It's deadly 、 It is also the most difficult to track and analyze . Logic error still Procedural error , It often appears when the program is running . But , If it is a compiled program , be called Runtime code , There is an error , How to know which line ? Which train ? Which function has a problem ?
You may have read many articles in this field , But then C# for , I suggest you use Beijing Liangao Software Development Co., Ltd Open source here Truffer One of the basic classes of :C# Runtime logic error probe class .
characteristic :
(1) Directly output which source code ? Line number ? Column number ? Function name ? Key information ;
(2) Insert probe And revoke It's very simple ;
(3) can Pagination The output exceeds 20 Billion probe information , As long as you have enough hard disk space ;
(4) Output based Probe information , You can do a lot of analysis ( big data );
(5) Based on the following code Truffer Xtracer Function more , For example, you can detect Dead cycle , Can detect the heap 、 Stack and over bound of important data .

Two 、 Logic error probe class ( A short edition )
#if DEBUG
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace Legalsoft.Truffer
{
public static class zz
{
private static StringBuilder sb { get; set; } = new StringBuilder();
private static string head { get; set; } = "";
private static string tail { get; set; } = "";
private static string format { get; set; } = "";
/// <summary>
/// The number of steps of the tracker
/// </summary>
private static long step { get; set; } = 0;
/// <summary>
/// Every html The file shows step Count
/// </summary>
private static long pagesize { get; set; } = 5000;
/// <summary>
/// every other writesize Output tracker statement (html)
/// If you set writesize=0, Then each tracker statement outputs a file ;
/// </summary>
private static long writesize { get; set; } = 100;
/// <summary>
/// initialization
/// </summary>
private static void a()
{
if (head.Length == 0)
{
StringBuilder sx = new StringBuilder();
sx.AppendLine("<html>");
sx.AppendLine("<body>");
sx.AppendLine("<style>td { padding:5px;text-align:left; } </style>");
sx.AppendLine("<table width='100%' border=1 bordercolor='#AAAAEE' style='border-collapse:collapse;'>");
sx.Append("<tr style='background-color:#EEEEFF;'>");
sx.Append("<td> Serial number </td>");
sx.Append("<td> file name </td>");
sx.Append("<td> That's ok </td>");
sx.Append("<td> Column </td>");
sx.Append("<td> Function name </td>");
sx.Append("<td> Information </td>");
sx.AppendLine("</tr>");
head = sx.ToString();
}
if (tail.Length == 0)
{
StringBuilder sx = new StringBuilder();
sx.Append("<tr style='background-color:#EEEEFF;'>");
sx.Append("<td> Serial number </td>");
sx.Append("<td> file name </td>");
sx.Append("<td> That's ok </td>");
sx.Append("<td> Column </td>");
sx.Append("<td> Function name </td>");
sx.Append("<td> Information </td>");
sx.AppendLine("</tr>");
sx.AppendLine("</table>");
sx.AppendLine("</body>");
sx.AppendLine("</html>");
tail = sx.ToString();
}
if (format.Length == 0)
{
StringBuilder sx = new StringBuilder();
sx.Append("<tr>");
sx.Append("<td style='text-align:right'>{0}</td>");
sx.Append("<td>{1}</td>");
sx.Append("<td>{2}</td>");
sx.Append("<td>{3}</td>");
sx.Append("<td>{4}</td>");
sx.Append("<td>{5}</td>");
sx.AppendLine("</tr>");
format = sx.ToString();
}
}
/// <summary>
/// Probe function
/// The calling method is :
/// zz.z(new StackTrace(new StackFrame(true)).GetFrame(0));
/// </summary>
/// <param name="sf"> Stack frame </param>
/// <param name="info"> You can define any type of output information </param>
public static void z(StackFrame sf, string info = "")
{
a();
string buf = String.Format(format, step + 1, sf.GetFileName(), sf.GetFileLineNumber(), sf.GetFileColumnNumber(), sf.GetMethod().Name, info);
sb.AppendLine(buf);
if (step > 0)
{
if ((step % writesize) == 0 || writesize == 0)
{
string fn = String.Format("Z-{0:D8}.html", (int)((step - (step % pagesize)) / pagesize));
File.WriteAllText(fn, head + sb.ToString() + tail, Encoding.UTF8);
}
if ((step % pagesize) == 0)
{
sb.Clear();
sb.AppendLine(buf);
}
}
step++;
}
}
}
#endif
Usage method :
Please save the above code as Basic.Z.cs , And add The project project that will do .
3、 ... and 、 Use of probes
Add the appropriate probe code to the original program , Then run the program to output Z-0000000.html Such a page file , You can read directly with the browser , Understand the sequence of program calls , And analyze the possible logical problems .
1、 Probe insertion
In need of recording ( Probe ) The location of , Insert the following statement :
#if DEBUG
zz.z(new StackTrace(new StackFrame(true)).GetFrame(0));
#endif
Or bring important information ( track begin end Data changes of ):
#if DEBUG
zz.z(new StackTrace(new StackFrame(true)).GetFrame(0), begin + ", " + end);
#endif
A complete code example :
...
using Legalsoft.Truffer;
namespace Legalsoft.DeepConfiser
{
public class TokenScanner
{
/// <summary>
/// Put all tokens (tokens)
/// Press {} Roughly divided into hierarchical blocks (block)
/// ( Depth first traversal recursive algorithm )
/// </summary>
/// <param name="parent"></param>
/// <param name="tokens"></param>
/// <param name="begin"></param>
/// <param name="end"></param>
public void Segmentation(Block parent, List<Token> tokens, int begin, int end)
{
#if DEBUG
zz.z(new StackTrace(new StackFrame(true)).GetFrame(0), begin + ", " + end);
#endif
if (tokens[begin].Buffer == "{" && tokens[end].Buffer == "}")
{
begin++;
end--;
}
...
}
}
}
2、 Withdrawal of probe

Compilation options #if DEBUG , Only applicable to the compilation results of debugging mode ; about RELEASE For the compiled version of , Among them exe The document does not contain The probe's , No output Z-00000000.html file , thus , Generally speaking , You don't need to delete ( revoke ) The probe code added earlier .
Small medicine cures serious diseases .
边栏推荐
- classLoader加载的class的回收
- Smart software completed round C financing, making Bi truly "inclusive"
- 微信小程序----微信小程序浏览pdf文件
- GB/T 41479-2022信息安全技术 网络数据处理安全要求 导图概览
- Maximum product of leetcode/ word length
- Win the bid! Nantah general gbase 8s won the bid for the 2022 database framework project of NARI Group
- 谷歌 Material Design 的文本框为什么没人用?
- 模型预测控制(MPC)解析(九):二次规划的数值解(下)
- GBase 8a MPP与银河麒麟(x86版)完成深度适配
- PHP Basics - PHP uses mysqli
猜你喜欢

1w5 words to introduce those technical solutions of distributed system in detail

2022牛客多校第二场解题报告

JS inheritance method

Can‘t connect to server on ‘IP‘ (60)

49-OpenCv深入分析轮廓

思迈特软件Smartbi完成C轮融资,推动国产BI加速进入智能化时代

Let me teach you how to assemble a registration center?

1w5字详细介绍分布式系统的那些技术方案

【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样
![[soft test software evaluator] 2013 comprehensive knowledge over the years](/img/c5/183acabd7015a5e515b7d83c127b2c.jpg)
[soft test software evaluator] 2013 comprehensive knowledge over the years
随机推荐
NDK 系列(6):说一下注册 JNI 函数的方式和时机
MySQL how to add users and set permissions?
Why can ThreadLocal achieve thread isolation?
优炫数据库导入和导出方法
PHP Basics - PHP uses mysqli
Flink Window&Time 原理
GBase 8a MPP与银河麒麟(x86版)完成深度适配
Hcip --- LDP and MPLS Technology (detailed explanation)
2018年1月西邻雪山自驾游攻略
Export SQL server query results to excel table
机器学习如何做到疫情可视化——疫情数据分析与预测实战
Blog Building 9: add search function to Hugo
[mindspire YiDianTong robot-01] you may have seen many Knowledge Q & A robots, but this is a little different
微服务架构 Sentinel 的服务限流及熔断
博客搭建九:hugo添加搜索功能
Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice
Three different numbers with 0 in leetcode/ array
49-OpenCv深入分析轮廓
Shell programming specifications and variables
Win the bid! Nantah general gbase 8s won the bid for the 2022 database framework project of NARI Group