Using Linux signal SIGUSR1 to debug embedded program __linux

Source: Internet
Author: User

Linux embedded due to a number of limitations, debugging methods are limited, often appear to face bugs helpless situation, now introduced a signal processing of Linux embedded applications debugging methods.

Linux in a total of 32 kinds of signals, in the/usr/include/bits/signum.h header file can be seen, specifically as follows: Sighup; SIGINT; Sigquit; Sigill; Sigtrap; SIGABRT; Sigiot; Sigbus; SIGFPE; SIGKILL; SIGUSR1; SIGSEGV; SIGUSR2; Sigpipe; SIGALRM; Sigterm; Sigstkflt; SIGCLD; SIGCHLD; Sigcont; SIGSTOP; SIGTSTP; Sigttin; Sigttou; Sigurg; SIGXCPU; Sigxfsz; SIGVTALRM; Sigprof; Sigwinch; Sigpoll; Sigio; SIGPWR; Sigsys; Sigunused

Where the SIGUSER1 signal user can define their handling behavior, the processing examples are as follows:

#include <stdio.h>  
#include <signal.h>  
void signal_handle (int sig_num)  
{  
    if (Sig_num = = SIGUSR1)  
    {  
        printf ("Capture sigusr1\n");  
    }  
    printf ("Signal_handle running ... \ n");  
}  

int main (int argc,char **argv)  
{  
    signal (SIGUSR1, signal_handle);  
    while (1)   
    {sleep  
        ();  
    }  
    return 0;  
}

The Signal_handle () function can be executed by sending a SIGUSR1 signal to the above process:

#向指定的pid进程发送SIGUSR1信号
kill-s SIGUSR1 pid

For the characteristics above, we designed a signal processing function, if received SIGUSR1 signal, our application will be a bit of flag bit true, once again received false, to control the application by sending signal to run the purpose of debugging the application. The signal processing function is designed as follows:

#include <stdio.h>  
#include <signal.h>  

//This variable can be used in the application
static BOOL flag = FALSE;
void Signal_handle (int sig_num)  
{  
    if (SIGUSR1!= sig_num)
    {return
        ;
    }

    Flag = flag? false:true;     
}  

int main (int argc,char **argv)  
{  
    signal (SIGUSR1, signal_handle);  
    while (1)   
    {
        if (flag)
        {
            printf (' flag is true!\n ');
        }
        else
        {
            printf ("Flag is false\n");
        }
        Sleep (5);  
    }  
    return 0;  
}

As above, we can control the operation of the program during the operation of the branch to achieve the purpose of controlling the application.

In addition to multithreading problem, the thread card dead situation (thread running state can not respond to the real situation), you can increase the method, through the thread run path to add a print log method to detect whether the thread is actually running.

if (flag) {debug ("Thread is running!\n");} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.