5

In IntelliJ, if I open a class file for which I don't have the source it gets automatically decompiled. The problem is I want to set a breakpoint on a line number that is further that the last line in the decompiled version of the file. I know that line number from a stacktrace.

How can I set a breakpoint on an arbitrary line number in IntelliJ?

qwertzguy
  • 15,699
  • 9
  • 63
  • 66
  • I am not aware of a way to do this. You may be stuck setting a method breakpoint, although those have unfortunate performance implications (they will _drastically_ slow your program while enabled). Is there somewhere temporally close to your failure point where you can set a line breakpoint, then use that to enable a method breakpoint? – Mike Strobel Apr 23 '18 at 17:03
  • @MikeStrobel I thought about it but unfortunately, even for line numbers that are within the range of the length of the file, IntelliJ doesn't let me add a breakpoint on some lines (ex: a line that only has a closing brace) even though it corresponds to an actual instruction in the real file. And unfortunately the only other place within range where I could break on, is a line with just a brace. – qwertzguy Apr 23 '18 at 17:23
  • 1
    You may be left with setting a method breakpoint then. It's _possible_ that IntelliJ will let you use the step function to advance line-by-line (according to the original source) once you enter the method. I can't be sure, but it's worth a try (you may need to try this both with and without the decompiler plugin enabled). – Mike Strobel Apr 23 '18 at 17:29
  • @MikeStrobel it's actually what I ended up doing, but the problem is IntelliJ doesn't let you set a breakpoint condition based on the method parameters unfortunately. So I had to break for every call and manually continue or step through based on the parameters. – qwertzguy Apr 23 '18 at 20:12
  • Whoever -1'ed the question, I would appreciate a comment explaining why. – qwertzguy Apr 29 '18 at 22:42

1 Answers1

0

I don't know if this actually works, but here's a workaround I would try.

  1. Go to the class file (i.e. the automatically decompiled source of it)
  2. Add a line breakpoint on a random line where a line breakpoint is allowed
  3. Go to the .iws file of your project (project_name.iws)
  4. Search for the name of the file you just added a breakpoint and find the right <line-breakpoint> tag
  5. Modify the <line> tag to be one less than the line you need (e.g. enter 844 if you need a breakpoint on 845).
  6. Save and invoke View Breakpoints to see a breakpoint is present on the correct line.

Now try and debug your program and see if it hits the breakpoint (<-- this part I haven't actually tested).

Another option may be to copy and paste the decompiled source into the right package structure in some directory somewhere. Attach this directory as source for the library you need to debug. You can then add breakpoints to the source you have created, while adding as many linebreaks as needed to the file to be able to set a breakpoint on the correct line.

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
  • It's a good idea, unfortunately it does not work. If I set it past the end of the decompiled file, it doesn't actually set the breakpoint in the JVM (I tried with a line that I can hit after stepping after a method breakpoint). If I put it on a line within the file, it doesn't actually set it to the line number I specified, it tries to find the closest point matching the decompiled code (I can see when the breakpoint is hit, that the line in the stacktrace is different and the local variables are different too). – qwertzguy Apr 30 '18 at 17:58
  • Sorry to hear that. Attaching source for the library (my second suggestion) also does not work? – Bas Leijdekkers May 01 '18 at 08:39