unix programming tools operating systems. man- on-line unix manual vi - a command line text editor...

50
Unix Programming Tools Operating Systems

Post on 21-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

Unix Programming Tools

OperatingSystems

Page 2: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

man - on-line unix manualvi - a command line text editormake - runs make filesgcc - compilergdb - debugger

OperatingSystems

Page 3: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

man pages

OperatingSystems

Page 4: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 5: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 6: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

vi

OperatingSystems

Page 7: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

Why learn a command line editor?

* it’s lean and fast* always available - even from a terminal

OperatingSystems

Page 8: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

$ vi [ filename ]

if no filename is provided, vi creates a new unnamed file

Starting vi

OperatingSystems

Page 9: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 10: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 11: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

vi commands are usually of the foem

[count] command [where]

for example.

23x deletes 23 characters

vi Commands

OperatingSystems

Page 12: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 13: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

[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

Page 14: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

ctrl-B scroll back one page

ctrl-F scroll forward one page

Some simple vi commands

OperatingSystems

Page 15: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 16: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

[n]w move forward n words

[n]b move backwards n words

Some simple vi commands

OperatingSystems

Page 17: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

[n]x delete n characters

[n]dd delete n ines

Some simple vi commands

OperatingSystems

Page 18: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 19: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

[“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

Page 20: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

[n]r char replace n characters with char

Some simple vi commands

OperatingSystems

Page 21: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

:set cindent shiftwidth=4 sets auto indent for C

Some simple vi commands

OperatingSystems

Page 22: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

vi Manual

OperatingSystems

http://vimdoc.sourceforge.net/htmldoc/usr_toc.html

Page 23: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

gcc

OperatingSystems

Page 24: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

gcc

OperatingSystems

gcc is the gnu C compiler. It processes input filein four distinct phases:

pre-processorcompilerassemblerlinker

Page 25: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

gcc flags

OperatingSystems

the following are common compiler flags. Seethe man pages for more information.

Page 26: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 27: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

gcc flags

OperatingSystems

-Idir add the directory dir to the list of directoriessearched for include files

-llibrary link in the name library

Page 28: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 29: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

gcc examples

OperatingSystems

compile a single source code file, and link

gcc test1.c

output will be a.out

Page 30: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

gcc examples

OperatingSystems

compile a single source code file, don’t link

gcc -c test1.c

output will be test1.o

Page 31: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 32: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

gcc examples

OperatingSystems

compile a single source code file, link math library

gcc -o power power.c -lm

output will be power (no extension)

Page 33: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

make

OperatingSystems

Page 34: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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.

Page 35: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 36: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

make example

OperatingSystems

Consider a project that contains the followingsource code files:

main.cstudentinfo.cstudentinfo.h

Page 37: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 38: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 39: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

make example

OperatingSystems

Step Two: Compile main.c

main.o: main.c studentinfo.hgcc -c main.c

Page 40: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

make example

OperatingSystems

Step Three: Link

demo.exe: main.o studentinfo.ogcc -o demo.exe main.o studentinfo.o

Page 41: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 42: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 43: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

gdb

OperatingSystems

Page 44: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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( )

Page 45: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 46: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 47: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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.

Page 48: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 49: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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

Page 50: Unix Programming Tools Operating Systems. man- on-line unix manual vi - a command line text editor make- runs make files gcc- compiler gdb- debugger Operating

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