2

I have a Java program that at some point eventually creates a string that is matched by "schema.sql". I want to find where in the source code this happens. Searching "schema.sql" on the source code does not yield the location. Instead the program must create the string at runtime. I just don't know where. It happens in some library on my project. How can I find where my program creates this string?

Can I watch the heap at runtime somehow for a string that is matched by "schema.sql"? I looked at things like VisualVM but it looks like it only analyzes heap dumps, not the running state of the heap. There must be a way to do this; any ideas?

David Groomes
  • 2,303
  • 1
  • 21
  • 23
  • Why are you trying to do this? – Sotirios Delimanolis Jun 23 '14 at 01:05
  • Why are you so sure you have the String "schema.sql" somewhere? – Elliott Frisch Jun 23 '14 at 01:08
  • @ElliottFrisch I have a library on my project which executes SQL scripts on my classpath titled `"schema.sql"`. It would follow that the library probably constructs some string that specifies a path to this file. That string would be matched by `"schema.sql"`. In fact, I found it at runtime while debugging and the string is `"classpath*:schema-all.sql,classpath*:schema.sql"`. Basically, I'm wishing for an enhanced conditional breakpoint that analyzed all Strings in the program and not just something like `x == "foo"`. – David Groomes Jun 23 '14 at 02:05
  • What you're asking is to examine every object that your program comes into contact with and query "instanceof String" to create a breakpoint hook there. You're better off trying to tackle your problem in a different way. Print statements all over to explain its current stage, or guess what it is and comment bits of code out until the problem disappears. – Perry Monschau Jun 23 '14 at 02:33
  • @PerryMonschau I hear you, so what I want is more expensive than I imagined. It's true I would fall back on other investigative techniques to figure it out, like you said. It's just that I have 60 libraries and a very simple and complete description of what I want; so I wish I could tell my IDE or write some code to do this. Fwiw just two weeks ago someone had an essentially identical question, unanswered at http://stackoverflow.com/questions/24135716/eclipse-conditional-breakpoint-break-at-any-string-equals – David Groomes Jun 23 '14 at 03:48
  • You mentioned that it matched a schema.sql? What's doing the matching, and how do you know it matches? – Perry Monschau Jun 23 '14 at 04:14

1 Answers1

2

You can do that with JProfiler with the following steps:

1) Start allocation recording at startup by configuring an appropriate allocation recording profile in the session startup dialog.

enter image description here

2) Take a heap snapshot after you think that the string has been created

3) Double-click on the java.lang.String row in the classes view of the heap walker to create a new object set with all strings

4) Switch to the "Outgoing references view"

5) Search for your string by choosing "Apply filter->By restricting the selected value" and entering the string value

enter image description here

6) Switch to the allocations view and check out the stack trace

enter image description here

Disclaimer: My company develops JProfiler.

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102