Segmentation fault in linux debug

Running application ends with «Segmentation Fault»

I have a command line application that when run does not do what it is supposed to do and at a certain point leaves the message:

2 Answers 2

A segmentation fault is the result of a memory access violation. The program has referred to a memory address outside of what was allocated to it, and the OS kernel responds by killing the program with SIGSEGV.

This is a mistake, since there is no point in trying to access inaccessible memory (it cannot be done). Mistakes of this sort are easy to make, however, particularly in languages such as C and C++ (which account for a lot of common applications). It indicates a bug in either the program itself or a library it links to. If you wish to report the bug (do — this helps), it is a good idea to include a backtrace of the events that led up to the seg fault.

To do this, you can run the program inside gdb (the GNU debugger), which should be available from any linux distro if it is not installed already (the package will just be called «gdb»). If the broken application is called «brokenapp»:

A paragraph about copyright and licensing will appear, and at the end a prompt with the cursor:

Type run and hit enter. If you need to supply arguments (e.g. -x —foo=bar whatever ) append those ( run -x —foo=bar whatever ). The program will do what it does, you will see the output and if you need to interact you can (note you can run any sort of program, including a GUI one, inside gdb). At the point where it usually segfaults you will see:

Program received signal SIGSEGV, Segmentation fault. 0x00000000006031c9 in ?? () (gdb) _ 

The second line of output here is just an example. Now type bt (for «backtrace») and hit enter. You’ll see something like this, although it may be much longer:

(gdb) bt #0 0x00000000006031c9 in ?? () #1 0x000000000040157f in mishap::what() const () #2 0x0000000000401377 in main () 

If it is longer, you’ll only get a screenful at a time and there will be a —More— message. Keep hitting enter until it’s done. You can now quit , the output will remain in your terminal. Copy everything from Program received signal SIGSEGV onward into a text file, and file a bug report with the application’s bug tracker; you can find these online by searching, e.g. «brokenapp bug report» — you will probably have to register so a reply can be sent to you by email. Include your description of the problem, any arguments you supplied to run , etc., and a copy of the backtrace (if it is very long, there may be a means to attach a text file in the bug tracker interface). Also include the version, if you know what it is ( brokenapp —version may work, or the man page may indicate how to get this), and which distribution you are using.

Читайте также:  Php serial port linux

Someone will hopefully get back to you in not too long. Filing bugs is a usually appreciated.

Источник

Segmentation Fault — how to debug? [closed]

My question is what does 367 here indicate? I am not able to find the core dump file anywhere.
Any clue how do I debug the segmentation fault in such a scenario?

@OpenSourceEnthusiast: don’t comment your question but edit it. Is your system using systemd ? Have you tried to run your application under gdb ? Do you have some minimal reproducible example (or perhaps an URL for the source code of your application)?

3 Answers 3

The 367 is a process id (of the program segfaulting). You could run your program under the gdb debugger.

any clue how do I debug the segmentation fault in such a scenario?

If that fault is reproducible, it is quite easy. Run your program under gdb

I am not able to find the core dump file anywhere..

Read carefully core(5) (and getrlimit(2); maybe core dumps have been disabled with e.g. some ulimit bash builtins). Your system might be configured (see proc(5)) to dump core in some other ways (e.g. by systemd(1), perhaps under /var/lib/systemd/coredump/ ). See perhaps coredumpctl(1) and systemd-coredump(8) and coredump.conf(5)

Don’t forget to enable all warnings and debug info when compiling your application with g++ -Wall -Wextra -g (of course, improve your code to get no warnings at all).

Normally my firsts steps to debug a segmentation fault are:

  1. Compile the application with debug symbols ( -g option) but leaving in optimization options to avoid altering too much
  2. Run the program under gdb with gdb —args ./myprog followed by r at the command prompt
Читайте также:  Red hat enterprise linux test page

If you’re lucky and the program crashes then type bt (backtrace) to see in which function it crashed, called from which function and so on up to main (or to thread start).

If possible an even better option is running the executable under valgrind to see what is the first detected «bad» behavior it has. In many cases doing bad things like reading or writing outside of an array boundary don’t generate an immediate segfault, valgrid will show you the first read or write operation on a location you shouldn’t have access to (but that can «work» silently when the program is executed normally). The downside of using valgrind is that execution is VERY slow because the program basically runs using an emulated processor; it’s so slow that it may be impossible to run the program in the intended context (for example because it needs communicating with a device that would timeout).

Another option is to compile the program with the address sanitizer ( -fsanitize=address ). That will add code in the executable itself to check for memory accesses outside boundaries or to freed memory. The advantage is that the execution while still slower than a regular non-instrumented executable is much faster than running the program under valgrind .

Источник

Linux — segmentation fault only sometimes — how to debug

I have a Linux program, that from time to time ends with a segmentation fault. The program is running periodically every hour, but the segmentation fault occurs only sometimes. I have a problem to debug this, because if I run the program again with the same input, no error is reported and all is OK. Is there a way, how to «report» in which part of the code error occured or what caused the problem?

Читайте также:  Policy routing with linux

@gowrath I have tried print statements, but it is a problem because error occured randomly. So I try to repair something, it runs fine afterwards, but the next day, crash again

3 Answers 3

The usual way is to have the crashing program generate a corefile and analyze this after the crash. Make sure, that:

  • the maximum corefile-size is big enough (i.e. unlimited) by calling ulimit -c unlimited in the shell, which starts the process.
  • The cwd is writable by the segfaulting process.

Then you can analyze the file with

While it is helpful (compile with -ggdb ), because the corefile contains more information then (for example to show the line number of the crashing source code line), it is not absolutely necessary.

@MartinPerry NO, you have to increase only process resource limit. Do run only on console what Ctx told. «ulimit -c unlimited»

Since your code not crashing every time, you can use backtrace as well. Using this you can see the function call stack at the time of crash. There are many examples available. In my projects I normally use the following code for backtracing.

/* * call reg_usr2 function from main * gcc -rdynamic myfile.c -o output */ #include #include #include #include #include #include #define FILE_NAME "/tmp/debug" #define MODE 0xFFFF void dbgprint(int flag, char* fmt, . ) < if(flag & MODE) < char buf[100]; va_list vlist; FILE *fp = fopen(FILE_NAME,"a"); va_start(vlist, fmt); vsnprintf( buf, sizeof( buf), fmt, vlist); va_end( vlist); fprintf(fp,"[%x]->%s\n", flag, buf); fclose(fp); > > /** Here is the code to print backtrace **/ void print_stack_trace () < void *array[20]; size_t size; char **strings; size_t i; size = backtrace (array, 20); strings = backtrace_symbols (array, size); dbgprint(0xFFFF, "Obtained %zd stack frames.", size); dbgprint(0xFFFF, "-------------------------"); dbgprint(0xFFFF, "---------Backtrace-------"); for (i = 0; i < size; i++) dbgprint (0xFFFF, "%s", strings[i]); dbgprint(0xFFFF, "-------------------------"); free (strings); >void sig_handler(int signo) < FILE *fp = fopen(FILE_NAME,"a"); if (signo == SIGUSR2)< dbgprint(0xFFFF, "received SIGUSR2"); dbgprint(0xFFFF, "----------------"); >print_stack_trace(); exit(0); > void reg_usr2() < if (signal(SIGUSR2, sig_handler) == SIG_ERR) printf("\ncan't catch SIGUSR2\n"); >int main() < reg_usr2(); //should be first line of main after variables //Code. return 0; >

Источник

Оцените статью
Adblock
detector