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 6175199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:52:59+00:00 2026-05-23T23:52:59+00:00

On x86 (either 64-bit or 32-bit) Linux — for example: void signal_handler(int) { //

  • 0

On x86 (either 64-bit or 32-bit) Linux —
for example:

void signal_handler(int) {
   // want to know where the program is interrupted ...
}

int main() {
    ...
    signal(SIGALRM, signal_handler);
    alarm(5);
    ...
    printf(...); <------- at this point, we trigger signal_handler
    ...
}

In signal_handler, how can we know we are interrupted at printf in main()?

  • 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-23T23:53:02+00:00Added an answer on May 23, 2026 at 11:53 pm

    Use sigaction with SA_SIGINFO set in sa_flags.

    Prototype code:

    #define _GNU_SOURCE 1  /* To pick up REG_RIP */
    #include <stdio.h>
    #include <signal.h>
    #include <assert.h>
    
    static void
    handler(int signo, siginfo_t *info, void *context)
    {
        const ucontext_t *con = (ucontext_t *)context;
    
        /* I know, never call printf from a signal handler.  Meh. */
        printf("IP: %lx\n", con->uc_mcontext.gregs[REG_RIP]);
    }
    
    int
    main(int argc, char *argv[])
    {
        struct sigaction sa = { };
        sa.sa_flags = SA_SIGINFO;
        sa.sa_sigaction = handler;
        assert(sigaction(SIGINT, &sa, NULL) == 0);
        for (;;);
        return 0;
    }
    

    Run it and hit Ctrl-C. (Use Ctrl-\ to terminate…)

    This is for x86_64. For 32-bit x86, use REG_EIP instead of REG_RIP.

    [edit]

    Of course, if you are actually in a library function (like printf) or a system call (like write), the RIP/EIP register might point somewhere funny…

    You might want to use libunwind to crawl the stack.

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

Sidebar

Related Questions

I want to write code that compiles easily for either 32 or 64 bit
I have a C# project that needs to compile as either x86, or x64,
Either v3.5 or v4.0...it seems that there is either an x86 version or x64
If I target the x86 platform for my .NET app, will it run properly
I've built the x86 Boost libraries many times, but I can't seem to build
Is there such a thing as an x86 assembler that I can call through
I believe the architecture type (x86 vs x64) is abstracted away for you when
can an x64 computer have both the x86 and x64 .net runtime installed? If
Evey now and then I see both x86 and x64 versions of a .NET
Is it possible to install the x86 Remote Debugger as a Service on a

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.