Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

Eitan Lilja




Posted Answers



Answer


 

A Brief Introduction to GDB, including its use within emacs

 

 

Prepared by Professor Daniel Spiegel

Kutztown University

 

A debugger is a program that is used to run other programs. In a debugger, a program may be executed, debugged, or a combination of both. Further, debuggers provide commands in order that data may be examined.

 

There are many operations available in most debuggers. This document will only cover a subset of the functionality of debuggers. The gdb debugger available to Kutztown students will be described.

 

You can find a detailed (and long) description of this debugger online:

GNU project gdb:      http://www.gnu.org/manual/gdb-5.1.1/html_mono/gdb.html

 

The gnu gdb documentation site page has a link to a detailed manual for its use within emacs (gdb in emacs is covered shortly).

 

Features of Debuggers:

¨      Check the order of statement execution in a program.

¨      Evaluate a variable, parameter, or (possibly) an expression at a particular point during program execution.

¨      Determine where execution was occurring in a program when a program crashes, even if the crash was in a library.

¨      Stop a program’s execution at a particular statement

 

Debugger Operations:

The following are basic operations of all debuggers:

 

Setting a Breakpoint

This operation is used to cause a program that is executing in the debugger to suspend itself, permitting debugging. In many integrated development environments (IDEs), a program can also be suspended and enter debugging mode via a command. Visual C++ allows this via a break command that suspends immediately and also by a run to cursor command that stops the program on the line the cursor is on.

 

Resume Execution

This operation restarts a program that is in debugging mode. The program will execute until it terminates, or encounters a breakpoint or other suspend directive.

 

Execute a statement

¨      Step over – Execute the present statement. If the statement is a function call, the function will be executed; the debugger will stop at the statement after the function call.

¨      Step into – Execute the present statement. If the statement is a function call, the function will be debugged; the debugger will stop at the first statement inside the function. Note that if you perform a step into on a library function call such as cout <<, the debugger may attempt to step into the file containing the library function. In Visual C++, this will load a file, usually full of assembly language statements; in gdb, you may have trouble, even if you use finish to get out. Use of step into with function calls should be limited to functions you wrote.

 

Note: Step into and Step over are indistinguishable if the present statement is not a function call.

 

¨      Step out – This operation resumes execution until the function the program is executing terminates; the debugger will stop at the statement after the function call. This function is available in both Visual C++ and gdb.

 

Watching Variables

At any time during a debugging session, you may examine the value of a variable via a watch. In IDEs, watches can be set up in a window. In gdb, the value of a variable and/or expression can be output.

 

Enabling and Starting GDB:

To enable gdb to be able to display the statements of your program, you must use the –g flag during compilation and linking of all files comprising your program. The –g flag causes the compiler and linker to maintain variable names and untranslated C++ statements, in order that variables can be examined and C++ statements can be displayed and followed.

 

To start a stand-alone gdb session, simply type gdb at the system prompt. For example, to debug a.out, issue the command

 

Unix Prompt >gdb a.out

 

gdb will start and load a.out.

 

Alternatively, gdb can be started by simply issuing the command gdb at the Unix prompt. To then load an executable, issue a file command. For example:

 

(gdb)file a.out

 

To start a gdb debugging session in emacs:

 

¨      Start emacs without specifying a file

o       Unix Prompt >emacs

¨      Issue the command to execute a program on the emacs command line

o       ESC-x  (then hit return)

¨      Start gdb

o       gdb (it will follow the M-x, which remains on the emacs command line)

¨      You will then be prompted by the phrase run like this? Add the file to debug (or use the file command after gdb starts)

 

The gdb environment will appear inside the single emacs window.

 

GDB commands:

When GDB starts, the loaded executable is not running; there are many commands that can be issued to prepare debugging. A glossary of commands can be displayed by issuing the command

 

(gdb)help

 

There will be a list of command areas, each of which has its own help list. For example,

 

(gdb)help running

 

will list commands available for running a program. If you issue a help command followed by and actual command, the help information for that command will be displayed.

 

Commands can often be issued without typing the entire command. Here are some commonly used commands; many of them can be invoked using only the first letter:

(gdb) quit – exit the debugger

(gdb) file – load an executable file

(gdb) break line-number/function name -- Set a break-point on a line/at start of function

(gdb) run -- start running the program; if there are command-line arguments, put them after the run invocation

(gdb) cont -- continue running, after a break

(gdb) next -- Next program line (step over function calls)

(gdb) step -- Step into function calls.

(gdb) finish - Step out of the present function

(gdb) print expression -- Show value of a variable or expression

(gdb) list – List 10 lines of the program being debugged. The sixth line is the preset statement. Subsequent, consecutive entry of list will list the next 10 lines.

(gdb) where – obtain a backtrace showing all function calls before the current statement

(gdb) up – Move to the function that called the present function. Useful if your program crashes in a library function; use up to get to the last function call in your program

(gdb) down – Reverses the action of up

(gdb) delete – Removes breakpoint by number (see example following). If no number, all deleted.

(gdb) kill – Terminates the program.

 

Notes on selected gdb commands:

Break: To set a breakpoint on a line, issue the break command with a line number. For example, to set a breakpoint on line 43, issue b 43  . To set a breakpoint at the start of a function, issue the break command with the function name. For example, to suspend execution and enter debugging mode at the start of function foo, issue b foo  . To debug from the start of the program, issue b main

In projects with multiple files, a breakpoint can be set in an associated file using :line number

A breakpoint can be set at the beginning of an object’s function using ::

Note: In IDEs a breakpoint is set explicitly within the program code in a window.

 

Print:    The print command has great functionality. You can enter just about any valid expression, and it will be evaluated an printed. You can print the values of pointers, dereference a pointer, and print entire arrays and objects. See the emacs example on the next page for sample uses of the print command.

 

 

To debug in emacs, start gdb within emacs as noted earlier. Then, set a breakpoint. Start your program, and when the program reaches the breakpoint, emacs will split into two windows. One window will contain the gdb command environment, and the other will contain the program code, with a text arrow (=>) indicating the present statement.

 

You may then issue gdb commands in the original emacs window while viewing the present line and surrounding code in the 2nd window.

 

To exit emacs, enter ctl-x followed by ctl-c. If a program is loaded at the time, you may be prompted before exiting.


Answer is posted for the following question.

can gdb debug itself?

Answer


"It wasn't my fault! I told them over and over that it was an accident! They wanted me to prove my worth, so they threw me up against a young whelp of


Answer is posted for the following question.

Why did uthgerd the unbroken attacked me?

Answer


Open Safari · In the Safari menu, select Preferences · In the Websites tab, click Pop - up Windows · Set the When visiting other websites option to


Answer is posted for the following question.

How to allow pop ups in safari?

Answer


Flagella are microscopic hair-like structures involved in the locomotion of a cell The word “flagellum” means “whip” The flagella have a whip-like appearance that helps to propel a cell through the liquid Some special flagella are used in few organisms as sensory organs that can sense changes in pH and temperature


Answer is posted for the following question.

What is flagella class 11?

Answer


How to create a PowerPoint template · 1 Click "File" at the top of the screen, and then click "Save as Template" · 2 In the pop-up that


Answer is posted for the following question.

How to make ppt template?

Answer


Press the trigger to start the drill The RYOBI 18-Volt ONE+ Lithium-Ion Cordless Brushless 1/2 in Drill /Driver has a Two-speed Gear Train of


Answer is posted for the following question.

How to use ryobi drill?

Answer


The Unified Payment Interface (UPI) is a smartphone-based fund transfer that allows bank customers to send and receive money using a single UPI ID. Within a 24-hour span, the overall limit for UPI transactions is Rs 1 lakh or 10 transactions per bank account. The 10-transaction cap applies only to fund transfers and does not apply to bill payment or merchant transactions. However, the cap varies by bank, but it is fairly similar for most of them. , UPI registration must be done in 30 seconds or less. If it takes longer than 30 seconds to register, you will receive a validation error. As a result, we ask that you complete the registration process in under 30 seconds. Below are the most popular UPI apps in India. Check how to change your UPI ID easily within minutes.


Answer is posted for the following question.

How to edit bhim upi id?


Wait...