从另一个非进程向日志发送信号到线程,日志堆栈未发生 [英] Sending signal to thread from annother non-process and logging stack not happening
问题描述
我正在尝试从另一个进程(不是从创建该线程的进程向POSIX线程发送信号.我使用kill(...):: p来发送信号的操作是
I am trying to send signal to a POSIX thread from another process (Not from the process that created that thread. What I did to send signal using kill(...)::
int trap_handle(pid_t child_waited )
69 {
70 printf("%s, new value: %d, child_waited=<%ld>\n", __func__,g_var_x, child_waited);
71 int errno_ = -1;
72 errno_ = kill(child_waited, SIGUSR1);
73 //syscall(SYS_tgkill, -1, child_waited, SIGUSR1);
74 //errno_ = pthread_kill(child_waited, SIGUSR1);
75 if(0==errno_)
76 printf("Signal sent to thread: %ld\n", child_waited);
77 else
78 printf("pthread_kill failed: error:%d", errno_);
79 }
并在注册SIGUSR1的线程中:
And in a thread that registered SIGUSR1:
230 void baz() {
231 g_var_x++;
232 }
233
234 void bak() { baz(); }
235 void bar() { bak(); }
236 void foo() { bar(); }
237
238 void stack_dump()
239 {
240 printf("******trap() entry ******\n");
241 void *array[100];
242 size_t size;
243 // get void*'s for all entries on the stack
244 size = backtrace(array, 100);
245
246 // print out all the frames to stderr
247 // fprintf(stderr, "Error: signal %d:\n", sig);
248 backtrace_symbols_fd(array, size, STDERR_FILENO);
249 printf("*******trap() exit ******\n");
250 }
251
252 void* thread_proc_one(void *lParam)
253 {
254 printf("--Entry: thread_one debugee tid<%ld> \n", syscall(SYS_gettid));
255 g_arg_params.debugee_tid = syscall(SYS_gettid);
256
257 struct sigaction trap_action;
258 //printf("Childprocess <tid> %d\n", syscall (SYS_gettid));
259 memset(&trap_action, 0, sizeof(trap_action));
260 sigaction(SIGUSR1, NULL, &trap_action);
261 trap_action.sa_sigaction = stack_dump;
262 trap_action.sa_flags = SA_SIGINFO | SA_RESTART | SA_NODEFER;
263 sigaction(SIGUSR1, &trap_action, NULL);
....
现在,这预计将回溯Thraed堆栈,而不是调用它的主进程.但是没有发生. stack_dump被调用,但不是记录线程堆栈,而是记录其父堆栈. Backtrace显示了创建该thread_proc_one线程的进程的堆栈.
Now This is expected that it will backtrace the Thraed stack not the main process that invoked it. But is not happening. stack_dump is called, but instead of logging thread stack, it is logging its parents stack. Backtrace is showing stack of the process that created this thread_proc_one thread.
这里有人遇到这个问题吗? 希望我清楚.
Anyone here faced this issue? Hope I am clear.
推荐答案
sigaction()
为整个过程安装一个信号处理程序.
sigaction()
installs a signal handler for the whole process.
来自 man sigaction
(斜体字):
sigaction()系统调用用于更改进程在接收到特定信号后采取的操作.
The sigaction() system call is used to change the action taken by a process on receipt of a specific signal.
该进程的线程中负责处理的全部留给操作系统.
Which of the process' threads handles it is left to the OS.
来自 man 7 signal
:
信号处理是每个进程的属性:在多线程中 在应用中,特定信号的配置对于 所有线程.
The signal disposition is a per-process attribute: in a multithreaded application, the disposition of a particular signal is the same for all threads.
要确保某个信号由特定线程处理,请使用 pthread_sigmask()
屏蔽所有线程的信号,但处理线程除外.
To make sure a certain signal is handled by a specific thread use pthread_sigmask()
to mask out the signal for all threads but the one to handle it.
再次从 man 7 signal
:
一个进程中的每个线程都有一个独立的信号掩码, 指示线程当前正在阻塞的信号集. 线程可以使用 pthread_sigmask(3)来操纵其信号掩码.
Each thread in a process has an independent signal mask, which indicates the set of signals that the thread is currently blocking. A thread can manipulate its signal mask using pthread_sigmask(3).
例如,可以通过调用 pthread_sigmask()
在主线程中先屏蔽有问题的信号,然后再创建任何线程,然后在线程内部处理信号调用
So this, for example, could be done by callîng pthread_sigmask()
in the main thread masking out the signal in question prior to creating any thread, and then inside the thread to handle the signal call pthread_sigmask()
again to unmask the signal to be handled.
这篇关于从另一个非进程向日志发送信号到线程,日志堆栈未发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!