Segmentation fault linux gcc

How to debug a GCC segmentation fault

Configure GCC with —enable-checking . Compile it with -g -O0 so that you can use gdb .

Compile your test case with -v -da -Q .

  • -Q will show which function in the test case is causing it to crash.
  • -v shows how cc1 was invoked (useful for invoking cc1 manually in gdb ).
  • -da dumps the RTL to a file after each stage.

Next, use gdb to get a stack trace:

bash$ gdb cc1 gdb> run arguments (cc1 will stop at the segmentation fault) gdb> where gdb> list 

Print out the values of interesting variables, e.g., the ones in the statement which got the segmentation fault. You can use the pt and pr macros from the gdbinit.in file to display GCC data. For example, if there is a value of type tree named t , and a value of type rtx named r , you can use these commands:

gdb> source .gdbinit gdb> print t gdb> pt gdb> print r gdb> pr gdb> pt 

For questions related to the use of GCC, please consult these web pages and the GCC manuals. If that fails, the gcc-help@gcc.gnu.org mailing list might help. Comments on these web pages and the development of GCC are welcome on our developer list at gcc@gcc.gnu.org. All of our lists have public archives.

Copyright (C) Free Software Foundation, Inc. Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.

These pages are maintained by the GCC team. Last modified 2022-10-26.

Источник

gcc segmentation fault on Ubuntu 12.04

#include int main(int argc, char** argv)

My gcc version is 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5). Initially it did not find cc1 so I added a soft link. Now I get this message when I try to compile:

gcc: internal compiler error: Segmentation fault (program cc1) 

Compiling the same program with g++ works fine. I tried reinstalling build-essential, but to no avail. What am I missing? EDIT: I tried reinstalling according to @gertyvdijk’s suggestion. As it did not help, here is the output of apt-cache policy gcc-4.6 :

 gcc-4.6: Installed: 4.6.3-1ubuntu5 Candidate: 4.6.3-1ubuntu5 Version table: *** 4.6.3-1ubuntu5 0 500 http://il.archive.ubuntu.com/ubuntu/ precise/main amd64 Packages 100 /var/lib/dpkg/status 
lrwxrwxrwx 1 root root 7 Mar 13 2012 /usr/bin/gcc -> gcc-4.6 
 gcc -v aaa.c Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) COLLECT_GCC_OPTIONS='-v' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-linux-gnu/4.6/cc1 -quiet -v -imultilib . -imultiarch x86_64-linux-gnu aaa.c -quiet -dumpbase aaa.c -mtune=generic -march=x86-64 -auxbase aaa -version -fstack-protector -o /tmp/ccHfcXMs.s gcc: internal compiler error: Segmentation fault (program cc1) Please submit a full bug report, with preprocessed source if appropriate. See for instructions. 

Источник

Читайте также:  File share read in linux

segmentation fault in c program

Modiying the content of a string literal (i.e «Hello World» in your code) is Undefined Behavior.

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

Try using array of characters.

char p[] = "Hello World"; p[1] = 'l'; 

invokes Undefined Behaviour as well because you are trying to deallocate the section of memory (using free ) which has not been allocated using malloc

. and because the read-only memory is still being modified, as after p = «Hello world» p doesn’t point to the malloc ed memory any more.

ok then does that mean the previously allocated memory (via malloc) is lost and right now what i m using is a new memory part of memory allocated by compiler for string literal «Hello world». Am i right?

Because char *p = «Hello world» has almost certainly given you a pointer to read-only memory and that means that trying to change it with *(p+1) = ‘l’ is a definite no-no (even if the memory isn’t read-only, the behaviour is still undefined).

The relevant part of C99 referring to string literals is in 6.4.5 para 6 :

It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

The reason why you still get a segmentation fault with something like:

char *p = malloc (100); // sizeof(char) is ALWAYS 1 p = "Hello"; // better would be: strcpy (p, "Hello") *p = 'a'; 

is because, even though you’re allocating memory which you’re allowed to modify, the second statement changes the pointer to point to a string literal (giving you a mmory leak as you lose access to the allocated memory), which you are not allowed to modify.

Читайте также:  Linux mysql user password

You need to differentiate changes to the pointer from changes to what the pointer points at.

That’s because you’re changing the pointer to point to another string literal, not trying to change the string literal itself.

ok, but what if memory is allocated through malloc. as i had done in the new code section of my problem.

«Hello world» is a string literal. It is represented by a chunk of bytes in a region of memory which may not be modified. char *p points at that chunk of bytes. *(p+1) = ‘l’ says to overwrite the next byte after the pointed-at one with an ‘l’. The next byte after the pointed-at one is part of the chunk which may not be modified. Attempting to overwrite something is attempting to modify it. Attempting to modify something which is not allowed to be modified is very bad.

In order to have a copy of the text in memory which may be modified, put it into an array, e.g. char p[] = «Hello world»; . (Notice that declaring the array this way makes it exactly big enough to hold the string, and therefore you may not lengthen it, since there is no more room.)

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Читайте также:  Логи удаления файлов linux

Источник

How to debug a GCC segmentation fault

Configure GCC with —enable-checking . Compile it with -g -O0 so that you can use gdb .

Compile your test case with -v -da -Q .

  • -Q will show which function in the test case is causing it to crash.
  • -v shows how cc1 was invoked (useful for invoking cc1 manually in gdb ).
  • -da dumps the RTL to a file after each stage.

Next, use gdb to get a stack trace:

bash$ gdb cc1 gdb> run arguments (cc1 will stop at the segmentation fault) gdb> where gdb> list 

Print out the values of interesting variables, e.g., the ones in the statement which got the segmentation fault. You can use the pt and pr macros from the gdbinit.in file to display GCC data. For example, if there is a value of type tree named t , and a value of type rtx named r , you can use these commands:

gdb> source .gdbinit gdb> print t gdb> pt gdb> print r gdb> pr gdb> pt 

For questions related to the use of GCC, please consult these web pages and the GCC manuals. If that fails, the gcc-help@gcc.gnu.org mailing list might help. Comments on these web pages and the development of GCC are welcome on our developer list at gcc@gcc.gnu.org. All of our lists have public archives.

Copyright (C) Free Software Foundation, Inc. Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.

These pages are maintained by the GCC team. Last modified 2022-10-26.

Источник

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