Find & Replace in Files on Linux

Code Monkey 2 Comments

TuxA lot of solutions I’ve found for recursively replacing text in files is implemented using shell scripts, perl, php, or some other inconvenient way. Rushi got it right by using the Linux command line. Here it is (slightly modified) from his blog:

find . -name “*.cpp” -print | xargs sed -i ’s/[find]/[replace]/g’

where “[find]” and “[replace]” are the things you are searching for and substituting.

To search files with multiple file extensions, use:

find . -name “*.cpp” -o -name “*.h” -o -name “*.c” | xargs sed -i ’s/[find]/[replace]/g’

ADDED 4-13-2009: See comments for other variations.

StackHash and Application Crashes on Windows

Code Monkey 6 Comments

Software BugWe got an interesting application crash yesterday with a confusing message similar to this:

Fault bucket 42424242, type 1
Event Name: APPCRASH
Response: None
Cab Id: 0

Problem signature:
P1: MyApp.exe
P2: 1.42.42.42
P3: 598773cf
P4: StackHash_ac62
P5: 0.0.0.0
P6: 00000000
P7: c0000007
P8: 00000000
P9:
P10:

We spent some time wondering if our crypto libraries were the problem (we just made some changes recently), but concluded that was unlikely. So what the heck is the “StackHash” module? Did our trashed stack cause the kernel to think we were a different module? Nope.

The answer is that the Windows executive couldn’t identify the module we were in when the application crashed (it uses the instruction pointer to determine what code was executing). In this case, the kernel simply takes a hash of the stack so at least we might be able to identify if we’ve seen this exact crash before. Here’s the answer summarized by an engineer from Microsoft:

In the OS when I try to get a faulting module name it is possible that there is no module laoded (sic) at that address. For example in this case the EIP was zero. So in those cases where a module is not loaded and it is not also in the unloaded module list, I take a stack hash of the stack so that we can identify this crash from other crashes where also the module is not known.

The “Analogy” Screensaver

Oh So Random, Tech and Security 1 Comment

I’ve been jealous of Rob’s screensaver for awhile now. I thought it was Mac only until I asked him about it… nope. I installed the Windows version today. What a beautiful piece of art! The creator, Jesson Yip, describes it like this:

Analogy is a typographic clock which fuses the immediacy of digital with the visual-spatial quality of analogue into a hybrid format. It presents an everyday object with a fresh twist.

Click on the image below to visit his site and download it. Enjoy!

Analogy Screensaver

How To Get An Error Message Describing Why A Library Failed To Load

Code Monkey No Comments

The title’s kind of a misnomer. This post is really to help me remember how to get a human-readable string from a Windows error code… I’m finally tired of always having to look it up :) . However, my current situation revolves around determining why a DLL (or *.so on Linux) failed to load, so that’s why this post it titled the way it is.

I like to disable the annoying default dialog that pops up in Windows when a library fails to load.

SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOOPENFILEERRORBOX);

Now, here’s the code to get a user friendly text string:

#ifdef WIN32
   LPVOID pStr = 0;
   DWORD_PTR args[1] = { (DWORD_PTR)pFilename };
   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
      FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_ARGUMENT_ARRAY,
      NULL,
      GetLastError(),
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      (LPTSTR)&pStr,
      0,
      (va_list*)args);

   // TODO: Do something with the string pStr here.

   LocalFree(pStr);
#else
   // TODO: Call dlerror() and do something with the string.
#endif

The Harsh Reality: Obama’s First Online Town Hall

Government, The Harsh Reality No Comments

Presidential SealWelcome to the first of (what I hope are) many posts where I share what I see as the real motives behind many of the events in our daily lives.

First up: The first every online Presidential Town Hall. Cool, huh? Obama bringing politics into the 21st century. Saving the government money by not traveling. Helping the environment by not using fuel on Air Force One. Giving the ‘little guy’ easier access to the President. This is all well-and-good, but what’s the real motive?

Unfortunately, I think it’s to collect names, addresses, and email for future political campaigns. To submit questions and vote on the ones President Obama will answer, you need to enter your contact information. This information will be used to contact people to try and gain support for future legislation, candidates, and the Presidents own re-election. As of right now, 92,927 have participated… seems to have worked.

How to Recursively Delete Subversion .svn Directories

Code Monkey 1 Comment

Subversion LogoOk, some posts are clearly just to help me remember how to do things… this is one of them. The Subversion source control system keeps private information in .svn directories. There is one such directory for EVERY directory in your source tree. Here’s how you recursively delete ALL the .svn directories from the current directory in Linux (or Cygwin in Windows).

rm -rf `find . -type d -name .svn`

NOTE: Those are back ticks around the ‘find’ command, not apostrophes. I recommend you just run the ‘find’ command first and verify it is listing the directories you expect.

Marcus Miller: Live at Paradiso 2007

Tunes and Grooves No Comments

I finally found the full Marcus Miller concert that includes the song “Blast” which I linked to earlier. Unfortunately, the video has been removed from YouTube. Here it is from FabChannel.

UPDATE 3-17-2009: FabChannel is no longer around :-( Click HERE to download a clip from another concert (*.mp4 file playable in QuickTime).

Marcus Miller

Click HERE to visit Marcus Miller’s website.

Making the Definition of Life and Death Consistent

Carrying the Cross No Comments

I like things that make sense. I’m definitely a pragmatist. Regardless of the principles behind the abortion debate, I’ve always had a problem with the inconsistent definitions of life and death. So I put this question to a friend (and fellow musician) who happens to work for Central California Right To Life.

Josh,

An important issue in the abortion debate is the argument over when life begins; at conception, at birth, or somewhere in between? I’ve heard many opinions over the years, but one (from a fellow engineer) has always struck me as a possible solution. The logic of the argument goes like this: ‘life’ and ‘death’ are inverse states of existence. Therefore, the set of criteria that define ‘life’ and ‘death’ should also be inverse quantities. Thus, we should define ‘life’ by the inverse of the criteria we use for ‘death’, since those criteria are pretty well accepted both legally and morally (of course there are always edge-cases). For example: if someone is pronounced dead when their heart and brain consciousness stop, then life is defined as conscious brain function and a heart beat (over-simplified of course).

This is a very pragmatic argument. It view’s the woman’s body as analogous to a life-support system, thus rendering null the argument that life begins when the baby can exist outside the mother’s womb. This is an important step, because technology renders this a moving target… at some point, an artificial womb may be able to carry an embryo to full term. I’d be interested to get your thoughts on this.

-Tom

Josh responded to my question on his video podcast LifeReport.

Thanks Josh for the response, but you are too gracious with the praise! :-)

(requires Adobe Flash plugin… click HERE to watch it on YouTube)

Caden Thomas Distler

Oh So Random 2 Comments

What a wonderful gift to bring home on Christmas!

Caden Thomas Distler

How to Print a Stack Backtrace Programatically in Linux

Code Monkey 1 Comment

GNU LogoSo here’s a cool feature of GNU’s implementation of libc: you can get a stack backtrace (as an array of strings) dynamically in your code. This can be really useful when trying to determine the code path taken when an error occurs. Most times, it’s faster to just run the code in a debugger and use it to display a backtrace, but there are instances when doing it programmatically is your best option. For example, you could get a backtrace in your application’s exception handler and use it to augment error log messages.

First, you need to include execinfo.h to your code:

#include <execinfo.h>

Next, call the backtrace() function to get an array of void pointers that represents the current stack (the pointers are the return addresses for each stack frame).

void* tracePtrs[100];
int count = backtrace( tracePtrs, 100 );

The backtrace() function returns the number of entries in the array (read the man pages for more info about the array size).

Finally, you need to resolve the function names associated with the pointers. You have 2 options: backtrace_symbols() and backtrace_symbols_fd(). Both of these methods resolve the pointers to strings, but the difference is that backtrace_symbols() allocates the strings on the heap while backtrace_symbols_fd() writes the strings to a file descriptor that you can read. Just keep in mind that backtrace_symbols() won’t work if the heap has been trashed.

Here’s an example using backtrace_symbols():

char** funcNames = backtrace_symbols( tracePtrs, count );

// Print the stack trace
for( int ii = 0; ii < count; ii++ )
   printf( “%s\n”, funcNames[ii] );

// Free the string pointers
free( funcNames );

NOTE: Make sure you call free() on the array of strings returned from backtrace_symbols().

For more information, here’s a good article from the Linux Journal.

« Previous Entries Next Entries »