510 likes | 647 Views
Operating Systems. 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 Systems. man pages. Operating Systems. Most unix systems have a set of on-line
E N D
Operating Systems 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 Systems man pages
Operating Systems Most unix systems have a set of on-line documentation called the man pages. These are typically organized into several sections. The first three are of most interest to us: 1 user commands 2 system calls 3 C library functions
Operating Systems Each man page covers a specific command, utility, or system call. Individual man pages commonly are divided into the following sections: HEADER the title of this man page NAME one line summary SYNOPSIS brief description DESCRIPTION detailed description ERRORS conditions for errors FILES files used BUGS known bugs
Operating Systems vi
Operating Systems Why learn a command line editor? * it’s lean and fast * always available - even from a terminal
Operating Systems Starting vi $ vi [ filename ] if no filename is provided, vi creates a new unnamed file
Operating Systems Leaving vi 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
Operating Systems The two modes of vi Command mode and Insert Mode Command mode Insert Mode: press i or a Insert Mode Command Mode: press Esc key
Operating Systems vi Commands vi commands are usually of the foem [count] command [where] for example. 23x deletes 23 characters
Operating Systems Some simple vi commands a enter insert mode, the characters typed will be after the current cursor position. i enter insert mode, the character types will be before the current cursor position
Operating Systems Some simple vi commands [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
Operating Systems Some simple vi commands ctrl-B scroll back one page ctrl-F scroll forward one page
Operating Systems Some simple vi commands 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
Operating Systems Some simple vi commands [n]w move forward n words [n]b move backwards n words
Operating Systems Some simple vi commands [n]x delete n characters [n]dd delete n ines
Operating Systems vi registers 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
Operating Systems Some simple vi commands [“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
Operating Systems Some simple vi commands [n]r char replace n characters with char
Operating Systems Some simple vi commands :set cindent shiftwidth=4 sets auto indent for C
Operating Systems vi Manual http://vimdoc.sourceforge.net/htmldoc/usr_toc.html
Operating Systems gcc
Operating Systems gcc gcc is the gnu C compiler. It processes input file in four distinct phases: pre-processor compiler assembler linker
Operating Systems gcc flags the following are common compiler flags. See the man pages for more information.
Operating Systems gcc flags -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
Operating Systems gcc flags -Idir add the directory dir to the list of directories searched for include files -llibrary link in the name library
Operating Systems gcc flags -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
Operating Systems gcc examples compile a single source code file, and link gcc test1.c output will be a.out
Operating Systems gcc examples compile a single source code file, don’t link gcc -c test1.c output will be test1.o
Operating Systems gcc examples 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
Operating Systems gcc examples compile a single source code file, link math library gcc -o power power.c -lm output will be power (no extension)
Operating Systems make
Operating Systems make The unix make utility allows programmers to create “recipes” for compiling and linking multiple files in a project. These “recipes” called make files, allow for incremental re-compilation ... only changed files are recompiled.
Operating Systems make Make files are located in the same directory as the source code files they are meant to work with. A make file is made up of a set of dependencies and rules. 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
Operating Systems make example Consider a project that contains the following source code files: main.c studentinfo.c studentinfo.h
Operating Systems make example Step One: Compile the studentinfo file the first line lists the dependecies studentinfo.o: studentinfo.c studentinfo.h this is the name of the target in order to build this target, we need these source code files
Operating Systems make example Step One: Compile the studentinfo file the second line tells the rule to build studentinfo.o studentinfo.o: studentinfo.c studentinfo.h gcc -c studentinfo.c the rule line must start with a tab character
Operating Systems make example Step Two: Compile main.c main.o: main.c studentinfo.h gcc -c main.c
Operating Systems make example Step Three: Link demo.exe: main.o studentinfo.o gcc -o demo.exe main.o studentinfo.o
Operating Systems make example The complete makefile demo.exe: main.o studentinfo.o gcc -o demo.exe main.o studentinfo.o main.o: main.c studentinfo.h gcc -c main.c studentinfo.o: studentinfo.c studentinfo.h gcc -c studentinfo.c
Operating Systems make example To execute the makefile, type make on the command line. This makefile should produce the following output: gcc -c studentinfo.c gcc -c main.c gcc -o demo.exe main.o studentinfo.o
Operating Systems gdb
At the command prompt type gdb program_name Starting GDB 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 and the current source line is set to the first executable statement in main( )
Program Execution run run (or rerun) the program from the beginning step execute the next source instruction and return to 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
info 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 breaknum enable breaknum delete breaknum delete breakpoint breaknum command breaknum execute the commands when breaknum is reached cont continue execution Breakpoints
The 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.
backtrace show stackframes useful 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 Examining the Stack
Source 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 Data print expression print value of expression, evaluated within the 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