Debugger Tool Belt: BBEdit (...or whatever your favourite text editor is)
Understanding the debugging process deeply is the key to becoming a master debugger. However, any master debugger will have a set of go-to tools to elevate their game. While it is common to write simple tools during debugging, using COTS products that are focused on doing one thing well can greatly accelerate resolution of problems.
In this post, I will focus on a tool I use all the time: BBEdit, by Bare Bones Software.
Because I am a Mac guy, I am focusing on BBEdit, but you can insert your favourite text editor into this discussion and hopefully map its equivalent features.
The power of text
Text files are the lingua franca of software. Your source code is in text files, your log files are (generally) in text files, and just about any high-level diagnostic tool will allow its data to be exported into text files of some form.
For this reason, being able to manipulate text quickly, easily, and flexibly is a debugging power move.
To be able successfully process text data, you need an editor that will:
- Be able to consume and efficiently work with large files.
- Be able to do advanced searching using grep-style (regular expression) patterns.
- Be able to do advanced sorting using grep-style (regular expression) patterns.
- Be able to do (2) and possibly (3) on multiple files at once.
- Be able to do smart comparisons between files.
I'll drill into these a bit more, showing how BBEdit helps me solve problems.
A good text editor for debugging needs to handle large files. Log files collected over an extended period of time, especially with elevated debug levels, can be very large and your text file cannot choke on them.
BBEdit can easily chew through files of hundreds of megabytes, and even over 1Gb. If your files are much bigger than that, you might consider pre-filtering your data using grep or, at least, getting it open in BBEdit and then grepping the content into a smaller file for deeper analysis from there.
Related to file handling is file differencing. This can be important when you are running side-by-side tests and looking at log output to see what has changed. BBEdit makes this easy to do, while filtering out whitespace and such. This is a very common thing to do, so there is nothing magic about BBEdit here. But if your editor of choice does not do that in a simple and intuitive way, find another editor.
Search and sort
I put searching and sorting together because both should rely on a common matching algorithm that is, ideally, based on grep-style regular expressions. Yes, that grep. It has taken me a long time to warm to regular expressions but because they are used everywhere it was a sink-or-swim kind of thing. Now that I am fairly comfortable with them, I strongly prefer them.
To be totally up front about it, I can't say that I am an expert at regular expressions (who is?) and so I rely heavily on sites like https://rubular.com to help me out.
BBEdit has really nice feedback as you construct your patterns, and highlights text as you modify the pattern. This lets you immediately know you are selecting the right thing. One benefit of something like rubular.com, though, is the little cheat sheet at the bottom of the page if your grep fu is not as strong as it could be.
But search goes beyond just finding words in the file. Two huge features in BBEdit that build off the regular expression patterns are line matching and sorting, both of which I use heavily.
For line matching, there is the "Process Lines Containing..." menu item, which allows you to pull lines out of the file and copy them to the clipboard or a new document in one fell swoop:
This command is awesome for high-level filtering of your data, especially if you have a very large initial file. It is especially useful when you have multiple files and are pulling line matches from each of them; use "Copy to clipboard" on each file and simply paste into a new document that has the consolidated lines.
The other killer tool is "Sort Lines...":
The power move here is that you can match text from the middle of each line and use it as the sorting key. In the sample above, StartDateTime was a Unix-based timestamp that, being numerical, allowed me to easily sort by that value as opposed to the timestamp on the log row itself.
I cannot overstate how powerful this is. In a recent example, because the log file in question had data from various StartDateTime values interleaved but sorted by "log time", I could not understand the root of the issue I was debugging. But by sorting using a value embedded in the log lines, I was able to immediately see the problem because the human eye is really good at seeing patterns and, in this case, huge runs of replicated values. Yes, I could have pulled this into Excel, written some formulae to parse out values, and then sorted, but...well...Excel.
I should also point out that BBEdit remembers search patterns so, when I had to analyze another log file for a potentially similar problem the next day, I was able to sort, eyeball, and confirm the issue in seconds.
One file, multiple files
I'll mention this for completeness, although I am sure any decent editor will handle this. You need to be able to work with multiple files as easily as one. Search and replace needs to work across files. Sorting less so (and I don't think I can sort multiple files with BBEdit in one command) as long as your criteria are remembered. But whipping through multiple files in the UI has to be effortless.
And while on completeness, look at the other BBEdit-y goodness just in this one menu alone.
While this has been about BBEdit, I can say that much of this functionality is also available in Notepad++ on Windows. The interface is a bit "dense" for my liking, but I am sure that if that is your go-to tool, you'll be as fluent in it as I am in BBEdit.
Whichever tool you use, my advice is that you learn all the tricks it has to offer so that you can maximize your analysis capability beyond what Cmd/Ctl-F can provide.