AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.

 View Only
Expand all | Collapse all

how to dump a stacktrace in my trapped signal

  • 1.  how to dump a stacktrace in my trapped signal

    Posted Fri April 18, 2008 07:50 AM

    Originally posted by: SystemAdmin


    Hi,

    I want to dump a stack trace in my program.In linux we will backtrace function for this.Here is the small program to do this. Can anybody tell me how we can do the same in aix.Equivalent code will be very helpful.
    #include <signal.h>
    #include <execinfo.h> //this is not there in AIX

    /* get REG_EIP from ucontext.h */
    #define __USE_GNU
    #include <ucontext.h>

    void bt_sighandler(int sig, siginfo_t *info, void *sigcontext) {

    void *trace16;
    char **messages = (char **)NULL;
    int i, trace_size = 0;
    ucontext_t *uc = (ucontext_t *)sigcontext;

    /* Do something useful with siginfo_t */
    if (sig == SIGSEGV)
    printf("Got signal %d, faulty address is %p, ", sig, info->si_addr);
    else
    printf("Got signal %d\n", sig);

    trace_size = backtrace(trace, 16); //what is equivalent function in AIX5.0
    /* overwrite sigaction with caller's address */
    trace[1] = (void *) uc->uc_mcontext.gregsREG_EIP; // ? in AIX5.0

    messages = backtrace_symbols(trace, trace_size); // ? in AIX5.0
    /* skip first stack frame (points here) */
    printf("path.....\n");
    for (i=1; i<trace_size; ++i)
    printf("bt %s\n", messages);

    exit(0);
    }
    struct sigaction sa;

    sa.sa_sigaction = (void *)bt_sighandler;
    sigemptyset (&sa.sa_mask);

    sa.sa_flags = SA_RESTART | SA_SIGINFO;
    sigaction(SIGSEGV, &sa, NULL);
    sigaction(SIGQUIT, &sa, NULL);
    sigaction(SIGILL, &sa, NULL);
    Can we use mt_trce() here?
    Sharath.


  • 2.  Re: how to dump a stacktrace in my trapped signal

    Posted Fri April 25, 2008 04:05 AM

    Originally posted by: nagger


    Haven't tried it myself but its a documented AIX function.

    http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.basetechref/doc/basetrf1/mt_trce.htm

    Includes a code sample of using this mt__trce() function.
    It also says it works in a signal handler.
    You may have to install a AIX package to get the library /lib/libptools_ptr.a installed

    Note the double underline in this function name.

    Let us know how you get on.

    Hope this helps, N