View
215
Download
0
Tags:
Embed Size (px)
Citation preview
Unix Programming Tools
OperatingSystems
man - on-line unix manualvi - a command line text editormake - runs make filesgcc - compilergdb - debugger
OperatingSystems
man pages
OperatingSystems
Most unix systems have a set of on-linedocumentation called the man pages. Theseare typically organized into several sections.The first three are of most interest to us:
1 user commands2 system calls3 C library functions
OperatingSystems
Each man page covers a specific command, utility,or system call. Individual man pages commonly aredivided into the following sections:
HEADER the title of this man pageNAME one line summarySYNOPSIS brief descriptionDESCRIPTION detailed descriptionERRORS conditions for errorsFILES files usedBUGS known bugs
OperatingSystems
vi
OperatingSystems
Why learn a command line editor?
* it’s lean and fast* always available - even from a terminal
OperatingSystems
$ vi [ filename ]
if no filename is provided, vi creates a new unnamed file
Starting vi
OperatingSystems
ESC - to get into command more :q to quit :q! to force a quit without saving the file :wq to save the file and then quit
Leaving vi
OperatingSystems
Command mode and Insert Mode Command mode Insert Mode: press i or a Insert Mode Command Mode: press Esc key
The two modes of vi
OperatingSystems
vi commands are usually of the foem
[count] command [where]
for example.
23x deletes 23 characters
vi Commands
OperatingSystems
a enter insert mode, the characters typed willbe after the current cursor position.
i enter insert mode, the character types willbe before the current cursor position
Some simple vi commands
OperatingSystems
[n]j move the cursor down n lines
[n]k move the cursor up n lines
[n]h move the cursor left n characters
[n]l move the cursor right n characters
Some simple vi commands
OperatingSystems
ctrl-B scroll back one page
ctrl-F scroll forward one page
Some simple vi commands
OperatingSystems
g0 move to the first character on the line
g$ move to the last character on the line
[n]G go to line n
[n]gg go to line n
Some simple vi commands
OperatingSystems
[n]w move forward n words
[n]b move backwards n words
Some simple vi commands
OperatingSystems
[n]x delete n characters
[n]dd delete n ines
Some simple vi commands
OperatingSystems
the un-named register (“”)
numbered registers (0-9) 0 used by most recent yank command 1 used by most recent delete command
named registers (a-z and A-Z)
:reg show contents of all registers
vi registers
OperatingSystems
[“x][n]yy yank n lines into register x
[“x][n]dd delete n lines into register x
[“x][n]p put register x after the cursor, n times
Some simple vi commands
OperatingSystems
[n]r char replace n characters with char
Some simple vi commands
OperatingSystems
:set cindent shiftwidth=4 sets auto indent for C
Some simple vi commands
OperatingSystems
vi Manual
OperatingSystems
http://vimdoc.sourceforge.net/htmldoc/usr_toc.html
gcc
OperatingSystems
gcc
OperatingSystems
gcc is the gnu C compiler. It processes input filein four distinct phases:
pre-processorcompilerassemblerlinker
gcc flags
OperatingSystems
the following are common compiler flags. Seethe man pages for more information.
gcc flags
OperatingSystems
-ansi enforce full ansi compliance
-c compile and assemble, but do not link
-g create a symbol table for debugging
-E stop after running the pre-processor.do not compile
gcc flags
OperatingSystems
-Idir add the directory dir to the list of directoriessearched for include files
-llibrary link in the name library
gcc flags
OperatingSystems
-O optimize. There are 3 levels of optimization,O1, O2, and O3
-o filename stores the resulting output in filename, most often used for linker output
- Wall issue compiler warnings
gcc examples
OperatingSystems
compile a single source code file, and link
gcc test1.c
output will be a.out
gcc examples
OperatingSystems
compile a single source code file, don’t link
gcc -c test1.c
output will be test1.o
gcc examples
OperatingSystems
compile multiple source code files, and link
gcc -c test1.c gcc -c test2.c gcc -o test.exe test1.o test2.o
output from the first line will be test1.o
gcc examples
OperatingSystems
compile a single source code file, link math library
gcc -o power power.c -lm
output will be power (no extension)
make
OperatingSystems
make
OperatingSystems
The unix make utility allows programmers tocreate “recipes” for compiling and linking multiplefiles in a project. These “recipes” called make files,allow for incremental re-compilation ... only changedfiles are recompiled.
make
OperatingSystems
Make files are located in the same directory asthe source code files they are meant to work with.A make file is made up of a set of dependencies andrules.
a dependency - defines a target file and the files required to build that target
a rule - tells the system what it must do to build a target
make example
OperatingSystems
Consider a project that contains the followingsource code files:
main.cstudentinfo.cstudentinfo.h
make example
OperatingSystems
Step One: Compile the studentinfo file
studentinfo.o: studentinfo.c studentinfo.h
this is thename ofthe target
in order to build thistarget, we need these source code files
the first line lists the dependecies
make example
OperatingSystems
Step One: Compile the studentinfo file
studentinfo.o: studentinfo.c studentinfo.hgcc -c studentinfo.c
the second line tells the rule to build studentinfo.o
the rule line must startwith a tab character
make example
OperatingSystems
Step Two: Compile main.c
main.o: main.c studentinfo.hgcc -c main.c
make example
OperatingSystems
Step Three: Link
demo.exe: main.o studentinfo.ogcc -o demo.exe main.o studentinfo.o
make example
OperatingSystems
The complete makefile
demo.exe: main.o studentinfo.ogcc -o demo.exe main.o studentinfo.o
main.o: main.c studentinfo.hgcc -c main.c
studentinfo.o: studentinfo.c studentinfo.hgcc -c studentinfo.c
make example
OperatingSystems
To execute the makefile, type make on the command line.This makefile should produce the following output:
gcc -c studentinfo.cgcc -c main.cgcc -o demo.exe main.o studentinfo.o
gdb
OperatingSystems
Starting GDBStarting GDBStarting GDBStarting GDB
At the command prompt type
gdb program_name
program_name is an executable, compiled with –g option
the –g option creates a symbol table used by gdb
the current source file is set to the file containing main andthe current source line is set to the first executable statementin main( )
Program ExecutionProgram ExecutionProgram ExecutionProgram Execution
run run (or rerun) the program from the beginning
step execute the next source instruction and returnto gdb. Follow subroutine calls.
step count execute count source lines
next same as step, but does not step into functions
finish run until the current function returns
return return to calling function
jump address jump to specified source line
BreakpointsBreakpointsBreakpointsBreakpointsinfo break list all breakpoints
break function set breakpoint at beginning of function
break linenumber set breakpoint at this line number
break filename:line set breakpoint at specified line in file
break fn if expr stop at breakpoint fn if expr is true
disable breaknum disable or enable breakpoint breaknumenable breaknum
delete breaknum delete breakpoint breaknum
command breaknum execute the commands when breaknum is reached
cont continue execution
The Stack FrameThe Stack FrameThe Stack FrameThe Stack Frame
A stack frame is the portion of the run-time stack allocated to the currently executing function.
gdb assigns numbers to stack frames, counting from zero for the currently executing function.
Variable names are all relative to the current stack frame.
Examining the StackExamining the StackExamining the StackExamining the Stack
backtrace show stackframesuseful for looking at calling sequence
frame framenumber look at stack frame framenumber
down look at stack frame called by this one
up look at stack frame calling this one
info args show argument variables in the current stack frame
info locals show local variables in the current stack frame
Source CodeSource CodeSource CodeSource Code
list linenum print 10 lines of source code, centered around linenum
list function print 10 lines of source code, centered around the
beginning of function
list print 10 more lines of source code
Examining DataExamining DataExamining DataExamining Data
print expression print value of expression, evaluated withinthe current stack frame
set variable = expression assign result of expression to variable
display expression print value of expression every time program stops ( a watch)
undisplay cancels previous display requests