AlanaPasco 发表于 2025-5-28 22:05:55

聊一聊 .NET Dump 中的 Linux信号机制

一:背景

1. 讲故事

当 .NET程序 在Linux上崩溃时,我们可以配置一些参考拿到对应程序的core文件,拿到core文件后用windbg打开,往往会看到这样的一句信息 Signal SIGABRT code SI_USER (Sent by kill, sigsend, raise),参考如下:
(1.1d): Signal SIGABRT code SI_USER (Sent by kill, sigsend, raise)
libc_so!wait4+0x57:
00007fbd`09313c17 483d00f0ffff    cmp   rax,0FFFFFFFFFFFFF000h
0:023> ? 1d
Evaluate expression: 29 = 00000000`0000001d
0:023> ~29s
*** WARNING: Unable to verify timestamp for libSystem.Native.so
libc_so!read+0x4c:
00007fbd`0933829c 483d00f0ffff    cmp   rax,0FFFFFFFFFFFFF000h从字面上看是 kill,sigsend,raise 发出了携带 SI_USER 代码的 SIGABRT 信号,看起来和Linux信号机制有关,那具体是什么意思呢?这就是本篇和大家详聊的。
二:Linux 信号机制

1. 信号机制简介

简单的说Linux信号是一种进程间通信机制,大概可以做三件事情。

[*]通知进程发生了某种事件,比如:段错误。
[*]允许进程间发送简单的消息。
[*]控制进程行为,比如:终止、暂停、继续等。
在 linux 上有60多个信号,默认能产生core文件的有11个,这也是我们最关心的,整理成表格如下:
信号名称信号编号说明SIGQUIT3通常由 Ctrl+\ 触发SIGILL4非法指令SIGABRT6由 abort() 函数产生SIGFPE8浮点异常SIGSEGV11段错误(非法内存访问)SIGBUS7总线错误(内存访问对齐问题等)SIGSYS31错误的系统调用SIGTRAP5跟踪/断点陷阱SIGXCPU24超出 CPU 时间限制SIGXFSZ25超出文件大小限制SIGEMT7EMT 指令(某些架构)有了这些基础之后就可以解读 Signal SIGABRT code SI_USER (Sent by kill, sigsend, raise) 这句话了。
1) SIGABRT

全称 signal abort ,是一种能产生 core 的信号。
2) SI_USER

在 linux 源码中有这样一句代码(type == PIDTYPE_PID) ? SI_TKILL : SI_USER,参考如下:
static void prepare_kill_siginfo(int sig, struct kernel_siginfo *info,enum pid_type type)
{
        clear_siginfo(info);
        info->si_signo = sig;
        info->si_errno = 0;
        info->si_code = (type == PIDTYPE_PID) ? SI_TKILL : SI_USER;
        info->si_pid = task_tgid_vnr(current);
        info->si_uid = from_kuid_munged(current_user_ns(), current_uid());
}代码中的 kernel_siginfo.si_code 字段用来表示信号的来源,比如说 SI_USER 表示信号来源于用户进程,而后者的 SI_TKILL 表示信号来源于 tgkill,tkill 系统调用。
3) kill,sigsend,raise

熟悉 linux 的朋友应该对 kill 和 raise 方法非常熟悉,毕竟他们遵守 POSIX 标准,至于他们有什么区别,看签名就知道了。。。
/* Raise signal SIG, i.e., send SIG to yourself.*/
extern int raise (int __sig) __THROW;

/* Send signal SIG to process number PID.If PID is zero,
   send SIG to all processes in the current process's process group.
   If PID is < -1, send SIG to all processes in process group - PID.*/
#ifdef __USE_POSIX
extern int kill (__pid_t __pid, int __sig) __THROW;
#endif /* Use POSIX.*/相比前面的函数,这个 sigsend 就不是 POSIX 标准了,只在部分Unix上可用,比如 Solaris,SunOS,不过功能还是很强大的,不仅可以指定 pid,还可以指定 pidgroup 以及 user 来批量的 kill 进程,这里做个了解即可,签名如下:
int sigsend(idtype_t idtype, id_t id, int sig);这些信息汇总之后更准确的意思就是:你的程序可能调用了 kill(SIGABRT) ,raise(SIGABRT),abort 引发的程序崩溃,那是不是这样的呢?可以用 windbg 的 ~* k 观察每个线程的调用栈,最终还真给找到了。
0:023> k
# Child-SP          RetAddr               Call Site
00 00007fbd`03c62a70 00007fbd`090bf635   libc_so!wait4+0x57
01 00007fbd`03c62aa0 00007fbd`090c0580   libcoreclr!PROCCreateCrashDump+0x275
02 00007fbd`03c62b00 00007fbd`090be22f   libcoreclr!PROCCreateCrashDumpIfEnabled+0x770
03 00007fbd`03c62b90 00007fbd`090be159 (T) libcoreclr!PROCAbort+0x2f
04 (Inline Function) --------`-------- (T) libcoreclr!PROCEndProcess+0x7c
05 00007fbd`03c62bb0 00007fbd`08db667f (T) libcoreclr!TerminateProcess+0x84
...
09 00007fbd`03c63950 00007fbd`08d4524e   libcoreclr!UMEntryThunk::Terminate+0x38
0a (Inline Function) --------`--------   libcoreclr!InteropSyncBlockInfo::FreeUMEntryThunk+0x24
19 00007fbd`03c63e30 00007fbd`092c91f5   libcoreclr!CorUnix::CPalThread::ThreadEntry+0x1fe
1a 00007fbd`03c63ee0 00007fbd`09348b00   libc_so!pthread_condattr_setpshared+0x515
1b 00007fbd`03c63f80 ffffffff`ffffffff   libc_so!_clone+0x40
1c 00007fbd`03c63f88 00000000`00000000   0xffffffff`ffffffff在上面的代码中我们看到了 libcoreclr!PROCAbort 函数,在 coreclr 中方法定义如下:
/*++
Function:
PROCAbort()

Aborts the process after calling the shutdown cleanup handler. This function
should be called instead of calling abort() directly.

Parameters:
signal - POSIX signal number

Does not return
--*/
PAL_NORETURN
VOID
PROCAbort(int signal)
{
    // Do any shutdown cleanup before aborting or creating a core dump
    PROCNotifyProcessShutdown();

    PROCCreateCrashDumpIfEnabled(signal);

    // Restore the SIGABORT handler to prevent recursion
    SEHCleanupAbort();

    // Abort the process after waiting for the core dump to complete
    abort();
}

VOID PROCCreateCrashDumpIfEnabled(int signal, siginfo_t* siginfo, bool serialize)
{
        // If enabled, launch the create minidump utility and wait until it completes
        if (!g_argvCreateDump.empty())
        {
                std::vector<const char*> argv(g_argvCreateDump);
    ...
        }
}卦中的代码逻辑非常清楚,在 abort 退出之前,先通过 PROCCreateCrashDumpIfEnabled(signal) 方法踩了一个dump,也就是说 dump 中看到的信息就是用他来填充的,可以观察 libcoreclr!g_argvCreateDump 全局变量,参考如下:
0:023> x libcoreclr!*g_argvCreateDump*
00007fbd`09192360 libcoreclr!g_argvCreateDump = {size=8}
0:023> dx -r1 (*((libcoreclr!std::vector<const char *, std::allocator<const char *> > *)0x7fbd09192360))
(*((libcoreclr!std::vector<const char *, std::allocator<const char *> > *)0x7fbd09192360))               : {size=8}
    [<Raw View>]   
             : 8
           : 8
                : 0x5555b5d71140 : "/usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.15/createdump"
                : 0x7fbd08b61d8f : "--name"
                : 0x7ffd1b7e1cec : "/db/xxxx/crash.dmp"
                : 0x7fbd08b6ce5f : "--full"
                : 0x7fbd08b4c7ee : "--diag"
                : 0x7fbd08b58630 : "--crashreport"
                : 0x5555b5dd7230 : "1"
                : 0x0 2. C代码眼见为实

为了能够让大家有一个更加贴切的眼见为实,我们用 C 代码亲自演示一下,为产生 core 文件,配置如下:
root@ubuntu2404:/data2# ulimit -c unlimited
root@ubuntu2404:/data2# echo /data2/core-%e-%p-%t| sudo tee /proc/sys/kernel/core_pattern
/data2/core-%e-%p-%t配置好之后,大家可以使用 abort,kill,raise 这三个方法的任何一个,这里我就用 kill 来演示吧。
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

void sig_handler(int signo, siginfo_t *info, void *context)
{
    fprintf(stderr, "Received signal: %d (sent by PID: %d, UID: %d)\n",
            signo, info->si_pid, info->si_uid);
}

int main()
{
    struct sigaction sa;

    sa.sa_sigaction = sig_handler;
    sa.sa_flags = SIGABRT;
    sigemptyset(&sa.sa_mask);

    if (sigaction(SIGSEGV, &sa, NULL) == -1)
    {
      perror("sigaction");
      return 1;
    }

    printf("My PID: %d\n", getpid());
    printf("Press Enter to send SIGABRT to myself...\n");
    getchar();

    kill(getpid(), SIGABRT);// 第一种方式
    // raise(SIGABRT);      // 第二种方式
    //abort();            //第三方方式

    printf("This line may not be reached.\n");
    return 0;
}ternimal 如下:
root@ubuntu2404:/data2# ./app
My PID: 7403
Press Enter to send SIGABRT to myself...

Aborted (core dumped)
root@ubuntu2404:/data2#
root@ubuntu2404:/data2# ls -lh
total 160K
-rwxr-xr-x 1 root root21K May 27 10:25 app
-rw-r--r-- 1 root root813 May 27 10:25 app.c
-rw------- 1 root root 432K May 27 10:25 core-app-7403-1748312729用 windbg 打开 core-app-7403-1748312729 文件,熟悉的画面又回来了,哈哈。截图如下:

三:总结

要分析linux 上的.NET程序崩溃,理解Linux信号机制是一个必须要过的基础,调试之路艰难哈。。。


来源:新程序网络收集,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 聊一聊 .NET Dump 中的 Linux信号机制