What is a good way to dump a Linux core file from

2020-02-03 06:42发布

We have a server (written in C and C++) that currently catches a SEGV and dumps some internal info to a file. I would like to generate a core file and write it to disk at the time we catch the SEGV, so our support reps and customers don't have to fuss with ulimit and then wait for the crash to happen again in order to get a core file. We have used the abort function in the past, but it is subject to the ulimit rules and doesn't help.

We have some legacy code that reads /proc/pid/map and manually generates a core file, but it is out of date, and doesn't seem very portable (for example, I'm guessing it would not work in our 64 bit builds). What is the best way to generate and dump a core file in a Linux process?

标签: linux gdb
8条回答
趁早两清
2楼-- · 2020-02-03 07:13

system ("kill -6 ")

I'd give it a try if you are still looking for something

查看更多
闹够了就滚
3楼-- · 2020-02-03 07:15

use backtrace and backtrace_symbols glibc calls to get the trace, just keep in mind that backtrace_symbols uses malloc internally and in case of heap corruption it might fail.

查看更多
够拽才男人
4楼-- · 2020-02-03 07:16

Some possible solutions^W ways of dealing with this situation:

  1. Fix the ulimit!!!
  2. Accept that you don't get a core file and run inside gdb, scripted to do a "thread all apply bt" on SIGSEGV
  3. Accept that you don't get a core file and acquired a stack trace from within the application. The Stack Backtracing Inside Your Program article is pretty old but it should be possible these days too.
查看更多
啃猪蹄的小仙女
5楼-- · 2020-02-03 07:17

Try using the Linux command gcore

usage: gcore [-o filename] pid

You'll need to use system (or exec) and getpid() to build up the right command line to call it from within your process

查看更多
三岁会撩人
6楼-- · 2020-02-03 07:18

Google has a library for generating coredumps from inside a running process called google-coredumper. This should ignore ulimit and other mechanisms.

The documentation for the call that generates the core file is here. According to the documentation, it seems that it is feasible to generate a core file in a signal handler, though it is not guaranteed to always work.

查看更多
聊天终结者
7楼-- · 2020-02-03 07:18

I assume you have a signal handler that catches SEGV, for example, and does something like print a message and call _exit(). (Otherwise, you'd have a core file in the first place!) You could do something like the following.

void my_handler(int sig)
{
   ...
   if (wantCore_ && !fork()) {
      setrlimit(...);  // ulimit -Sc unlimited
      sigset(sig, SIG_DFL);  // reset default handler
      raise(sig);  // doesn't return, generates a core file
   }
   _exit(1);
}
查看更多
登录 后发表回答