Are you sure you want to use a breakpoint?

Source: Internet
Author: User
Tags add time

There are 6 questions from the shap to the deep. You can try to answer them.

  1. The following statement for (INT I = 0; I <10; I ++) {if (I = 5) J = 5 ;}, writes everything in one row, how do you insert a breakpoint before J = 5?
  2. When you set a breakpoint within a 1000-time cycle body, how can you achieve this when the cycle is interrupted after 900 times?
  3. You have an expression that changes one time in the above loop. You want to know which expression is used, where it is, and how to do it?
  4. What do you want your breakpoint to be interrupted once every three times after it is hit, for example, 100, 103rd, and 106th?
  5. You are debugging a service program and want to place a breakpoint inside it. However, because this is a public service, you do not want other programs that access this service to be disturbed by your debugging, what do you want to do?
  6. How to know the interval between two breakpoint interruptions

Question 1, 2

The two questions are the simplest. I will illustrate them in one example.

For example, the following Loop

For (INT I = 0; I <1000; I ++) {dosomething ......}

Right-click the braces of the loop and insert the breakpoint. This method can be used to deal with the guys who prefer to write the statement on a line. in net3.5, we certainly often write complex expressions on one line. This insertion method will be useful in this case.

OK. Now let's edit the breakpoint condition, right-click the breakpoint, and select the menu item

In the displayed window, you can set the breakpoint hit condition I = 900.

Note that I am debugging C # code. The default conditional statement syntax is C #. If you want to switch, use ctrl-B to insert a breakpoint, select a language in the pop-up window.

By setting the conditional breakpoint in this way, we can solve our problem 1 and 2.

 

Question 3

By setting conditional breakpoints, we can also solve our problem 3. Tracking expression changes

String user = "yizhu2000"

For (INT I = 0; I <10000; I ++ ){

Dosomething1 ()

.......

Dosomethingn ()

}

When the execution of the loop is complete, we find that the user has changed to "smart_boy". You don't know how many cycles this value has changed. will you choose to break the breakpoint, one interruption at a time. How can I check it? Of course not required

At the end of the loop body, we set a breakpoint to open the conditional editing window (the method is the same as above), set the expression to user, and check the haschanged below, that is, you will tell the breakpoint, triggered only when the user value changes

(Note: When a breakpoint is executed for the first time, the program will be interrupted and the value of the expression will be calculated. Therefore, the so-called change occurs, it refers to the comparison between the expression value and the expression value when the breakpoint is executed for the first time)

Question 4

How can I trigger a breakpoint when the specified number of hits is greater than a certain number of times? The method is to set the hitcount of several breakpoints, right-click the breakpoint, and select hit count in the pop-up menu. The following window is displayed:

In the "when the break point is hit" drop-down list, we can see four options

Break always: always interrupted

Break when the hit count is equal to: equal to a certain number of times of Interruption

Beak when the hit count is a multpile of: interrupted when the number of times is a multiple of a certain number

Break when the hit count is greater than or equal to: interrupted when the value is greater than or equal to a certain number

Question 5

The first four problems have been solved. The solution to the 5th problems is to use the filter function of the breakpoint. For example, I want the breakpoint to be triggered only when it is accessed by a machine named yizhu, I can set it like this

When other machines access the program, the breakpoint will not be triggered. The advantage of this is that by setting the machine name, we can make other machines do not feel the existence of the breakpoint when accessing the program, in addition, you can set the machine name, process number, process name, thread number, and thread name as the filter, and combine them, for example, if you want to trigger a task only when the dllhost process of the yizhu machine is called, you can set the problem to machinename = "yizhu" & processname = "DLLHOST"

Question 6

Now we can solve 6th problems:

When debugging program performance, we often need to know the execution efficiency of a code segment. Generally, we can add time points in the program and subtract time points to get the time interval, the obvious disadvantage of this method is that you need to modify the program. If you want to avoid modifying the program, you need some tools. Is there any way to insert a time point in a declarative manner and calculate the value? In fact, the breakpoint can be fully implemented

Before providing the method, let's take a look at another breakpoint setting item, when hit. This option allows us to do something after hitting the breakpoint, including outputting some content or calling macros, for example, output the value of a variable in a program

We output the value of the variable user. The following continue execution indicates that the program will not be interrupted, and the execution will continue after the output. Note that the expression must be included in {}, and other parts will be output as strings. After whenhit is set, the breakpoint is changed to a square shape (I really like this guy when it's annoying)

View the output result in output as follows:

Since expressions can be computed, our first simplest solution is to output datetime when the program is executed to a breakpoint. now, this is certainly feasible, but we need a time interval, so we still need to calculate a subtraction by ourselves, which is quite troublesome, how can we let the program output time intervals by itself? The idea is that we declare a time variable at the previous breakpoint, And then subtract the variable with datatime. Now in the following breakpoint, that is

Condition of breakpoint 1: {datetime _ t = datetime. Now ;}

Condition of breakpoint 2: {datetime. Now-T ;}

It looks good, but there is a problem in actual operation. Let's take a look at the output.

In the highlighted section above, the variable Declaration can only be performed in the immediate window, so the variable in breakpoint 1 is not declared successful. We will be involved in immediatewindow in the future, I want to declare the variable in the expression. so how can we not declare variables and time points?

Now I think of the thread. setdata method. This method can insert some data into the space provided by the current thread and get data through getdata. For details, refer

Http://msdn2.microsoft.com/zh-cn/library/system.threading.thread.setdata (vs.80). aspx

So the solution is ready. Place the time in the dataslot of the thread at the first breakpoint, and subtract the second breakpoint.

Condition of breakpoint 1: {thread. setdata (thread. getnameddataslot ("executiontime"), datetime. Now );}

Condition of breakpoint 2: {datetime. Now-(datetime) system. Threading. thread. getdata (system. Threading. thread. getnameddataslot ("executiontime "));}

View output results

Our goal has been achieved, and the time interval of successful output in output is not perfect. First, this method is limited to two breakpoints. You want to open several more breakpoints, the test interval is still troublesome. the measurement accuracy can also be improved. If you are interested, you can study the extension of this method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.