Whenever I want a breakpoint someplace where there isnt anything to break on just (inside a loop, &c), I tend to automatically drop down a:
int i = 2;
I'm curious what others use.
Whenever I want a breakpoint someplace where there isnt anything to break on just (inside a loop, &c), I tend to automatically drop down a:
int i = 2;
I'm curious what others use.
console.write("stop here")
In .net always use exactly that line, I have no idea why, just have for years.
In C#, I break on the end brace, and in VB.NET I break on the End If/Sub/Function/whatever.
If it's C++ on a x86 machine, I just use:
__asm int 3
and just wait for the exception to hit. Don't even have to start in the debugger on Windows, or you use good ole DebugBreak().
In the .NET Framework (using C# here):
System.Diagnostics.Debugger.Break();
To avoid typing this every time just create a new code snippet for your preferred language:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>sddb</Title>
<Shortcut>sddb</Shortcut>
<Description>
Code snippet for System.Diagnostics.Debugger.Break()
</Description>
<Author>Peter McGrattan</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="csharp">
<![CDATA[System.Diagnostics.Debugger.Break();$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
I do the same thing with the int value but my favorite is "int y=1;".
i = i;
or any variable defined in the scope.
In C#/VS you get a compiler warning, so it's also a good method to mark code as not finished etc.
int moo = 42;
Why moo? Don't know, it just springs to mind. Why 42? Why wouldn't you pick it!
What's the point of inserting code to break? Isn't there already code where you want to break? I just put the breakpoint on the actual line in question; my IDE (Eclipse) stops before executing that line. Seems easy enough.
Now, before I learned about conditional breakpoints, I used to add code to test for interesting conditions so that I wasn't breaking on every loop iteration or every call of a commonly-used function.
In most if not all Javascript debuggers you can use the
debugger;
statement, which behaves as a breakpoint.
I use this in C#:
If (Debugger.IsAttached)
Debugger.Break();
I throw the IsAttached so that if it doesn't get removed it doesn't effect production code.
Let's consider the desirable properties. The code should
Depending on which optimizer you use, the best breakpoint code may vary.
I'm not much for adding assembly. It's not portable and it screws up the compiler's analysis of the surrounding code. Even if it's a no-op, it might impact performance.
Printing is user-visible. Talking to the debugger feels like you're doing something wrong. I've heard stories about how threading bugs disappeared when the code was ran in the debugger (and that's how debuggers got their name :D). I'd say it's something to avoid, and the problems it solve should be solved by a better debugger instead (an easy-to-use monkey-patcher, maybe?).
I think the best code is bool breakpoint_dummy = /* the value means nothing */ false;
. It fits in 80 columns, it explains what it is, and if your optimizing compiler can't deal with unused variables, it'll (hopefully?) compile to xorl eax, eax
, a fast and small piece of code (I'd guess). In general, zeros are cheaper than ones ;-)
What I've always done is make some kind of #define
, e.g. (VC++):
#define NOP __asm{nop}
This can then easily be something else for different platforms, e.g. (gcc):
#define NOP asm("nop")
If there's no inline assembly language, you can fudge in something sensible:
/* insert directives to prevent inlining and optimization */
void nop() {}
#define NOP (nop())
It can also be turned into nothing for the builds you don't want it in:
#define NOP ((void)0)
This sort of thing can go wherever the platform-specific typedefs and so on go.