Gdb Tutorial Bus Error
Contents |
Programming Boards C Programming help needed on debugging "bus error" Getting started with C or C++ | C Tutorial | C++ bus error c++ Tutorial | C and C++ FAQ | Get a compiler | Fixes
Bus Error Linux
for common problems Thread: help needed on debugging "bus error" Thread Tools Show Printable Version Email this Page… Subscribe
Bus Error (core Dumped) Linux
to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 05-18-2007 #1 wuhua View Profile View Forum Posts Registered User Join Date May 2007 Posts 2 help
Bus Error Core Dumped C
needed on debugging "bus error" Hello. This is my first post here. Thanks for your help. I have been coding a program which handles many arrays of strings. The program sometimes (there is a random number generator in the codes) will crash with a message "bus error". I am guessing it's caused by those strings. I tried to find out where exactly bus error (core dumped) centos the error occurs by adding print lines inbetween coding blocks like the following: 100 printf("100\n"); codes... 200 printf("200\n"); codes... 300 printf("300\n"); However, it didn't help because the output will be something like 100 200 300 100 20 which means it crashes in the middle of printing 200? Anyway, because i can't exactly pin down which part of the code causes the problem, it is very hard for me to fix it. Any ideas? Thanks a lot. 05-18-2007 #2 Salem View Profile View Forum Posts and the hat of int overfl Join Date Aug 2001 Location The edge of the known universe Posts 34,484 > there is a random number generator in the codes I would avoid calling srand() until you're sure everything works. At least then you'll get some measure of repeatability. As for the bus error, that is much harder. If you run it inside the debugger, it will trap the BE and stop the program where the problem is first identified. Unfortunately, this is seldom related to the root cause of the problem which is likely to be somewhere else in the co
manifest themselves in strange ways, such as the program printing interesting messages like "core dump" or "bus error" with no additional information. This is the price you pay for the efficiency and low-level control that the how to fix bus error in linux C language provides. Debugging is a big subject, and we can only scratch the bus error core dumped in unix surface here. In general, here are three approaches you can use for debugging: When you get a bug, put lots of bus error vs segmentation fault print statements in code likely to have caused the bug so that you can monitor the values of variables which may not be what they should be. Add lots of assert statements so that when http://cboard.cprogramming.com/c-programming/90001-help-needed-debugging-bus-error.html something goes wrong the program halts right away instead of continuing. If you don't know about assert, do "man assert". We will talk more about this later in the course. Use a debugger to find out where your code went wrong. These approaches are not mutually exclusive and almost every programmer uses a combination of all three (plus others). The first two methods are pretty self-explanatory. The third needs a bit http://courses.cms.caltech.edu/cs11/material/c/mike/misc/gdb.html more explanation, which we provide below. You can also do "man gdb" and/or "info gdb" to get much more information. GDB basics GDB stands for Gnu DeBugger. It is an environment under which you can run a C program in such a way as to make it very easy to identify bugs. To use gdb, do the following: Compile your program with the -g flag e.g. gcc -Wall -Wstrict-prototypes -ansi -pedantic -g myprog.c -o myprog (Note that we're using a lot of warning options as well, which are the "-Wall -Wstrict-prototypes -ansi -pedantic" options; these force the compiler to complain if your code isn't ANSI-compliant or if it has other suspicious features. It's a good habit to always use these options.) The "-g" option puts debugging information into the executable. Most importantly, it puts the text of the source code file into the executable so you can examine it as the program executes (we'll see how below). Type gdb myprog (for the example above). This will start the interactive debugger. It's basically an interpreter-like environment in which you can run your program line-by-line and do useful debugging tasks as well. When in the debugger, you have a choice of lots of commands. Do "info" to get a list of c
code with the -g option. This tells the compiler to annotate your executtable with the names of variables, locations in the code etc. Then type gdb. At the prompt type file nameOfExecutable. Then use the set command to set any parameters http://marvin.cs.uidaho.edu/Teaching/CS445/gdb.html that you would type after the executable. For example if you would normally type fred arg1 arg2 then you can do: gdb file [filename] set args [arg1] [arg2] r or gdb [filename] run arg1 arg2 The r command will run the debugger. When it gets the bus error, seg fault or whatever, the location in your source code will be displayed. If you want to see the whole stack to help find out where the failing routine was called from etc you can use the bt bus error command which backtraces the execution stack. quit gets you out of the debugger. Go to the gnu documention on the web for details or just type help in gdb. The Man Page for gdb Here is the man page for gdb. As you can see it can do a lot more than just the tiny example above. GNU Tools gdb(1) NAME gdb - The GNU Debugger SYNOPSIS gdb [-help] [-nx] [-q] [-batch] [-cd=dir] [-f] [-b bps] [-tty=dev] [-s symfile] [-e prog] [-se prog] [-c bus error (core core] [-x cmds] [-d dir] [prog[core|procID]] DESCRIPTION The purpose of a debugger such as GDB is to allow you to see what is going on ``inside'' another program while it executes-or what another program was doing at the moment it crashed. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act: o Start your program, specifying anything that might affect its behavior. o Make your program stop on specified conditions. o Examine what has happened, when your program has stopped. o Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another. You can use GDB to debug programs written in C, C++, and Modula-2. Fortran support will be added when a GNU Fortran compiler is ready. GDB is invoked with the shell command gdb. Once started, it reads commands from the terminal until you tell it to exit with the GDB command quit. You can get online help from gdb itself by using the command help. You can run gdb with no arguments or options; but the most usual way to start GDB is with one argument or two, specify- ing an executable program as the argument: gdb program You can also start with both an executable program and a core file specified: GNU Tools Last change: 4nov1991 1 GNU Tools gdb(1) gdb program core You can, instead, specify a process ID as a second argument, if you