When using gdb to debug there are a couple of ways to automate the actions performed when a break point is hit. This is good for cases where I only have compiled code with debug symbols, not source code. It is also nice when I want to instrument something interactively without relying on code reloading to insert print statements.
Is there a way to do this with the Eclipse debugger and Java code? All I need is a way to print objects and variables and then continue from the breakpoint.
You can inject code using conditional breakpoints.
In this example the breakpoint never suspends because of return false;, but always print the absolut path of a file-variable named "file"!
Related
Follow these steps to reproduce:
Create a '.jsh' script with some kind of error(missing import, syntax error etc)
open a jshell
/open the erroneous script
The /open command will finish silently.
The elements declared in that script won't be in the current namespace.
Is there a way to force jshell to spit out the error and it's location in the script if an erroneous script is loaded? Setting the feedback level to verbose doesn't change anything.
jshell imports a lot of classes on startup and any subsequent snippet (even if load from file) can use them, SO few of the missing imports can be taken care here.
--no-startup can be used to disable this behavior.
This problem seems to be related to the „forward references“ feature of JShell: When a method or type declaration refers to a still undefined variable or method, JShell checks only for syntactical correctness and delays the compilation of the method resp. type. In interactive mode it prints a helpful message like „method x refers to undefined method y and can’t be invoked until method y is defined“. But while a startup script runs, that message is not printed and in many cases the script executes nothing, which is very confusing. I have not found a way to enable the printing of that message.
Hypercritical persons might say that this bug/feature renders JShell useless for „real“ scripting applications, and I tend to agree.
I propose that the „forward references“ feature be disabled during the execution of scripts. In order to make forward references still possible, the entire script should be considered ONE big snippet. That would also eliminate the problem with line-numbers-per-snippet which makes no sense at all with code parsed from scripts.
I'm writing scripts with a Java api which compile to class files and are executed out of my perspective on a virtual machine (I don't have access to this virtual machine and can't debug my class files from within.) These scripts require observable data to execute and don't run properly unless they've been executed within this virtual machine. As a consequence of the way that these class files are executed, to my knowledge, I can't debug them with my IDE's built-in debugger.
I don't really have the coding vocabulary to even search for answers to that last statement, so please correct me if I'm wrong.
Anyways, the only way which I've determined will allow me to debug these scripts is the classic print-statement method. It's horrible. But it works. And it seems that the entire community for the api uses this method.
I had the idea that I could use reflection to grab information from my classes as they're executing, but this still doesn't give me access to the line-by-line debugging that I'm looking for.
What I'd like to do is monitor the execution of my script step-by-step (every calculation the VM does) and store information about those calculations (variable "foo" in class "bar" becomes 4 on line "soandso") as a sort of running cache which dumps to the system console whenever an error occurs.
Is this possible?
You can take a look at dynamic proxies.
Here is a very good explanation of it.
With a dynamic proxy you can output (log, send as mail, ...) the method that is beeing called and the parameters that are passed to the method.
It's not exactly what you are looking for but maybe it will help you.
I am working on a project of java. I opened the project in debugging mode, and goes through the program. One thing where I got stuck is that, if I step into a specfic function, it dont go into it. Instead if I put a breakpoint inside that function then program goes upto that point. I am using Eclipse 3.7.2. I dont know why eclipse is showing such a behaviour. Any help will be appreciaed.
dystroy already said in a comment what I was planning to say in this answer: the most common cause for me experiencing this is when the actual runtime class instance is a dynamic proxy, usually from either hibernate, or Spring, or a mock object framework (when testing) such as Mockito. In those cases, you generally have to do exactly what you have done, and put a breakpoint inside the method being stepped into.
is there a way to print each line of source when the program executes without having to insert System.out.println after each line?
Use a debugger. Debuggers provide a number of useful tools to step through your program. Check your IDE, it probably has one.
First a question: Why would you need that? You might use a debugger if it's just for debugging.
If it is for logging purposes, logging each line would be overkill.
Second a suggestion: you might use AOP to log each method call (assignments etc. could not be intercepted), but that might require a lot of work (incorporate AOP into your build process etc.) and might not be worth the hassle.
Eclipse Test & Performance Tools Platform Project has a way to show you a sequence diagram of the program execution:
http://www.eclipse.org/tptp/home/documents/tutorials/profilingtool/profilingexample_32.html
For printing as text, AOP is nice but complicated, debug statements are easiest but most invasive.
I'm pretty sure Eclipse lets you step through the program line by line, the link is unfortunately down for maintenance but maybe check back on it later: http://help.eclipse.org/indigo/topic/org.eclipse.jdt.doc.user/tasks/task-stepping.htm
Suppose that I have a Java program within an IDE (Eclipse in this case).
Suppose now that I execute the program and at some point terminate it or it ends naturally.
Is there a convenient way to determine which lines executed at least once and which ones did not (e.g., exception handling or conditions that weren't reached?)
A manual way to collect this information would be to constantly step with the debugging and maintain a set of lines where we have passed at least once. However, is there some tool or profiler that already does that?
Edit: Just for clarification: I need to be able to access this information programmatically and not necessarily from a JUnit test.
eclemma would be a good start: a code coverage tool would allow a coverage session to record the information you are looking for.
(source: eclemma.org)
What you're asking about is called "coverage". There are several tools that measure that, some of which integrate into Eclipse. I've used jcoverage and it works (I believe it has a free trial period, after which you'd have to buy it). I've not used it, but you might also try Coverlipse.
If I understand the question correctly you want more than the standard stacktrace data but you don't want to manually instrument your code with, say, log4j debug statements.
The only thing I can think of is to add some sort of bytecode tracing. Refer to Instrumenting Java bytecode. The article references Cobertura which I haven't used but sounds like what you need...