debugging technique
Dec. 3rd, 2006 07:59 amDear LiveJournal Genie,
Suppose that whenever I click the mouse at a certain widget, the file named "a.txt" gets re-read into my program. I want to figure out how this is happening (i.e. which function is reading the file), so I look at the event handler for the click, but the code is unreadable, and it calls 20 other functions, each of which call 20 functions, etc.
What's a quick way to find out what function is loading the file?
Suppose that whenever I click the mouse at a certain widget, the file named "a.txt" gets re-read into my program. I want to figure out how this is happening (i.e. which function is reading the file), so I look at the event handler for the click, but the code is unreadable, and it calls 20 other functions, each of which call 20 functions, etc.
What's a quick way to find out what function is loading the file?
(no subject)
Date: 2006-12-03 08:58 am (UTC)(no subject)
Date: 2006-12-03 08:59 am (UTC)(no subject)
Date: 2006-12-03 09:01 am (UTC)Suggestions
Date: 2006-12-03 10:06 am (UTC)Such as: derive GustavoFile from File, and then make the whole system use GustavoFile. On File::Open, before you pass through to the base class check to see if the name of the file is "a.txt". You can put whatever conditional code you need in there to catch the situation in question.
A tool that will help you get a view of what's going on in some cases, though not necessarily this one, is FileMon. It doesn't generate breakpoints but can give you more information about when files are accessed and by what processes.
(no subject)
Date: 2006-12-03 03:46 pm (UTC)Again on Unix, you can interpose on open() and dump core there; then if you run in the debugger you will have gotten yourself a stack trace.
Modern languages are easier to do this in -- apparently you can croak() on perl in File::Open (I gather that's what the other person is saying to do); in Java you provide your own... I forgot what class opens files, but you provide your own one of those, raise an exception, and viola, a big violin.
(no subject)
Date: 2006-12-03 03:49 pm (UTC)(no subject)
Date: 2006-12-03 03:50 pm (UTC)"the program uses some library you have no access to that then in turn opens your file"
Re: Suggestions
Date: 2006-12-03 04:16 pm (UTC)The problem is precisely identifying the function that opens the file. The code really is obfuscated, and reading it is an exponential problem.
Searching for the string "a.txt" gives too many hits.
Also, the whole program runs in the same process, so FileMon won't give much information.
Re: Suggestions
Date: 2006-12-03 04:19 pm (UTC)Of course, this will only work if the code really is using File (or whatever classes I make wrappers for).
(no subject)
Date: 2006-12-03 04:27 pm (UTC)(no subject)
Date: 2006-12-03 04:36 pm (UTC)(no subject)
Date: 2006-12-03 04:39 pm (UTC)(no subject)
Date: 2006-12-03 04:40 pm (UTC)1. search for file-open statements in the code, note their classes/functions
2. search for the classes/functions and note where they are called from
3. add debug variables to step 2
4. repeat steps 2 and 3
Then I can at least know where the calls are being made, and I can debug any of the code along the way. It may not be that simple if any functions are called recursively of if the data structures are very complex.
(no subject)
Date: 2006-12-03 05:15 pm (UTC)During any given run, about 15 of those lines will be executed. And during my clicking, only 1 gets executed: namely the one I'm interested in.
(no subject)
Date: 2006-12-03 06:41 pm (UTC)How to implement your own little java.io.File I don't quite remember. In C you have to interact directly with the dynamic linker; in Java there must be a way to open up a library (given its filename) and specifically inspect it -- I would assume this is, in fact, much better supported than in C.