java java中有asm nop等价物吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10736275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there asm nop equivalent in java?
提问by mark
When I program C/C++ with Visual Studio I often use __asm nop;
command to insert a noopcode in order to have something to break on. For instance:
当我使用 Visual Studio 编程 C/C++ 时,我经常使用__asm nop;
命令来插入一个noop代码,以便有一些东西可以打破。例如:
if (someCondition())
{
__asm nop;
}
I have no idea what to do when the condition occurs, but I want to stop the execution and examine the current state. Sometimes someCondition()
is simple enough to create a conditional breakpoint, but conditional breakpoints slow down the execution significantly, besides it is not always possible.
我不知道当条件发生时该怎么做,但我想停止执行并检查当前状态。有时someCondition()
创建条件断点很简单,但条件断点会显着减慢执行速度,而且并不总是可行。
Now, in C# I break into the debugger directly either by calling System.Diagnostics.Debugger.Break()
or System.Diagnostics.Debugger.Launch()
.
现在,在 C# 中,我通过调用System.Diagnostics.Debugger.Break()
或直接进入调试器System.Diagnostics.Debugger.Launch()
。
Now I am forced to program Java and until now I have found no better alternative than just do System.out.println("bla-bla")
and set a breakpoint there. Again, please consider the case when a conditional breakpoint is not feasible.
现在我被迫编写 Java 程序,直到现在我还没有找到比在System.out.println("bla-bla")
那里设置断点更好的选择。同样,请考虑条件断点不可行的情况。
So, I wonder - is there an __asm nop
or System.Diagnostics.Debugger.Break()
alternative in Java?
所以,我不知道-是有一个__asm nop
或System.Diagnostics.Debugger.Break()
替代Java中?
回答by aioobe
In bytecode you have a nop
instruction, but there's no nop
statement in the Java language.
在字节码中有一条nop
指令,但nop
在 Java 语言中没有语句。
You can add an extra ;
on a line by itself and the code will still compile, but that's not much more meaningful than adding an empty line.
您可以;
在一行上单独添加一个额外的代码,代码仍然会编译,但这并不比添加一个空行更有意义。
Another "does nothing" statement could be:
另一个“什么都不做”的声明可能是:
assert true;
which has no side-effects what so ever, and can be turned off when executing the program.
它没有任何副作用,并且可以在执行程序时关闭。
As it turns out, assert true
does not seem to generate any bytecode instructions, which causes break-points on assert true to be skipped all together. Eclipse is however able to break on a statement such as
事实证明,assert true
似乎没有生成任何字节码指令,这会导致断言断点被一起跳过。然而,Eclipse 能够中断诸如
assert Boolean.TRUE;
which is quite similar.
这是非常相似的。
回答by mikera
You can just put in any arbitrary assignment statement that doesn't do anything, e.g.
您可以放入任何不做任何事情的任意赋值语句,例如
if (someCondition()) {
int t=0;
}
The debugger will be happy to break on this. Since t
is local to the block, it can't possibly have any side effects (and will get JIT-compiled out of existence in production code).
调试器会很乐意解决这个问题。由于t
是块本地的,它不可能有任何副作用(并且会在生产代码中不存在 JIT 编译)。
Alternatively, you can write a static function which has a breakpoint permanently set inside it, so you can just do:
或者,您可以编写一个静态函数,其中永久设置了一个断点,因此您可以执行以下操作:
if (someCondition()) {
breakPoint();
}
回答by Ben Leggiero
Java interprets this as an empty statement:
Java 将此解释为一个空语句:
;
Though, as noted in comments, Eclipse won't let you set a breakpoint here. If you want something useless that you can put a breakpoint on that's also nice and easy to type, I suggest:
不过,正如评论中所指出的,Eclipse 不会让您在此处设置断点。如果你想要一些无用的东西,你可以在上面放置一个断点,这也很好且易于输入,我建议:
if(false){}
Your compiler might warn you that this is never entered, which can be useful for reminding you to take it out before compiling for production. Hope this helps!
您的编译器可能会警告您从未输入过它,这对于提醒您在编译生产之前将其取出很有用。希望这可以帮助!
回答by Ben Leggiero
I'm separating this into a new answer because I'm surprised no one has said this, yet. Modern IDEs let you add logic to breakpoints!Instead of compiling a dedicated if
statement just for debugging, place the breakpoint somewhere you actually care about, then right-click it and select Breakpoint Properties!
我将其拆分为一个新答案,因为我很惊讶还没有人这么说。现代 IDE 可让您向断点添加逻辑!与其编译专门if
用于调试的语句,不如将断点放在您真正关心的地方,然后右键单击它并选择 Breakpoint Properties!
回答by user2761220
I did not find perfect solution for this, but here is the solution I like most. Probably not efficient but it lets you place a breakpoint, Eclipse does not complains or warns and from code reader point of view it is obvious what you want to do.
我没有为此找到完美的解决方案,但这是我最喜欢的解决方案。可能效率不高,但它可以让您放置断点,Eclipse 不会抱怨或警告,从代码阅读器的角度来看,您想要做什么很明显。
// Silly method that allows me to make noop
private static void noop() {
}
public otherMethod() {
// Here I need breakpoint
noop();
}
If i want to precondition breakpoint I place the condition before:
如果我想预先设置断点,我会将条件放在:
public otherMethod() {
// Here I need breakpoint
if(someCondition())
noop();
}