• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Stack Backtrace Demo Code For Free
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Stack Backtrace Demo Code For Free


  • Subject: Stack Backtrace Demo Code For Free
  • From: Rush Manbert <email@hidden>
  • Date: Fri, 21 Apr 2006 13:04:23 -0700

Hi all,

I got help from a few people when I was trying to figure this out, so now I'm giving back to the community. Since the code is free, it's worth what you paid for it. There is no warranty, no support, etc. You are free to modify and use as you please.

Attached is a directory archive that contains 5 files. They are:
main.cpp
level1.cpp
level1.h
builtinReturnAddress.cpp
builtinReturnAddress.h

Main just calls a function named level, which is in level1.cpp. It passes the "call level" as zero. The level function prints the call stack, up to 20 levels deep. It calls a function in builtinReturnAddress.cpp to get the return addresses, then uses dladdr() and __cxa_demangle() to generate the display. It then calls itself recursively, which continues until the call depth gets to 10 levels. The call is issued from different places for odd and even values of call level in order to make the stack trace more interesting and informative.

I fiddled with the calls to __builtin_return_address for a while, and finally wrapped them them up, because as far as I can tell, the level argument really needs to be a compile time constant. It makes for a nasty switch statement with a compile time decision about the max number of levels you can trace, and I would be happy to hear from someone who can do something more elegant.

Another disclaimer:
This is test code that I hacked together yesterday and today, so it's not polished. I'll probably turn it into a class for my own use, but I haven't decided yet and I need to work on something else for a while. That's why I'm posting this in its current state. I'm getting old and I might forget to do it later. :-)


How to use:
I can't provide a project for you because I'm running a *different* version of Xcode that was mentioned here a little while ago.


To use this code, make a Xcode project for a C++ command line tool. Copy all of the files into the directory where your *.xcodeproj is, overwriting the original main.cpp. Add level1.cpp and builtinReturnAddress.cpp to the project. I think that's all that is required. It should build and run.

Thanks again to Eric Albert, Steve Sisak, and Steve Checkoway for their help. (See the "Why no execinfo.h" thread)

Regards,
Rush

Attachment: StackBacktraceFiles.zip
Description: Zip archive

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Stack Backtrace Demo Code For Free
      • From: Greg Hurrell <email@hidden>
  • Prev by Date: Re: huge temporary files
  • Next by Date: Fwd: Database Development
  • Previous by thread: Re: huge temporary files
  • Next by thread: Re: Stack Backtrace Demo Code For Free
  • Index(es):
    • Date
    • Thread