linu develop tools 2000 summer latest update: 11/30 2000
TRANSCRIPT
LinuLinuDevelop ToolsDevelop Tools
LinuLinuDevelop ToolsDevelop Tools
2000 SummerLatest update: 11/30 2000
Linux Develop Tools 2
OutlineOutline
GCC / G++- C / C++ compiler
MAKE - Compiler project manager
GLOBAL - Source code browse
GDB - Console debug trace tool
DDD - X-window debug trace tool
GPROF - Profile tool
CVS - Source code version control
Outline
Linux Develop Tools 3
gcc / g++gcc / g++
What is gcc / g++?"GCC" is a common shorthand term for the GNU C compiler. This is both the most general name for the compiler, and the name used when the emphasis is on compiling C programs.
When referring to C++ compilation, it is usual to call the compiler "G++". Since there is only one compiler, it is also accurate to call it "GCC" no matter what the language context; however, the term "G++" is more useful when the emphasis is on compiling C++ programs.
gcc
Linux Develop Tools 4
Building flowBuilding flow
Preprocessing Preprocessing CompilingCompiling AssemblyAssembly LinkingLinking
.c
.cc
.s
.S
.S .o
.o
a.out
cc1
cc1plus
as
gas
nasm
collect2
ld
ar (lib)
gcc / g++
cpp
.i
.a
Linux Develop Tools 5
Suffixes of source file namesSuffixes of source file names.c C source; preprocess, compile, assemble.C C++ source; preprocess, compile, assemble.cc C++ source; preprocess, compile, assemble.cxx C++ source; preprocess, compile, assemble.m Objective-C source; preprocess, compile, assemble.I preprocessed C; compile, assemble.ii preprocessed C++; compile, assemble.s Assembler source; assemble.S Assembler source; preprocess, assemble.h Preprocessor file; not usually named on command line
.o Object file
.a Archive file
gcc / g++
Linux Develop Tools 6
GCC beginningGCC beginning
% gcc –o a.out pr1.c pr2.c
% gcc –c pr1.c
% gcc –c pr2.c
% gcc –o exefile pr1.o pr2.o
gcc / g++
Linux Develop Tools 7
GCC argumentGCC argument
-o outputfileassign Output file name
-c compiler but not linking
-Iincludepath% gcc –c –I/usr/X11R6/include pr1.c
gcc / g++
Linux Develop Tools 8
GCC argumentGCC argument
-Ddefinename=value% gcc –c –DDEBUG pr1.c
% gcc –c –DSIZE=100 pr1.c
% gcc –c –DFILE=\”a.txt\” pr1.c
-Uundefinename% gcc –c –UDEBUG pr1.c
gcc / g++
Linux Develop Tools 9
GCC argumentGCC argument
-Llibpath
-llibfilename
% gcc –o a.out –L/usr/X11R6/lib –lx11 pr1.o pr2.o
-g create extra inform for gnu debug
-p link with “prof” (mon.out)
-pg link with “gprof” (gmon.out)
-S make to assmberly file(*.S)
gcc / g++
Linux Develop Tools 10
GCC argument WarningGCC argument Warning
-wNo Warning
-WWarning that Legality but maybe have some
problems
-WallWarning all
-WerrorWarning -> error
gcc / g++
Linux Develop Tools 11
GCC argument OptimizeGCC argument Optimize
-Onoptimize, n = 0, 1, 2
-finline-functionMake simple function inline
-fno-inline-funroll-loop
Unroll loop for avoid hazard, program size will advance, maybe decrease efficiency
gcc / g++
Linux Develop Tools 12
MakeMake
What is make?Automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them.
Keyword DefineTargetDependencyUp to date
make
Linux Develop Tools 13
Simple project exampleSimple project example
Source files:const.hhmain.ccobject1.hh object1.ccobject2.hh object2.cc
Dependency:main.o -> main.cc const.hh object1.hh object2.hhobject1.o -> const.hh object1.hh object1.ccobject2.o -> const.hh object2.hh object2.cca.out -> main.o object1.o object2.o
make
Linux Develop Tools 14
Build projectBuild projectBuild all
% g++ –c main.cc% g++ –c object1.cc% g++ –c object2.cc% g++ –o a.out main.o object1.o object2.o
Modify object1.cc% g++ –c object1.cc% g++ –o a.out main.o object1.o object2.o
Modify object1.hh% g++ –c main.cc% g++ –c object1.cc% g++ –o a.out main.o object1.o object2.o
make
Linux Develop Tools 15
Make exampleMake examplea.out: main.o object1.o object2.o
g++ –o a.out main.o object1.o object2.omain.o: main.cc const.hh object1.hh object2.hh
g++ –c –O –UDEBUG main.ccobject1.o: const.hh object1.hh
g++ –c –O –UDEBUG object1.ccobject2.o: const.hh object2.hh
g++ –c –O –UDEBUG object2.cc.PHONY : clean clean:
-rm *.o-rm a.out
make
Linux Develop Tools 16
Make Rule SyntaxMake Rule Syntax
targets : dependencies command ...
or like this:
targets : dependencies ; command command ...
TAB
TAB
make
Linux Develop Tools 17
makemake% make will find GNUmakefile, makefile, and Makefile
% makeg++ –c –O –UDEBUG main.ccg++ –c –O –UDEBUG object1.ccg++ –c –O –UDEBUG object2.ccg++ –o a.out main.o object1.o object2.o
% make object1.og++ –c –O –UDEBUG object1.cc
% make cleanrm *.orm a.out
make
Linux Develop Tools 18
Make example2Make example2OPT = –O –UDEBUGOBJ = main.o object1.o object2.oa.out: $(OBJ)
$(CC) –o $@ $(OBJ)main.o: main.cc object1.hh object2.hh
$(CC) –c $(OPT) main.cc object1.o: const.hh object1.hh object1.cc
$(CC) –c $(OPT) object1.ccobject2.o: const.hh object2.hh object2.cc
$(CC) –c $(OPT) object2.cc
make
Linux Develop Tools 19
Default compilation rulesDefault compilation rules.SUFFIXS.SUFFIXS: .cc .oOPT = –O –UDEBUGOBJ = main.o object1.o object2.oa.out: $(OBJ)
g++ –o $@ $(OBJ).cc.o:
g++ –c $(OPT) $< -o [email protected]: main.cc object1.hh object2.hhobject1.o: object1.cc const.hh object1.hhobject2.o: object2.cc const.hh object2.hh
Same as $*.o
make
Linux Develop Tools 20
GLOBALGLOBAL
What’s global ?Global can find the locations of a specified object quickly.Global can locate not only object definitions but also object
references.Global can understand POSIX 1003.2 regular expressions.
How to start ?% cd /usr/src_locate% gtags% ls G*GPATH GRTAGS GSYMS GTAGS
global
Linux Develop Tools 21
Four databasesFour databases
GTAGS - database of function definitions
GRTAGS - database of function references
GSYMS - database of other symbols
GPATH - database of path names
global
Linux Develop Tools 22
global Basicglobal Basic
global
% global func1../DIR1/fileB.c
% global -r func2../DIR1/fileA.c
% global 'func[1-3]'DIR1/fileB.cDIR2/fileC.c
% global func2DIR2/fileC.c
% global -x func2func2 2 DIR2/fileC.c func2() { i++; }func2 4 DIR2/fileC.c func2() { i--; }
% global -a func1/home/user/ROOT/DIR1/fileB.c
% global -xs XX 1 DIR2/fileC.c #ifdef X
% global -xg '#ifdef'#ifdef 1 DIR2/fileC.c #ifdef X
% vi `global func1`
% global -c kmemkmem_alloc_pageablekmem_alloc_waitkmem_free
relative path
be referred from
POSIX regular expressions
shows the details
absolute path
any symbols but functions
grep
complete
Linux Develop Tools 23
Incremental updatingIncremental updating% gtags% vi tty.c modify tty.c. ...% global –vi -v means verbose. [Sun Dec 6 16:27:47 JST 1998] Gtags started Tag found in '/usr/src/sys'. Incremental update. Updating tags of 'kern/tty.c' ...GTAGS..GRTAGS..GSYMS.. Done. Global databases have been modified. [Sun Dec 6 16:28:30 JST 1998] Done.
% global –vi try again. [Sun Dec 6 16:28:48 JST 1998] Gtags started Tag found in '/usr/src/sys'. Incremental update. Global databases are up to date. do nothing. [Sun Dec 6 16:28:52 JST 1998] Done.
global
Linux Develop Tools 24
Hypertext generatorHypertext generator% gtags <- make the tag database(GTAGS,GRTAGS,GSYMS)
% htags <- make the hypertext(HTML/)
% systags [-f] <- make the hypertext(HTML/) of the kernel (?)
[feitian@cmldb7 linux]$ ls G* -l-rw-r--r-- 2 root root 528384 Jun 16 23:04 GPATH-rw-r--r-- 2 root root 39276544 Jun 16 23:07 GRTAGS-rw-r--r-- 2 root root 46632960 Jun 16 23:13 GSYMS-rw-r--r-- 2 root root 7729152 Jun 16 23:04 GTAGS[feitian@cmldb7 linux]$ du -b HTML/156217344 HTML/S495616 HTML/I13279232 HTML/D71098368 HTML/R741376 HTML/files1740800 HTML/funcs94289920 HTML/cgi-bin337907712 HTML
global
Linux Develop Tools 25
Linux Develop Tools 26
Compact formatCompact format% gtags –c% htags –c% systags –c
[feitian@cmldb7 linux]$ ls -l G*-rw-r--r-- 2 root root 528384 Jun 20 01:55 GPATH-rw-r--r-- 2 root root 39276544 Jun 20 02:00 GRTAGS-rw-r--r-- 2 root root 46632960 Jun 20 02:17 GSYMS-rw-r--r-- 2 root root 7729152 Jun 20 01:55 GTAGS[feitian@cmldb7 linux]$ du -b HTML/37109760 HTML/S495616 HTML/I13033472 HTML/D54681600 HTML/R700416 HTML/files462848 HTML/funcs94294016 HTML/cgi-bin200826880 HTML
global
Linux Develop Tools 27
gdbgdb
What’s gdb ?It’s a debugger tool, and 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.
How to start gdb ?
% gcc –g file.c
% gdb program [ core ]
gdb
Linux Develop Tools 28
gdb simple example 1gdb simple example 1void main( int argc, char **argv ){ int *num = NULL, i; //num = (int*) malloc(sizeof(int) *
10);
for (i=0; i<10; i++) { num[i] = rand() % 100; printf("%d ", num[i]); } printf("\n"); qsort( (void *)num, 10, sizeof(int),
compare );
/* Output sorted list: */:
}
int compare( const void *arg1, const void *arg2 )
{ return *((int*)arg1) - *((int*)arg2);}
gdb
Linux Develop Tools 29
gdb simple example 1gdb simple example 1$ gcc -g t.cc$ ./a.outSegmentation fault (core dumped)$ gdb a.outGNU gdb 19991004Copyright 1998 Free Software
Foundation, Inc.:
(gdb) runStarting program:
/home/feitian/unix/sum/a.out
Program received signal SIGSEGV, Segmentation fault.
0x80484c7 in main (argc=1, argv=0xbffffb44) at t.cc:14
14 num[i] = rand() % 100;
(gdb) print num[i]Cannot access memory at address
0x0(gdb) print i$1 = 0(gdb) print num$2 = (int *) 0x0 got it !(gdb) quitThe program is running. Exit anyway?
(y or n) y$
gdb
Linux Develop Tools 30
Run / quitRun / quit
(gdb) run [argument]
(gdb) set args argument(gdb) show args show now argument
(gdb) quit
gdb
Linux Develop Tools 31
listlist
To print lines from a source file. By default, ten lines are printed.
(gdb) list line1, line2
(gdb) list routine_name
gdb
Linux Develop Tools 32
Step / NextStep / Next
(gdb) step [count]Continue running your program until control reaches a different source line.
(gdb) stepiStep one machine instruction.
(gdb) next [count]Continue to the next source line in the current (innermost) stack frame, but function calls that appear within the line of code are executed without stopping.
(gdb) nextiNext one machine instruction.
gdb
Linux Develop Tools 33
Break pointBreak point(gdb) break [filename:]line-number(gdb) break [filename:]routine-name(gdb) break line-or-func if condition(gdb) info breakpoints(gdb) watch condition(gdb) continue(gdb) disable breakpoint-no(gdb) enable breakpoint-no(gdb) enable once breakpoint-no(gdb) delete breakpoint-no(gdb) clear
gdb
Linux Develop Tools 34
gdb simple example 2gdb simple example 2 7 void main( int argc, char **argv ) 8 { 9 int *num = NULL, i;10 num = (int*) malloc(sizeof(int) * 10);1112 for (i=0; i<10; i++) {13 num[i] = rand() % 100;14 printf("%d ", num[i]);15 }16 printf("\n");1718 qsort( (void *)num, 10, sizeof(int),
compare );1920 /* Output sorted list: */
:}
28 int compare29 ( const void *arg1, const void
*arg2 )30 {31 return *((int*)arg1) -
*((int*)arg2);32 }
gdb
Linux Develop Tools 35
gdb simple example 2gdb simple example 2$ gcc -g t.cc
$ gdb a.out:
(gdb) break 10Breakpoint 1 at 0x80484af: file t.cc, line 10.(gdb) break 18Breakpoint 2 at 0x804858d: file t.cc, line 18.(gdb) info breakpointsNum Type Disp Enb Address What1 breakpoint keep y 0x080484af in main at t.cc:102 breakpoint keep y 0x0804858d in main at t.cc:18
(gdb) runStarting program: /home/feitian/unix/sum/a.out
Breakpoint 1, main (argc=1, argv=0xbffffb44) at t.cc:1010 num = (int*) malloc(sizeof(int) * 10);(gdb) continueContinuing.83 86 77 15 93 35 86 92 49 21
Breakpoint 2, main (argc=1, argv=0xbffffb44) at t.cc:1818 qsort( (void *)num, 10, sizeof(int), compare );(gdb)
gdb
Linux Develop Tools 36
typetype
(gdb) whatis variable(gdb) ptype variable
complex v;(gdb) whatis v
type = struct complex
(gdb) ptype vtype = struct complex {
double real; double imag;}
gdb
Linux Develop Tools 37
printprint
(gdb) print[/f] express
(gdb) print variable@count
(gdb) x/nfu addr
n : count
f : format, x c d f u o t a
u : size, b h w g
binary pointer
gdb
Linux Develop Tools 38
Call StackCall Stack
(gdb) where / backtrace / info stack
(gdb) frame
(gdb) up [n]
(gdb) down [n]
func3
func2
func1
main
up
down
frame
where
gdb
Linux Develop Tools 39
dddddd
What’s ddd:A graphic UI for gdb and dbx.
How to start ddd ?% gcc –g file.c
% ddd program [ core ]
ddd
Linux Develop Tools 40
ddd
Linux Develop Tools 41
Command ToolCommand Tool
ddd
Linux Develop Tools 42
Tool barTool bar
ddd
Linux Develop Tools 43
Print / Display / Looking up DefinitionsPrint / Display / Looking up Definitions
ddd
Linux Develop Tools 44
BreakpointsBreakpoints
ddd
Linux Develop Tools 45
DisplayDisplay
ddd
Linux Develop Tools 46
DisplayDisplay
ddd
Linux Develop Tools 47
PlotPlot
ddd
Linux Develop Tools 48
GPROFGPROF
What’s gprof ?Profiling allows you to learn where your program spent its time and which functions called which other functions while it was executing. It can also tell you which functions are being called more or less often than you expected.
How to start gprof ?% gcc –o a.out –pg file.c% ./a.out It will write the profile data into file `gmon.out'
% gprof a.out > profile.txt
gprof
Linux Develop Tools 49
Flat profile:Flat profile: % cumulative self self total
time seconds seconds calls us/call us/call name
50.00 0.02 0.02 50602 0.40 0.40 SCRDC::Putc(int, int, char)
25.00 0.03 0.01 503943 0.02 0.02 vector<basic_string<char, string_ …
25.00 0.04 0.01 336 29.76 54.92 SCRDC::Fill(RECT, char)
0.00 0.04 0.00 503936 0.00 0.02 __vc__t6vector2Zt12basic_string3Z …
0.00 0.04 0.00 13670 0.00 0.00 vector<basic_string<char, string_ …
0.00 0.04 0.00 13670 0.00 0.00 vector<basic_string<char, string_ …
0.00 0.04 0.00 13670 0.00 0.00 vector<basic_string<char, string_ …
0.00 0.04 0.00 7695 0.00 1.07 TMemo::LineLen(int)
0.00 0.04 0.00 5250 0.00 0.00 SCRDC::Print(int, int, char *,...)
0.00 0.04 0.00 3994 0.00 0.00 POS::POS(void)
0.00 0.04 0.00 3022 0.00 0.00 POS::operator=(POS const &)
:
:
gprof
Linux Develop Tools 50
Flat profile fields explainFlat profile fields explain% Time
The percentage of the function total running time.
Cumulative secondsA running sum of the number of seconds accounted for by this function and those listed above it.
Self secondsThe number of seconds accounted for by this function alone.
CallsThe number of times this function was invoked.
Self ms/callThe average number of milliseconds spent in thisfunction per call.
Total ms/callThe average number of milliseconds spent in this function.
NameThe name of the function.
gprof
Linux Develop Tools 51
Call graphCall graphindex % time self children called name
<spontaneous>
[1] 100.0 0.00 0.04 main [1]
0.00 0.04 1/1 CSys::Run(void) [2]
0.00 0.00 1/1 CDestop::CDestop(void) [34]
0.00 0.00 1/1 CSys::Enter(void) [120]
0.00 0.00 1/1 CDestop::Create(void) [119]
0.00 0.00 1/1 CSys::Exit(void) [121]
0.00 0.00 1/1 CDestop::~CDestop(void) [248]
-----------------------------------------------
0.00 0.04 1/1 main [1]
[2] 100.0 0.00 0.04 1 CSys::Run(void) [2]
0.00 0.02 30/30 CSys::KeySender(int) <cycle 3> [67]
0.00 0.02 22/50 CSys::Paint(void) [4]
0.00 0.00 30/1235 POS::POS(int, int) [200]
0.00 0.00 30/3022 POS::operator=(POS const &) [198]
gprof
Linux Develop Tools 52
Call graph fields explainCall graph fields explain% Time
This is the percentage of the `total' time that was spent in this function and its children. Note that due to different viewpoints, functions excluded by options, etc, these numbers will NOT add up to 100%.
SelfThis is the total amount of time spent in this function.
ChildrenThis is the total amount of time propagated into this function by its children.
CalledThis is the number of times the function was called. If the function called itself recursively, the number only includes non-recursive calls, and is followed by a `+' and the number of recursive calls.
NameThe name of the current function.
gprof
Linux Develop Tools 53
CVS - Concurrent Versions SystemCVS - Concurrent Versions System
What’s cvs ?It’s a front-end of RCS. It is a version control system. Using it, you can record the history of your source files.
Terminology:repository
pserver
CVSROOT
cvs
Linux Develop Tools 54
CVS Beginning (local)CVS Beginning (local)
% CVSROOT=~/cvsroot/
% export CVSROOT
% cvs init init repository
% cd project/
% cvs import project –m “some message” vendor start
If you don’t set CVSROOT environment variable, you must add argument ‘ -d cvsroot ’ to assign repository path.
cvs
Linux Develop Tools 55
CVS Usage ModelCVS Usage Model
CheckoutCheckout
Makes private copy in working directory
Can check out anywhere
Check out multiple copies, multiple versions
CommitCommit
Commit changes to the repository when finished
Working copies must be up to date with repository
UpdateUpdate
Brings working copy up to date with repository
cvs
Linux Develop Tools 56
Concurrent checkoutConcurrent checkoutMaster Repository
foo.c
WorkingCopyV1.7
WorkingCopy V1.1
WorkingCopy V1.2
WorkingCopyV1.7
WorkingCopy
V1.2.2.1
checkout branch rel_1_fix checkout latest
checkout latest
checkoutV1.2
checkoutV1.1Kant
Fei-tian yichenPatrick
X XV1.8 or 1.9
V1.8 or 1.9V1.2.2.2
checkin
checkincheckin
checkin prohibited
Lulala
Checkout does not lock the files in repository
cvs
Linux Develop Tools 57
Ideal development with CVSIdeal development with CVScheckoutupdatecheckindevelopment
repository
Developer A
Developer B
cvs
Linux Develop Tools 58
Real development with CVSReal development with CVS
checkin
X
repository
Developer A
Developer B
updateconflict resolutioncheckin
conflict
cvs
Linux Develop Tools 59
Common CVS commandsCommon CVS commands
% cvs [cvs-options] command [cmd-options] [files]
cvs checkout Check out source for editing.
cvs add Add new file/directory to repository.
cvs remove Remove an entry from the repository.
cvs status Show status of checked out files.
cvs log Show revision history for files.
cvs diff Compare working files to version in repository or versions inside the repository.
cvs update Bring working files into sync with repository.
cvs commit check files into the repository
cvs tag Label the sources.
cvs
Linux Develop Tools 60
CVS exampleCVS example% cvs checkout projectcvs checkout: Updating projectU project/MakefileU project/bar.cU project/foo.cU project/main.c
% joe main.c::
% cvs commit -m “add a explaining message." main.cChecking in main.c;/class/'username'/cvsroot/project/main.c,v <-- main.cnew revision: 1.2; previous revision: 1.1done
cvs
Linux Develop Tools 61
Conflict exampleConflict examplevoid foo(){
printf("FOO\n");}
void foo(){
printf("FOO\n"); printf("YOU\n");
}
void foo(){
printf("FOO\n"); printf("TOO\n");
}
void foo()
{
printf("FOO\n");
<<<<<<< foo.c
printf("TOO\n");
=======
printf("YOU\n");
>>>>>>> 1.2
}
Orig
ina
lV
ersio
n.
AV
ersio
n.
B
Merg
e
cvs
Linux Develop Tools 62
Conflict exampleConflict example% cvs update foo.cRCS file: /class/'username'/cvsroot/project/foo.c,vretrieving revision 1.1.1.1retrieving revision 1.2Merging differences between 1.1.1.1 and 1.2 into foo.crcsmerge: warning: conflicts during mergecvs update: conflicts found in foo.cC foo.c
% cvs commit –m foo.c
% cvs checkout foo.c
% cvs checkout foo.c
% cvs commit foo.c
% cvs commit foo.cThis will conflict …
% cvs status foo.cFile: foo.c Status: Needs Merge Working revision: 1.1.1.1 'Some Date' Repository revision: 1.2 /class/'username'/cvsroot/project/foo.c,v
Sticky Tag: (none)
Sticky Date: (none)
Sticky Options: (none)
cvs
Linux Develop Tools 63
The Status of a fileThe Status of a file
Up-to-date The file is identical with the latest revision in the repository.
Locally Modified You have edited the file, and not yet committed your changes.
Needing Patch Someone else has committed a newer revision to the repository.
Needs Merge Someone else has committed a newer revision to the repository, and
you have also made modifications to the file.
cvs
Linux Develop Tools 64
Working on branchesWorking on branches
1.1 1.2 1.3
release_1
cvs tag release_1
cvs up -r rel_1_fix
1.4
release_2
cvs tag release_2
1.2.2.1 1.2.2.2
patch
rel_
1_fixcvs tag -b rel_1_fix
cvs
Linux Develop Tools 65
Remote CVS setupRemote CVS setup/etc/services
cvspserver 2401/tcp #CVS network server
cvspserver 2401/udp #CVS network server
/etc/inetd.conf
cvspserver stream tcp nowait root /usr/bin/cvs cvs -b /usr/bin \
--allow-root=/home/cvsroot pserver
% mkdir /home/cvsroot
% cvs –d /home/cvsroot init
cvs
Linux Develop Tools 66
Use remote CVS serverUse remote CVS server
% CVSROOT=:pserver:[email protected]:/home/cvsroot
% export CVSROOT
% cvs login
Password: ******
Do this will create a file in your home directory:
~/.cvspass
cvs