Thursday, October 1, 2009

Traversing "Huge" Source Code Efficiently

It's inevitable that as a software developer, you will encounter such an enormous amount of code and tasked to modify it to siut the goal that you (or the project manager) have set. Of course, you will ask, why don't use Ctags or something like that? Yeah, of course I use Ctags, but sometimes it's just not fine-grained enough or simply refer to the wrong definition of variables or functions and moreover it can't handle assembly source code reliably.

I have made two bash scripts to cope with this problem. The first script helps you in finding a word (usually function names or symbols in assembler source code):

#!/bin/bash
#
# Script to find a word in an subversion checkout
#
#

SEARCH_PATH=""
WORD=""

if [ $# -ne 2 ]; then
echo "Usage: $(basename $0) path_to_search word"
exit 1;
fi

SEARCH_PATH=$1
WORD=$2

find ${SEARCH_PATH} -type f -name '*' -exec grep -nH "\<${WORD}\>" \{} \; | awk '$0 !~ /\.svn/ {print}'



The second script is used to find a string:

#!/bin/bash
#
# Script to find a string in an subversion checkout
#
#

SEARCH_PATH=""
WORD=""

if [ $# -ne 2 ]; then
echo "Usage: $(basename $0) path_to_search word"
exit 1;
fi

SEARCH_PATH=$1
WORD=$2


find ${SEARCH_PATH} -type f -name '*' -exec grep -nH "${WORD}" \{} \; | awk '$0 !~ /\.svn/ {print}'

What you need to run both of the previous scripts are find, grep, awk and a bash compatible shell.

Another trick is to search where the symbol/function-name in the compiled object file. More on that later.
Post a Comment

No comments: