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");}