Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4107070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:27:26+00:00 2026-05-20T21:27:26+00:00

My question is somewhat different from others that have asked about fault addresses. I’m

  • 0

My question is somewhat different from others that have asked about fault addresses. I’m trying to implement a horrible hack to determine, from a signal handler, whether the signal interrupted a syscall or ordinary user code by inspecting the code at the saved instruction pointer and comparing it against the possible syscall entry instructions for the host architecture it’s running on. This is part of implementing correct POSIX thread cancellation that does not suffer from the race condition and resource leak described in my old question:

How are POSIX cancellation points supposed to behave?

If this approach is unreliable or otherwise wrong, I’d also like to hear reasons.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:27:27+00:00Added an answer on May 20, 2026 at 9:27 pm
    /* sigsegv.c */
    /**
     * This source file is used to print out a stack-trace when your program
     * segfaults. It is relatively reliable and spot-on accurate.
     *
     * This code is in the public domain. Use it as you see fit, some credit
     * would be appreciated, but is not a prerequisite for usage. Feedback
     * on it's use would encourage further development and maintenance.
     *
     * Due to a bug in gcc-4.x.x you currently have to compile as C++ if you want
     * demangling to work.
     *
     * Please note that it's been ported into my ULS library, thus the check for
     * HAS_ULSLIB and the use of the sigsegv_outp macro based on that define.
     *
     * Author: Jaco Kroon <jaco@kroon.co.za>
     *
     * Copyright (C) 2005 - 2010 Jaco Kroon
     */
    #ifndef _GNU_SOURCE
    #define _GNU_SOURCE
    #endif
    
    /* Bug in gcc prevents from using CPP_DEMANGLE in pure "C" */
    #if !defined(__cplusplus) && !defined(NO_CPP_DEMANGLE)
    #define NO_CPP_DEMANGLE
    #endif
    
    #include <memory.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <signal.h>
    #include <ucontext.h>
    #include <dlfcn.h>
    #ifndef NO_CPP_DEMANGLE
    #include <cxxabi.h>
    #ifdef __cplusplus
    using __cxxabiv1::__cxa_demangle;
    #endif
    #endif
    
    #ifdef HAS_ULSLIB
    #include "uls/logger.h"
    #define sigsegv_outp(x)         sigsegv_outp(,gx)
    #else
    #define sigsegv_outp(x, ...)    fprintf(stderr, x "\n", ##__VA_ARGS__)
    #endif
    
    #if defined(REG_RIP)
    # define SIGSEGV_STACK_IA64
    # define REGFORMAT "%016lx"
    #elif defined(REG_EIP)
    # define SIGSEGV_STACK_X86
    # define REGFORMAT "%08x"
    #else
    # define SIGSEGV_STACK_GENERIC
    # define REGFORMAT "%x"
    #endif
    
    static void signal_segv(int signum, siginfo_t* info, void*ptr) {
        static const char *si_codes[3] = {"", "SEGV_MAPERR", "SEGV_ACCERR"};
    
        int i, f = 0;
        ucontext_t *ucontext = (ucontext_t*)ptr;
        Dl_info dlinfo;
        void **bp = 0;
        void *ip = 0;
    
        sigsegv_outp("Segmentation Fault!");
        sigsegv_outp("info.si_signo = %d", signum);
        sigsegv_outp("info.si_errno = %d", info->si_errno);
        sigsegv_outp("info.si_code  = %d (%s)", info->si_code, si_codes[info->si_code]);
        sigsegv_outp("info.si_addr  = %p", info->si_addr);
        for(i = 0; i < NGREG; i++)
            sigsegv_outp("reg[%02d]       = 0x" REGFORMAT, i, ucontext->uc_mcontext.gregs[i]);
    
    #ifndef SIGSEGV_NOSTACK
    #if defined(SIGSEGV_STACK_IA64) || defined(SIGSEGV_STACK_X86)
    #if defined(SIGSEGV_STACK_IA64)
        ip = (void*)ucontext->uc_mcontext.gregs[REG_RIP];
        bp = (void**)ucontext->uc_mcontext.gregs[REG_RBP];
    #elif defined(SIGSEGV_STACK_X86)
        ip = (void*)ucontext->uc_mcontext.gregs[REG_EIP];
        bp = (void**)ucontext->uc_mcontext.gregs[REG_EBP];
    #endif
    
        sigsegv_outp("Stack trace:");
        while(bp && ip) {
            if(!dladdr(ip, &dlinfo))
                break;
    
            const char *symname = dlinfo.dli_sname;
    
    #ifndef NO_CPP_DEMANGLE
            int status;
            char * tmp = __cxa_demangle(symname, NULL, 0, &status);
    
            if (status == 0 && tmp)
                symname = tmp;
    #endif
    
            sigsegv_outp("% 2d: %p <%s+%lu> (%s)",
                     ++f,
                     ip,
                     symname,
                     (unsigned long)ip - (unsigned long)dlinfo.dli_saddr,
                     dlinfo.dli_fname);
    
    #ifndef NO_CPP_DEMANGLE
            if (tmp)
                free(tmp);
    #endif
    
            if(dlinfo.dli_sname && !strcmp(dlinfo.dli_sname, "main"))
                break;
    
            ip = bp[1];
            bp = (void**)bp[0];
        }
    #else
        sigsegv_outp("Stack trace (non-dedicated):");
        sz = backtrace(bt, 20);
        strings = backtrace_symbols(bt, sz);
        for(i = 0; i < sz; ++i)
            sigsegv_outp("%s", strings[i]);
    #endif
        sigsegv_outp("End of stack trace.");
    #else
        sigsegv_outp("Not printing stack strace.");
    #endif
        _exit (-1);
    }
    
    static void __attribute__((constructor)) setup_sigsegv() {
        struct sigaction action;
        memset(&action, 0, sizeof(action));
        action.sa_sigaction = signal_segv;
        action.sa_flags = SA_SIGINFO;
        if(sigaction(SIGSEGV, &action, NULL) < 0)
            perror("sigaction");
    }
    
    $ g++ -fPIC -shared -o libsigsegv.so -ldl sigsegv
    
    $ export LD_PRELOAD=/path/to/libsigsegv.so
    

    I found this code on a LUG. Couldn’t get to the page to point the URL here, so pasted the whole code. This code prints a small stack trace when SIGSEGV occurs. Not sure if there is some other way that does not use ucontext_t.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a somewhat silly question, if i have a series of processes that are
This question is somewhat similar to How to combine two branches from two different
This is a somewhat different question about Tomcat deployment. It has been covered partially
I'll rewrite a different question of mine, because the problem case somewhat changed: If
This question is related somewhat to the one i asked HERE . Now, i
This is somewhat of a broad question, but it is one that I continue
This question is not coming from a programmer. (obviously) I currently have a programmer
I have a question about how to design my controllers properly. Project is fairly
I have a thorny question about transforming Linq Expressions. I had a good search
I'm still pretty new to programming so I have somewhat of a noob question.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.