6

Alright so don't ask why, but I want to create a core file of a test program I made. It's not corrupted and it doesn't screw up, but I want to generate a core file of it. Heres the code:

#include <stdio.h>
int main(){
printf("TEST");
}

As I said its really simple, but I want to be able to dump a core of it. How can I do it? BTW I'm on Ubuntu 10.04

Jack Jacobsen
  • 93
  • 1
  • 1
  • 3

4 Answers4

12

From the gdb command prompt with your executable loaded, issue the generate-core-file command.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
6

If you can change the program adding a call to abort() will generate a core dump in many unix environments.

You need to make sure that you have core files enabled. The most common reason for core files not being generated is a zero size core ulimit. Check with the command ulimit -c and reset if zero with ulimit -c unlimited.

If you don't want to change the program you can send an abort signal with the kill command: kill -SIGABRT <pid> but with such a short program you are probably going to have to used a script and even then you may not be able to get the signal in before the process exits.

With bash you can try something like this (assuming that your program is called a.out and is in the current directory):

./a.out & kill -SIGABRT $!

& says run this in the background and $! is the PID of the most recently executed background command.

stackoverflower
  • 545
  • 1
  • 5
  • 21
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • (-1) Is it possible that the application exit before the SIGABRT signal is send? You would need a sleep() or scanf() to wait for. – Raphael Bossek Jan 05 '11 at 23:21
  • @Raphael B.: Yes, this is what I said: "even then you may not be able to get the signal in before the process exits". I'm not sure I understand your (-1) explanation? – CB Bailey Jan 05 '11 at 23:27
6

Load your program in gdb, set a breakpoint, run to the break point and then

(gdb) generate-core-file

jcopenha
  • 3,935
  • 1
  • 17
  • 15
  • Ugh... GCC 6.6 on OpenBSD: *`Undefined command: "generate-core-file". Try "help".`* – jww Nov 24 '19 at 06:53
0

Another option is to use - gcore command.

dev
  • 649
  • 9
  • 11