How to track the methods used in a Java application while running - java

I am working on understanding a project which was already created by someone else. It did not followed a proper coding method (no comments and so on). I'm finding it difficult to find where a method is used during the use of the app .So ideally I want to track how each method is used and when it's called while manipulating the applciation . I've already searched the problem here and nothing worked for me. So can anyone please help me out.
I am using Intellij btw .
Thank you .

You can use the debugger of Intellij, that's a really cool tool and besides your problem, it can be used to find bugs in your code.
The basic and easiest way to use it is:
Add breakpoints on lines where you want the application to stop just before executing that line. You can add as many breakpoints as you want and in as many files you want. To add a breakpoint, just click on the empty space beside the line numbering on the left side of the editor window.
then turn on the debugger from the run menu or Shift + F9.
then use the step over option or F8 to jump to the next breakpoint from the current location.
In your case, you can add a breakpoint in the method for which you wanna test, and then see if the program approaches that particular breakpoint during execution.
You can read the more detailed documentation on debugging from this link: Complete debugging guide: Jetbrains.
Hope this helps to solve your issue.

use Find Usages for the method to see where it is used
use a method call hierarchy to see which methods invoke this method
use method breakpoint to stop in debugger any time the method is invoked

Related

IntelliJ debugger gets stuck

I'm debugging a normal Java application, no GUI, just a lot of computations and ~5 calls in the stack for the main thread when the problem occurs. Basically it keeps saying "Collecting data" in the local variable watch.
So instead of going step-by-step I've tried to add a breakpoint immediately after an press "Resume". Now it says "Waiting until last debugger command completes".
Have anyone had this problem before? Is changing the debugger the only way to figure this out?
On IntelliJ (2017.1.4 Community Edition), the following fixed the problem for me:
File->Settings
Type in "toString"
Navigate to Build, Execution, Deployment->Debugger->Data views->Java
Find the "Enable 'toString()' object view:" checkbox
Uncheck the box
Re-run the debugger.
The following fixed it for me on IntelliJ 2018.2.4:
Right click breakpoint
Toggle the setting to suspend "Thread" instead of "All"
This won't be helpful if you actually need to suspend all the threads for debugging, but it got rid of the "Collecting data..." and "Waiting until last debugger command completes" messages for me. The setting also persists for subsequent breakpoints, so you only need to change it once.
I just ran into what looks like the same issue. In my case it was a class (KafkaStream) in the breakpoint stack trace with a "bad" toString method. The toString method blocks and therefore hangs the debugger. I tested the toString method in the main line code and it hung the main thread (i.e. this is not a debugger specific issue).
Here is the stack trace for my thread that hit the breakpoint (on a line that was just trying to test a boolean attribute of my class):
Intellij provides a way to work around for my issue. It allows you to override how the debugger renders the class:
If your issue comes back I suggest taking a thread dump (inside or outside of the IDE) and see what your thread is doing.
In most cases, it would be because of the watches that you add while debugging.
Clear the watch statements that would result in recursive execution of same statements as in the code.
Always keep the watches clean before debugging.
It happened to me once (on version 2020.3.3) and "Invalidate Caches" and restart solved it.
The fix that worked for me was to remove method breakpoints. That made it superfast.

Can i trace back in Netbeans debugger?

I am currently developing a java application, I am trying to use the builtin debugger in Netbeans. I wanted to know how to trace back in the debugger.
Assuming I am executing the instructions line by line, if the program is currently executing 105th line of code and if i would want the program to go back and execute the 103rd line of code, how do i do it? Is this even possible ?
Please read the below link. There is a concept of Pop Topmost Call which might help you.
http://wiki.netbeans.org/FaqDebugBackup
You can do that by setting a break point on 103 line. Then go the call trace and then on the call before to the current right click and do drop to frame. It will re execute that part. Then it will hit you break point on line 103.
I suggest you don't dot it multiple times in the same run. the state of the system becomes unstable for the run by doing this over and over again.

How on earth does he debug a running application like this, and more importantly, how can I?

"Debugception!"
You may notice that within the first 15 seconds of this YouTube video (from 1:01:01 to 1:01:16), Markus Persson (aka "Notch", creator of Minecraft) has somehow managed to save/update an application and attach a debugger to it while it was already under the process of being debugged, supposedly all with a simple keyboard shortcut. The previously coded application somehow magically became the newly edited one, and seemingly without relaunching it or spawning a new process... It's possible that this is just some form of locally remote debugging, but something about it just doesn't seem quite right.
I've spent several days Googling and asking around on how he was able to do this, yet to no avail. I've found no such option under Eclipse preferences, and whenever I try to save & debug an already running application, it simply launches a separate instance of the newly updated application, side-by-side with the older, outdated one.
Am I missing something? How was this possible?
How was he able to utilize such an astounding, powerful debugging feature?
Thanks in advance!
Update
Okay, so this appears to be a standard feature specific to Eclipse.
Coming from a background in NetBeans and Visual Studio, I'm astounded that this doesn't seem to exist elsewhere (or at least in NetBeans!)...
This is a built-in feature of Eclipse. If you edit a method while the program is running in debug mode, it will compile the new method, and replace the old method with the new version. If some thread was already running that method, it will jump back to the beginning (AFAIK; this might only happen when the program is paused).
You don't need to re-launch the program or set any special preferences. Just edit and save, and the magic will happen.
Eclipse can't always figure out how to merge your changes into the running program - usually if you changed anything outside a method body (including the method's parameters or return type). In this case, you will get a warning dialog, with the option to stop the program, restart the program or ignore the changes.

Ecplise View flow of running program

I'm currently writing a pretty large program that calls the same methods from different places.
Now I would really like to see how the program goes from one method to another as it is running. Like a live view that shows when what method is opened (and why?). Call Hierarchy doesn't suit my needs at this point. Is there a way?
One way to follow the logic of your application is by placing breakpoints at the line of code you want your application to stop at but, to do this you'll have to setup it up in debug mode.
Every major IDE will let you do this, including Eclipse.
Have a look at this tutorial:
Java Debugging with Eclipse
Once you setup your program in debug mode you can add a breakpoint in the gutter next to the line numbers.

Can I find out which thread is running using Eclipse?

I close my application by pressing a "Close" button. But in the Eclipse I see a red square indicating that something is still running. When I press this red square, I kill my application completely.
Is it possible to find out what is still running (which method, which loop) using Eclipse?
P.S. I am a newbie. So, it would be nice to have a simple solution. I also might not understand your answer if you use "technical" words which I do not know.
ADDED:
I cannot use System.exit since it will kill not only my software but also an "external" software which calls my software.
Have a look at jps and jstack commands they will give you the process id for your application and then you can view the threads and their states using jstack.
Its very useful for this kind of issue.
Sorry just realised you wanted something in eclipse to do this. well I'll leave this answer as it should work but not inside eclipse.
Debug View shows just that. To see the concrete methods for each thread you need to stop the application. Most probably you just need to set default close operation for the main JFrame.

Categories

Resources