Is it possible to generate a mini core dump for debugging purpose without crashing the process. Let's say if a function receives an unexpected value, just printing logs and returning gracefully might not be sufficient to debug the issue. On the other hand, if i can get a screenshot of memory and look at the stack, i could find more useful information to debug.
Asked
Active
Viewed 3,116 times
1
-
What's your OS? – StoryTeller - Unslander Monica Mar 02 '18 at 00:22
-
linux machine - ubuntu – user1762571 Mar 02 '18 at 00:24
-
This might help: https://unix.stackexchange.com/q/11185/7084 – Michael Burr Mar 02 '18 at 00:30
-
gcore is not showing symbols. Is there any other industry standard way? – user1762571 Mar 02 '18 at 00:31
-
Can you show the output you're getting? Is `ulimit -c` configured to allow core to be written (run `ulimit -c unlimited` *before* the process is started)? – Michael Burr Mar 02 '18 at 00:39
-
Possible duplicate of [What is a good way to dump a Linux core file from inside a process?](https://stackoverflow.com/questions/318647/what-is-a-good-way-to-dump-a-linux-core-file-from-inside-a-process) – Stargateur May 02 '18 at 00:52
2 Answers
2
Yes,
According to gdb's documentation, once attached with gdb you may issue the following command:
(gdb) gcore
(gdb) q
This will dump the core to "core.pid" without crashing the process.
or this one-liner:
sudo sh -c 'echo gcore <output_core> | gdb -p <pid>'
-
1
-
@Barmar Good point. Unless it's root process (and I would question 'why' the question is wanting this - and I'd question the validity of such a test too) or there's some limitation in place (though nothing comes to mind atm) there's no need at all to use sudo since they're no need to be root at all. It seems rather silly to be root just to kill a process that isn't owned by root - and unnecessary too. – Pryftan Oct 22 '19 at 18:12
1
There is not building function to do that, you could use ptrace()
to debug your own process but it would not be easy. Call gcore
is the easiest method.
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/wait.h>
int main(void) {
pid_t parent = getpid();
pid_t pid = fork();
if (pid < 0) {
// oh dear we are on trouble
} else if (pid == 0) {
char tmp[42];
snprintf(tmp, sizeof tmp, "%" PRIdMAX, (intmax_t)parent);
execvp("gcore", (char *[]){"gcore", tmp, NULL});
} else {
int wstatus;
waitpid(pid, &wstatus, 0);
}
}

Stargateur
- 24,473
- 8
- 65
- 91
-
I tried this and dont see coredump getting generated in ubuntu. am i missing something? – user1762571 May 01 '18 at 01:55
-
where can find a core file. I don't see it in current directory or in this location /proc/sys/kernel/core_pattern – user1762571 May 01 '18 at 14:21
-
@user1762571 This is what you mean by code dump... That wasn't clear in your question, http://man7.org/linux/man-pages/man5/core.5.html. – Stargateur May 01 '18 at 14:51
-
I want to generate it from code and don't want to use external utility like gcore – user1762571 May 01 '18 at 15:08
-
You can specify "-o" and "
" in the arguments array of execvp to generate the core file at desired location and name. Gcore would also require that your program runs with root access(sudo ./your_application) – Karan Joisher Jul 05 '19 at 15:28 -
@KaranJoisher Exactly why would it require it to be root? You can generate a core without being root - it's all about permissions and the same user/etc. would work just as well. Much like if you were to send a SIGSEGV to it directly or if you dereferenced a NULL pointer or ... – Pryftan Oct 22 '19 at 18:15