Sie sind auf Seite 1von 12

CS101 - Lab Manual

Contents
1 Introduction 1.1 Typographical Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Getting Started 2.1 Logging in and out . . . . . . . . . . 2.2 Graphical User Interface . . . . . . . 2.3 Getting Help . . . . . . . . . . . . . 2.4 Files . . . . . . . . . . . . . . . . . . 2.4.1 Handling Files . . . . . . . . 2.4.2 Program Output and Files . 2.4.3 Text Files . . . . . . . . . . . 2.4.4 Organizing Files Directories 2.5 Several Tasks at Once . . . . . . . . 2.6 Program Development . . . . . . . . 3 Emacs 3.1 Essential emacs Commands 3.2 Getting Started . . . . . . . 3.2.1 In Case of Diculty 3.2.2 Getting Help . . . . 3.3 Basic Editing . . . . . . . . 3.3.1 Files and Buers . . 3.3.2 Commands for Text 4 Coding standard for C 4.1 Names . . . . . . . . 4.2 Documenting code . 4.3 Layout . . . . . . . . 4.4 Divide code into les 1 2 2 2 2 2 2 3 4 4 5 5 6 7 7 8 8 8 9 9 10 10 10 10 10 10 11 11 11 12 12 12 12

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

5 Compiling and Debugging 5.1 Getting Help . . . . . . 5.2 Program Control . . . . 5.3 Data Access . . . . . . . 5.4 Source Files . . . . . . . 5.5 Core Dumps . . . . . . . 6 References

a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

. . . . .

Introduction

For the purposes of this course, each student needs to login to one of the desktops or thin-clients in the PC Lab. After logging in to the thin-clients, they need to start the VNC client and login to the VNC server which runs linux. The following gives a short intro to linux and some of the features that will be useful for this course.

1.1

Typographical Conventions
the name of a specic command or le you should replace le with a specic name

We use the following conventions in this guide: emacs le

Thu Jul 22 23:19:11 IST 1999 output that you see on the screen % date Ctrl-x at the Linux prompt, which we assume to be %, type the command date and press <ENTER>. hold down the Control key and press x

2
2.1

Getting Started
Logging in and out

To login, type your username at the Login: prompt, and your password at the Password: prompt. Note that your password is not displayed. Your password is the only key to the safety and privacy of your les. Choose a password that is easy for you to remember but that is non-obvious. Keep it secret and change it periodically. If you forget your password, get help from a system administrator.

2.2

Graphical User Interface

Linux supports graphical desktops such as GNOME or KDE which provide GUI for all kinds of applications such as editors (e.g. Emacs), web browsers (Firefox), command terminals etc. Typically, commands are run from the terminal window.

2.3

Getting Help
displays an index of all manual pages that contain word in the title. If the output lls the screen, press <SPACE> for the next screen. displays the manual page for name displays the manual page for name in section n of the Linux manual

man -k word man name man n name

command --help most commands when followed by --help give brief information on their usage. Try less --help. If the help scrolls o the top of the screen, type: command --help | less The Linux manual is organised into sections 18 as follows: section 1: commands, section 2: system calls, section 3: functions, section 4: special les, section 5: le formats and protocols, section 6: games, section 7: miscellaneous information, section 8: administration commands. The man command looks for the given word in the sections in order. The man page for the rst section in which word appears is displayed. If you want the man page from a later section, you must specify the section number also. For example, man chmod displays the man page for the chmod command from section 1, while man 2 chmod displays the man page for the chmod system call from section 2.

2.4

Files

A Linux le is simply a sequence of bytes. Files are central to Linux. You create les for your programs, data and documents. Every command is in a le. Even devices such as the keyboard, display, mouse and Ethernet LAN appear as special les. It is even possible to access the internals of the Linux kernel as a le! Linux allows you to create directories for related les. A directory can contain sub-directories, resulting in a tree-structured hierarchy. All directories are part of a single tree. The root of the tree is /. Your les are in your home directory, usually with a name such as /home/servername/yourname. Each le has a name that is unique in its directory. The name is case-sensitive (Makefile and makefile are two distinct les), can contain a variety of special characters and can be quite long. a.out and A_Long_#%@!Name.txt.bak.gz are both valid names.

2.4.1

Handling Files move and/or rename les copies le src to dest. displays a text le one screen at a time. Press <SPACE> to see the next page. displays the contents of le(s)

mv src dest cp src dest less le(s) cat le(s)

To see a list of the les contained in the current directory, type ls. For details such as the le size and date of last access, type ls -l. ls *.c lists all C source les. ls programs lists the contents of the sub-directory programs. Normally, ls does not list hidden les whose names start with .. The -a option will also list such les. cat is the equivalent of the MS-DOS type command. Caution: if you cat a binary le, the display may get garbled or even hung. To recover, try the commands: reset or stty sane cat can be used to concatenate several les into one: % cat part1.txt part2.txt part3.txt >whole.txt Wildcards: Many Files at Once The use of wildcards allows a single command to process many les at once. Linux has a set of simple wildcards that allow one to select various subsets of le.1 * ? [abc...] {n1,n2,n3,...} matches any string of 0 or more character in the le name matches any single character matches any one of the characters in the [] matches any one of the comma-separated names in the {}

Examples The echo command displays its arguments on the screen. For each of the commands below, typical output is shown to the right. Except for the last example, the les are only from in the current working directory. echo *.c echo *.[ch] echo [A-Z]* all C source les: . . . myio.c myutils.c prog.c ttydriver.c . . . all C source and header les in the current directory: myio.c myio.h myutils.c myutils.h prog.c ttydriver.c . . . all les in the current directory starting with a capital letter: Makele Readme

echo my{io,utils}.c all C source les starting with the prex my: myio.c myutils.c echo myutils.? all les named myutils with a single-character extension: myutils.c myutils.h myutils.o . . .

echo {src,doc}/myutils.{[ch],txt} all les named myutils with the extensions .c, .h, .txt in the subdirectories src and doc: doc/myutils.txt src/myutils.c src/myutils.h
1 Wildcards are actually provided by the shell (section ??). Despite some similarity, these wildcards are quite dierent from the regular expressions used by programs such as grep.

2.4.2

Program Output and Files

Many programs by default read from the keyboard and write to the display. You can easily change this without modifying the programs, using what is referred to as I/O redirection. This facility allows you to easily do complex tasks using several simple commands. Output to a le >: the command ls >files.list runs ls to obtain a listing of the les in the current directory. The listing is written into the le files.list rather than to the display. To append to an existing le, use >> instead. The following two commands will write the current date and time and the list of logged on users into the le user.list. % date >user.list % finger >>user.list Output to another program |: if two commands are separated by a |, the output of the rst is fed as input to the second. For example, % ls -lR ~ | less will display a detailed listing of all your les, one screen at a time. Such piping can be done between several commands on one line. Input from a le <: prog <infile will run prog reading input from infile instead of from the keyboard. Error output: when you use > or >> you may still nd some error messages appearing on the display. To redirect these also to a le, type: prog > outfile 2>&1, prog >> outfile 2>&1 or prog 2>&1 | prog2.2

2.4.3

Text Files

The most often used tool for text les is a text editor. Linux also has other commands for processing text les. emacs gedit vi wc sort grep a powerful text editor an easy-to-use graphical editor one of the earliest full-screen editors for Unix counts the characters, words and lines in one or more les sorts lines of text in ascending or descending order nd all lines that contain a given pattern

Linux has many text editors. The editor of choice for programmers is GNU Emacs, a powerful customisable, extensible editor. To get started, run emacs and then type Ctrl-h t (i.e., press Control-h followed by t). This will put you in an online tutorial where you can learn the basic Emacs commands. For more details, see the Emacs Info Ctrl-h i, the Emacs Reference Manual and The Emacs Tutorial. The command grep is used to nd lines of text that contain patterns. The simplest pattern is a string. For example, to see all the lines in which the function SomeFunc() is used, type: % grep SomeFunc *.c If a function declaration starts at the left margin, and ts on one line, and does not have a comment after the ), then the following command lists the denition of SomeFunc(): % grep ^[a-zA-Z].*SomeFunc(.*)$ *.c To see all function denitions, type: grep ^[a-zA-Z].*(.*)$ *.c Note: the around the search pattern are required so that the shell does not take them to be wildcards.
2 These command forms assume that you are using bash (see Section ??). If you are using csh, then the command forms are: prog >& outfile, prog >>& outfile or prog |& prog2.

2.4.4

Organizing Files Directories

As with le names, directory names are case-sensitive and can be very long. The special name ~ refers to your home directory. ~username refers to usernames home directory. A le name that starts with a / is an absolute name starting at the root of the tree. A name that does not start with / is relative to the current working directory. The name . refers to the current directory. mkdir dirname rmdir dirname cd dirname pwd make a directory dirname, which can be either absolute or relative. remove the directory dirname. You must own the directory and it must be empty. change the current working directory to dirname. print your current working directory.

mv srcdir destdir move a sub-directory from one place to another. du dirname ls ln -s nd summarizes the disk space usage of all les in dirname and all its sub-directories. lists les and sub-directories make a symbolic link to another directory or le nds les matching certain criteria in a sub-tree of directories

In a multi-user OS, users can share les. Linux allows you to control who can access your les and how. Each le and directory has an owner and a group. The owner can set the le permissions separately for the owner, group and others. In the output of ls -l below, tag is the owner of all three les and users is the group. -rw-r--r--rwxrwxr-x drwx-----drwxr-xr-x lrwxrwxrwx 1 1 3 3 1 tag tag tag tag tag users users users users users 21 34525 1024 1024 26 Apr Apr Jul Jul Jul 17 17 24 24 23 14:23 15:34 10:29 10:29 18:14 data ptest secret src sys -> /usr/include/sys

The rst character on each line indicates the type of le: - for an ordinary le, d for a directory and l for a symbolic link. The next 9 characters consist of 3 groups of 3 characters. These groups give the access permissions for the owner, group and world, respectively. In each group, the characters are interpreted as follows: 1. r for read access, - for no read access 2. w for write access, - for no write access 3. x for execute access, - for no execute access In the case of a directory, execute means the permission to list the contents of the directory. To change the permissions of a le or directory, use the chmod (change mode) command. chmod u+x le chmod a+x le chmod a-w le adds execute permission for the user (i.e. owner) adds execute permission for all makes the le read-only

chmod go-rwx le removes all access for group and others chmod g+w le gives write permission to group members

2.5

Several Tasks at Once

Linux is a multi-tasking operating system. It can run several programs concurrently. Each such program or task is referred to as a process. Piping whenever you pipe the the output of one command into another command, say man -k file | less, Linux runs the commands concurrently.

Background tasks if you end a command line with &, the shell runs the command in the background and immediately prompts you for the next command. It is useful to redirect the output into a le. For example, to run a compilation of a large le in the background: % gcc -g -o large large.c > gcc.log & Suspending a task while a program is running, you can suspend it by typing <Ctrl-z>. To resume the suspended task, type fg. To continue the task running in the background, type bg. Note that a suspended task does not execute until put in the background or brought to the foreground. jobs lists your suspended and background jobs. For example: [1] [2] [3] [4] - Running + Suspended Running Suspended ftp lantana emacs gcc -g -c myprog.c pine

The le transfer program ftp and a C compilation are running while Emacs and Pine are suspended. fg by itself will resume the job marked with a +, i.e., Emacs. To resume Pine, type fg %4 or fg %p (any unambiguous prex). ps kill -9 %gcc lists your processes. Useful options are -u for a more user-oriented output, -l for a detailed listing, and -x to see all processes. terminates a job, gcc, that is suspended or running in the background. Do not kill interactive jobs such as Emacs as you may lose les that are being edited. Rather, bring it to the foreground and exit. terminates the process with pid 3856 (obtained from the ps command).

kill -9 3856

killall -9 myprog terminates all copies of myprog that are running. nice if you start up many processes concurrently, you could make the system respond slowly to other users. The nice command runs a program at lower priority. % nice gcc ... runs the compilation at priority 4, which is lower than the default priority of 0. % nice +19 sim ... runs a long simulation at the lowest priority, 19. Note that priority in Linux is relative while other users are thinking, your lower priority jobs will run. Priority is also dynamic as a process uses the CPU, its priority gets automatically lowered to give others a chance to run.

2.6
gcc g++ java perl gdb make rcs cvs

Program Development
the GNU C compiler the GNU C++ compiler part of the Java Development Kit (jdk) a language that is useful for system administration, processing of text les and networking the GNU symbolic debugger, supports C and C++. Use the -g compiler option before running gdb a utility for building and managing projects consisting of many source code les a revision control system for saving multiple versions of a le. Good for personal use. a revision control system that is suitable for large projects involving many programmers

Linux supports a number of programming languages and other software development tools.

Emacs

GNU Emacs is a powerful text editor that is especially well-suited for programmers. Besides a wide range of useful features, it is customisable to your preferences. It is extendible. Most of its commands are written in ELisp, a LISP-like macro language. You can modify existing emacs commands and writing your own commands in ELisp. emacs is a part of Linux distributions. The source code is freely available and can be compiled and installed on most other operating systems. Note C-k M-k C-M-k In the GNU-Emacs Manual and on-line help, the following conventions are used: equivalent to Ctrl-k equivalent to Esc k (pronounced meta-k, since early terminals had a key labelled meta similar to the control key). equivalent to Esc Ctrl-k

3.1

Essential emacs Commands

Using only the commands in this Section, you can use emacs to do basic editing of programs and other text les. Starting and Stopping Type emacs le at the shell prompt to start editing le. Ctrl-x Ctrl-c Ctrl-g exit from emacs. Oers to save any les that you have modied but not saved. cancels a command that you have started typing, or stops a command that is executing. Type Ctrl-g one or more times if necessary. Typing Ctrl-g never does any harm. Files Ctrl-x Ctrl-s Ctrl-x Ctrl-f save the changes you have made to the current le. Use this frequently. nd an existing le for you to read and/or edit. Or, create a new le.

Cursor Movement Ctrl-f Ctrl-b Ctrl-n Ctrl-p Ctrl-v Esc v Esc < Esc > move right one character move left one character move down one line move up one line scroll forwards by one page (screenful) scroll backwards by one page (screenful) move to the start of the le move to the end of the le

On many terminals, the arrow keys, PageUp , PageDn , Home and End keys work with emacs. Try them. Insertion and Deletion To insert text at the cursor position, simply type it. Ctrl-d or Del Backspace Ctrl-k Ctrl-y Ctrl-x u delete the character under the cursor delete the character to the left of the cursor kill from the cursor to the end of the line. Press Ctrl-k twice to delete the line completely. yank back the last killed text. Use with Ctrl-k to cut and paste lines of text. undo the last insertion or deletion. Use repeatedly to undo recent changes. 7

Search and Replace Ctrl-s Search forwards towards the end of the le for a string (prompts you for the string). emacs searches incrementally. As soon as you type the rst character in the search string, emacs searches for it and moves the cursor to the character that it nds. This occurs for every character you type. To end the incremental search, press Enter . Search backwards towards the beginning of the le for a string, similar to Ctrl-s . replace several occurrences of one string to another. You are prompted for the old string (for example, vi) and the new string, (for example, emacs). emacs then searches from the cursor to the end of the le for vi. It places the cursor after each occurrence of vi and waits for you to type one of the following: y n ! Enter or q replace this occurrence and go on skip this occurrence and go on to the next replace all remaining occurrences without asking exit from query-replace.

Ctrl-r Esc %

Windows Sometimes, emacs divides the screen into two windows. You can also do this yourself and edit dierent les in each window. Ctrl-x 1 Ctrl-x 2 Ctrl-x o make the current window occupy the entire screen. split the current window into two windows. switch to the other window.

3.2
3.2.1

Getting Started
In Case of Diculty cancels a command that you have started typing, or stops a command that is executing. Type Ctrl-g one or more times if necessary. Typing Ctrl-g never does any harm.

Ctrl-g

Ctrl-l Ctrl-x u Ctrl-x Ctrl-c Ctrl-h

redraw the screen if it gets messed up because, for example, of messages written by some other user. The line with the cursor is centred. use repeatedly to undo recent changes. exit from emacs permanently. Oers to save any les that you have modied but not saved. Normally do this only when you want to logout. gets you help (see Section 5.1)

On some terminals, when you type Ctrl-s , the screen may freeze with subsequent keys being ignored. This happens because the terminal is using Ctrl-s for ow control. Typing Ctrl-q may solve this problem. If this happens, avoid using Ctrl-s and Ctrl-q until you can get some administrator to turn o the ow control. 3.2.2 Getting Help

An online tutorial is available that gives an interactive introduction to basic editing with emacs. To use the tutorial, start emacs by typing emacs at the shell prompt, then press Ctrl-h t and follow the instructions. For comprehensive coverage of emacs, read the Info pages. Type the emacs command Ctrl-h i to start Info. When info starts, type memacs Enter . The Info pages are even more comprehensive than the GNU-Emacs Manual.

Ctrl-h a topic

for a list of commands that deal with a particular topic, use the apropos command. For example, to nd out how to save a le, type: Ctrl-h a save Enter . Ctrl-h k keys describes what keys does. Type Ctrl-h k Esc a to learn what Esc a does. Ctrl-h f command-name Enter gives a detailed description of what command-name does. Type Ctrl-h f backward-sentence Enter for a description of the command (or function) backward-sentence.

Ctrl-h m

Lists special commands that are available in the current buer. E.g., Pascal-mode commands, C-mode commands, and so on.

3.3
3.3.1

Basic Editing
Files and Buers

When you edit a le, emacs makes a temporary copy of the le in a buer in memory. Any changes you make aect only the copy in the buer. To make your changes permanent, you must explicitly save the le to disk using Ctrl-x Ctrl-s . emacs has one buer for each le you are editing. Every buer has a name. This is usually related to the name of the le. emacs may also create buers which are not associated with any les. Examples are *Help* which the Help command Ctrl-h uses to display text; *mail* in which you compose a mail message; *shell* in which you can issue Shell commands. Ctrl-x Ctrl-f Ctrl-x i Ctrl-x Ctrl-s Ctrl-x s Ctrl-x b Ctrl-x Ctrl-b Ctrl-x k nd an existing le for you to read and/or edit. Or, create a new le. insert the contents of a le after the cursor. save the changes you have made to the current le. Use this frequently. for each le that you have modied but not saved, emacs oers to save it. Answer y or n as you wish. Use this often! switch to another buer. emacs prompts you for the buer name. The default is the buer you were last in. display a list of all the buers emacs has. The list includes the number of characters in each buer and the associated le name, if any. close a buer. emacs prompts for the buer name. Just type Enter to close the current buer. If the buer has been modied but not saved, emacs oers to save it. If you close a buer without saving it, the changes are lost forever.

C/C++/Java Mode In C, C++ and Java mode, the string (C) or (C++) or (Java) appears in the mode line. Some special commands (type Ctrl-h m for a complete list):3 Esc ; Esc a Esc e Esc Ctrl-h insert a comment on the current line. go to the beginning of the current statement. go to the end of the current statement. mark the current function useful to copy or delete the function.

Ctrl-j , Ctrl-Enter goes to the next line and indents it depending on the level of nesting of the code. Tab Esc Ctrl-q indents the current line. indent the block starting from the cursor.

To save on typing, Ctrl-c followed by various other keys inserts commonly-used code templates. This includes comments and proper indentation. You need only ll in the details.
3 The

commands listed here assume that you have loaded DONc.el.

Ctrl-c u Ctrl-c t Ctrl-c w Ctrl-c f Ctrl-c d Ctrl-c s Ctrl-c i Ctrl-c e 3.3.2

insert a template for a new function. Prompts for the function name and return type. insert a comment block at the top of the le. Use this for every le. insert a while loop. insert a for loop. insert a do-until loop. insert a switch statement. insert an if statement. insert an if-else statement.

Commands for Text

emacs has several commands that are useful for editing and simple formatting of text les such as mail messages. Changing Case Esc c Esc l Esc u capitalize the word to the right of the cursor change the word to the right of the cursor to lower-case change the word to the right of the cursor to upper-case

Coding standard for C

Following a coding standard enhances readability of code and makes it easier for other programmers to read and modify your code.

4.1

Names

An easy way to make code self-documenting is to use meaningful (long) names for variables rather than cryptic ones. Use camelcase to make separate words stand out even though they are part of a single word e.g. theRoomThatJackBuilt in variable names. For Classnames capitalize the name. For constants use all caps with underscore in between. Add a sux to distinguish Types (Typ), Pointers (Ptr) etc.

4.2

Documenting code

Use block comment (multi-line) at the top of each le to document what that le stands for. Use a inline comment to document variables (only where necessary). Use a multi-line comment at the top of a function to document what that function does.

4.3

Layout

Indent the code appropriately so that nesting of blocks is visually apparent and so that corresponding curly braces are aligned.

4.4

Divide code into les

Use header les to declare function prototypes, macros, type, constant and global variable declarations. Group logically related functions into separate .c les. Use make to automate building of your code.

10

Compiling and Debugging a program

A common approach to debugging a program is to insert print statements at various locations. This has the disadvantages that the code becomes harder to read as more such statements are inserted, and that the debugging output can become unmanageably voluminous. After the program is debugged, while removing some of these print statements, the program may get accidentally changed. A symbolic debugger such as gdb allows you to concentrate your attention on the small fraction of the program that typically has most of the bugs. The debugger allows you to control execution of the program, suspending the execution at certain lines. You can then examine and even change the values of any variables in the program. All this is done without touching the source code. Indeed, the most powerful method of debugging is single-stepping line-by-line through the code, examining the values of key variables after every statement. It is only with a symbolic debugger that this is possible. First, compile your program using the -g option. This instructs the compiler to put information about the symbols functions and variables into the executeable le for use by the debugger. Next, invoke gdb with the name of the program: % gcc -g -o myprog myprog.c % gdb myprog gdb To exit from gdb, type quit. For a list of commands, type help. In the sections below, we describe some of the more useful gdb commands. Although all commands are full words, you can type any unambiguous prex. For instance, cont instead of continue. You can also dene short aliases for frequently-used commands (see section ??).

5.1

Getting Help

Use the help command in gdb for a list of topics. Then type help topic for details on a specic topic. help cmd gives help on the command cmd. For comprehensive coverage of gdb, read the Info pages. At the shell prompt, type info. When info starts, type mgdb<ENTER>). Alternatively, if you use Emacs, type the Emacs command <Ctrl-h i> to start Info.

5.2

Program Control
set a break-point program execution stops when it reaches line nnn set a break-point program execution stops when it enters function func start program execution. continue execution from a break-point execute the next source line and then break. Steps into function calls. execute the next source line and then break. Steps over function calls. execute until the end of the current function pressing <ENTER> by itself repeats the last command execution breaks whenever the expression expr changes value

break nnn break func run continue step next nish <ENTER> watch expr

Before running the program, set some break-points. The run command optionally takes command-line arguments and I/O redirection: gdb run -n *.c < infile is equivalent to typing myprog -n *.c < infile at the shell prompt. next treats a function call as a single line, while step allows you to debug inside the function also. If you inadvertently step into a function, use finish to execute to its return without breaking. To single-step throug several lines, type next the repeatedly type <ENTER>. A watchpoint is useful to catch invalid data values. Example, if n should never exceed 100, the set a watchpoint: watch n > 100.

11

5.3

Data Access

When gdb stops execution at a break-point or after a next or step, you can examine and modify variables in the program. print x print *node print F(a+b) set x=10 displays the value of the variable x displays the elds of the structure node calls the function F() with argument a+b and prints its return value sets the variable x to 10

You can print the value of any arbitrary expression in the language of the program being debugged. By calling a function with a variety of arguments, you can test the function without having to write testing code.

5.4

Source Files
lists a window of 10 lines around line nnn in the source le searches forward for the next source line containing the regular-expression pat like search, but searches backwards

list nnn search pat reverse-search

The search commands without an argument use the previous argument. Note that a simple form of regular expression is a string. Example, search MyFunc will search for the next call to MyFunc or its denition.

5.5

Core Dumps

If your program, myprog, suers a serious exception such as a segmentation fault (usually a mis-behaving pointer in C/C++) and produces a core dump, you can use gdb to examine the state of the program at the time of the core dump. Start gdb with: % gdb myprog core

When gdb starts, it tells you the line number in which the exception occurred. You can use print to examine any variables. To see how the program got to that line, use the command bt (short for backtrace). This shows all function calls from main() to the function in which the fault occurred.

References
1. T.A. Gonsalves, A Guide to GNU Emacs, TeNeT Group, Dept. of Computer Science & Engg., I.I.T., Madras, Sep. 99. 2. T.A. Gonsalves, A Guide to the gdb Debugger, TeNeT Group, Dept. of Computer Science & Engg., I.I.T., Madras, Jul. 99. 3. B.W. Kernighan & R. Pike, The UNIX Programming Environment, Prentice-Hall, 84. 4. R.J. Stallman, GNU Emacs Manual, 13th ed., Free Software Foundation, Jun. 97.

12

Das könnte Ihnen auch gefallen