tags:

views:

807

answers:

22

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.

+4  A: 
console.write("stop here")

In .net always use exactly that line, I have no idea why, just have for years.

brendan
I like it; its self explanatory.
rektide
And if your console is asking to stop in a release build, you know you left something in the patient ;-)
RBerteig
+11  A: 

Visual Studio lets you break on a brace.

Andrew Hare
And if you can't use VS? Many debuggers don't allow this, or working on a different platform than windows?
xan
+2  A: 

In C#, I break on the end brace, and in VB.NET I break on the End If/Sub/Function/whatever.

Jim H.
+1  A: 
Console.Writeline(e.Message);
MrTelly
+2  A: 

bool breakHere = true;

It's self-documenting!

joelr
+3  A: 
(void *)"Insert witty comment."
Sid McAwesome
+7  A: 

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().

Lee Baldwin
this also works : *((char*)0) = 0;
Vardhan Varma
It looks cooler without the redundant parenthesis: *(int *) 0 = 0; is what I use. :)
unwind
With the interrupt, you can just continue execution, the debugger allows you to continue without issue. By throwing an unhandled exception, you get do get the break in the debugger, but you can't continue without setting the next statement.
Lee Baldwin
"this also works : *((char*)0) = 0;" -- it raises a SIGSEGV on my box. Not very useful unless you only want to run backwards from the breakpoint. I heard some versions of windows allows writes to address 0; is this still true?
Jonas Kölker
+7  A: 
Sung Meister
You're making an assumption about the environment he's working in here.
SpoonMeiser
Yes, there was no specific environment that the question mentioned. By the way, nice name. Our names almost rhyme ;)
Sung Meister
+3  A: 

asm { int3 }

But i'm a crazy person :D

olliej
+9  A: 

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"&gt;
  <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>
Peter McGrattan
+1  A: 

I do the same thing with the int value but my favorite is "int y=1;".

Levi Taylor
+4  A: 

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.

devio
Could cause issues if you override the = operator in C++ (and it is poorly written!).
strager
A: 

String foo = "bar";

Neil McKeown
+1  A: 
int moo = 42;

Why moo? Don't know, it just springs to mind. Why 42? Why wouldn't you pick it!

xan
+1  A: 
int x = 0;  
x = x;
Mark Ingram
+2  A: 

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.

Mr. Shiny and New
+1  A: 

asm volatile ("nop");

moonshadow
+1  A: 

In most if not all Javascript debuggers you can use the

debugger;

statement, which behaves as a breakpoint.

Ej
+2  A: 

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.

Gavin Miller
A: 

Let's consider the desirable properties. The code should

  1. be breakpoint-able
  2. clearly explain that it's only there for breakpoints.
  3. not be immediately visible to a user who runs it.
  4. not impact run-time behavior (including running time).

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 ;-)

Jonas Kölker
+3  A: 

In C/C++ you can use a no-op:

;

Yuval A
A: 

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.

brone