0

I'm working on a project for my Uni where I want to visualize code debugging. For this I somehow need to log the executed Lines of Code and the variables with their values for a given Java program. An example:

public class Main {

public static void main(String[] args){
   String abc = "def";
   String test = "hello world";
   String foo = abc+test;   
}
}

If i log this programm my output should be something like this:

  1. Main at line 3:
  2. Main at line 4: abc=def
  3. Main at line 5: abc=def,test = hello world
  4. Main at line 6: abc=def, test = hello world, foo = defhello world

The logging program should run in the background so I can use the logged program normally. I already tried stuff with Java Agents and Stacktrace but I could'nt get good results. I hope there is any way to do this. Thanks for any help in advance!

GreenTea26
  • 13
  • 2
  • 1
    Use the [Java Debug Interface](https://docs.oracle.com/javase/8/docs/jdk/api/jpda/jdi/) – Vince Jun 04 '20 at 19:12

1 Answers1

1

There are ways to do this, some IDE like Intelij IDEA actually display the variable value in the editor when you debug.

But if you want to log that, not only the information log would soon become huge (gigabytes/terabytes for real programs) but it would be quite complex.

Here several ways to do this:

Nicolas Bousquet
  • 3,990
  • 1
  • 16
  • 18
  • 1
    Thanks for the answer, I think i'll try it with the Java debugger API. Your link to the docomentation does not work though. Is it possible to start the traget program and the debug program in their own command prompts? – GreenTea26 Jun 04 '20 at 22:07
  • Look at what you IDE can do. It can start any java program and debug it... Or it can connect to any program with the right command line option so the debug port is open. The program can do whatever. I often debug webservers, but also command line based app too. – Nicolas Bousquet Jun 06 '20 at 08:22
  • Thanks to your answer I was able to write my own debugger which is able to log simple programs in the way i want. I'm now trying to debug a command line based java programm but i cant figure out how to forward the inputs and outputs between the debugger and the debuggee. I have wrote a [question](https://stackoverflow.com/questions/62471336/how-to-handle-input-for-a-java-console-application-debugged-with-java-jdi) on this, but haven't gotten an answer yet. Do you have an idea how to do this? – GreenTea26 Jun 21 '20 at 13:13