Unix: Programming with Standard I/O

Download Report

Transcript Unix: Programming with Standard I/O

Unix: Development Tools
CSRU3130, Spring 2008
Ellen Zhang
Today
•
•
•
•
•
•
A command a day: find
A Script a day:
Make
gdb
Profiling
memcheck
A command a day: find
• find command: process a set of files and/or
directories in a file subtree; you can specify
– where to search (pathname)
– what type of file to search for (-type: directories,
data files, links)
– how to process the files (-exec: run a process
against a selected file)
– the name of the file(s) (-name)
– perform logical operations on selections (-o and a)
Spring 2008
CSRU3130 Unix Programming
3
Examples
• Search for file with a specific name in a set of
files (-name)
– find . -name "rc.conf" -print
• Apply a unix command to all files found:
– find . -name "rc.conf" -exec chmod o+r '{}' \;
• Search for a string in a selection of files
– find . -exec grep "www.athabasca" '{}' \; -print
– search in current directory and all sub directories.
All files containing string will have their path
Spring 2008 printed to standard
CSRU3130
Unix Programming
4
output.
More ways to find files
• Find all files under the root, that is regular file
and modified seven or fewer days ago
– find / -type f -mtime -7 -print
Spring 2008
CSRU3130 Unix Programming
5
xargs
• find / -type f -mtime -7 | xargs tar -rf
weekly_incremental.tar
• xargs, a handy utility that coverts a stream of
input into command line arguments for the
supplied command
• Examples:
– find / -name core | xargs /bin/rm -f
– find / -name core -exec /bin/rm -f '{}' \; # same
thing
– find / -name core -delete # same if using Gnu find
GCC overview
• GNU Compiler Collection includes front ends
for C, C++, Objective-C, Fortran, Java, and Ada,
as well as libraries for these languages
(libstdc++, libgcj,...).
• gcc is the "GNU" C Compiler, and g++ is the
GNU C++ compiler
Compiling
Object files
Things can go wrong
• Preprocessor: processing directives, ifdef,
define, include, …
– some header files not found …
– Illegal directives
• Compiler:
– variable/function undeclared,
– syntax errors (missing semicolon, etc), etc.
• Linker: undefined reference to `foo'
collect2: ld returned 1 exit status
http://www.network-theory.co.uk/docs/gccintro/index.html
9
Warning messages
are important
#include <stdio.h>
int main (void) {
printf ("Two plus two is %f\n", 4);
return 0;
}
Any problem ?
What you need to know about
compiler ?
$ gcc –o myProg main.c
$ ./myProg
Two plus two is -0.693111
• Why ?
– printf does not check argument’s type, an integer 4
is printed out as a float
– Integers and floating-point numbers are stored in
different formats in memory, and generally occupy
different numbers of bytes, leading to a spurious
result.
This can be avoided
• Always turn on warning options, and make
sure your program compile cleanly (i.e.,
compile successfully and without warning
message)
$ gcc -Wall main.c -o main
main.c: In function `main': main.c:6: warning:
double format, different type arg (arg 2)
More about runtime errors after
we learn some basics about make
Compiling larger program
• Example
• Compile green.o: cc -c green.c
• Compile blue.o: cc -c blue.c
• Link the parts together: cc green.o blue.o
Splitting your C program
• Be sure no two files have functions with same name
in it, and no two files define same global variables.
• If you use global variables, be sure only one file
defines them, and declare them in your .h as follows:
– extern int globalvar;
• To use functions from another file, make a .h file with
function prototypes, and include those .h files within
your .c files.
• At least one of the files must have a main() function.
Dependency graph
Allow one to identify what needs to be compiled …
Dependency graph
represented by makefile
Command to update targets
Make:
* read dependency info from makefile
• checks modification times of files
• whenever a file becomes "newer" than something that
depends on it, make runs given command accordingly.
Syntax of makefile
• target : source file(s)
command (must be preceded by a tab)
• A target given in the Makefile is a file which
will be created or updated when any of its
source files are modified.
• Command(s) given in subsequent line(s)
(which must be preceded by a tab character)
are executed in order to create the target file.
Dependency on header file
main.o: data.h io.h main.c
cc –c main.c
• .h files are listed, but there are no references
in their corresponding commands.
– the .h files are referred within main.c files through
#include “data.h“, etc.
– If you do not explicitly include these in your
Makefile, your program will not be updated if you
make a change to your header (.h) files.
Running make
• The order in which dependencies are listed is
important. If you simply type make and then
return, make will attempt to create or update
the first dependency listed.
• You can also specify one particular targets
listed in the Makefile, and only that target
(and its corresponding source files) would be
made.
Example of makefile
SRC_FILES=main.c app.c bar.c lib.c
OBJ_FILES=$(patsubst %.c, %.o, ${SRC_FILES})
CFLAGS= -c –g
Make features:
LDFLAGS= -g
• Variables
• String manipulation functions
appexp: $(OBJ_FILES)
gcc $(LDFLAGS) –o appexp ${OBJ_FILES}
%.o:%.c
gcc ${CFLAGS} –o $@ $
Pattern rule: how to transform *.c files
into corresponding *.o files
$@: Filename matched for the left side of the rule
$: filename matched for the right side of the rule
Example of makefile (cont’d)
clean:
rm *.o appexp
MAIN_HDRS=lib.h app.h
LIB_HDRS=lib.h
main.o: ${MAIN_HDRS}
app.o: $(MAIN_HDRS}
bar.o:${LIB_HDRS}
lib.o: ${LIB_HDRS}
A target without source
• type “make clean” to delete
all objects and executables
Back to program debugging
Runtime error
• Segmentation fault or bus error
– These runtime messages indicate a memory
access error. Common causes include:
dereferencing a null pointer or uninitialized
pointer
– out-of-bounds array access
– incorrect use of malloc, free and related functions
– use of scanf with invalid arguments
Runtime error (cont’d)
• floating point exception
– caused by an arithmetic exception, such as
division by zero, overflow, underflow or an invalid
operation (e.g. taking the square root of -1). The
operating system determines which conditions
produce this error.
• Illegal instruction
– an illegal machine instruction is encountered. It
occurs when code has been compiled for one
specific architecture and run on another.
GNU debugger: gdb
• A debugger is a program that runs other
programs, allowing the user to
– exercise control over these programs,
– examine variables when problems arise.
Basic features of a debugger
• Allow one to step through the logic, e.g.,
– What statement or expression did the program
crash on?
– If an error occurs while executing a function, what
line of the program contains the call to that
function, and what are the parameters?
– What are the values of program variables at a
particular point during execution of the program?
– What is the result of a particular expression in a
program?
How to use gdb ?
• Compile your code with debug info:
– -g option for compilation and linking
• Run your program under gdb
– gdb yourProg
• Now gdb load your program, and ready to
execute the program …
gdb commands
• run command-line-arguments
– Starts your program as if you had typed
• a.out command-line arguments
– program will start running, and
• halts at the statement that causes runtime errors such
as segmentation fault and bus error
• runs to the end if there is no runtime error
Examining calling stack
• where
– Produces a backtrace - the chain of function calls
that brought the program to its current place. The
command backtrace is equivalent
• up
– go up one level in the calling stack
• down
– go down one level in the calling stack
Display variable value
• print E
– prints the value of E in the current (stack) frame in
the program, where E is a C expression (usually
just a variable).
• display E
– Similar to print, except every time you execute a
next or step, it will print out the expression based
on the new variable values
Changing data
• Change the data in a operating program
– (gdb) set op=3
Set breakpoints
• make the program stop at a given statement
• break place
– place can be function, file:function,line, file:line,
address (a physical address)
– Most common breakpoints are at the beginnings of
functions, as in
– (gdb) break Traverse
Breakpoint 2 at 0x2290: file main.c, line 20
– break main stops at the beginning of execution
Set breakpoints (cont’d)
• set breakpoints at a particular line in a file:
– (gdb) break 20
Breakpoint 2 at 0x2290: file main.c, line 20
• Conditional breakpoint
– (gdb) break operator if op=2
Break at function “operator” if op argument equals to 2
• When hitting a breakpoint, you'll get a message like
this.
– Breakpoint 1, Traverse(head=0x6110, NumNodes=4)
at main.c:16
(gdb)
Executing program
• next (n)
– Executes the current line of the program and stops
on the next statement to be executed. If the
current line of the program contains a function
call, it executes the function and stops at the next
line.
• Step (s)
– Similar to next, except if current line is a function
call, it executes next statement, and stops at the
first line of the function
Executing program
• finish
– Keeps doing nexts, without stepping, until
reaching the end of the current function
• cont
– Continues regular execution of the program until a
breakpoint is hit or the program stops
• Ctrl+c
– Stop the current running program
Other ways to run gdb
•
•
Some process might hang around after
running for a while …
Debugging an existing process
– $ gdb
– (gdb) attach 23548
•
•
Debug the program as usual
Release the process to continue normal
operations
(gdb) detach
run gdb: postmortem
• “Core dumped”: recorded state of working memory
of a program at a specific time, generally when the
program has terminated abnormally a program when
it encounters a run time error
$ ulimit –c unlimited
$ ./testapp
…..
Segmentation fault (cored dumped)
$ ls
core.34523 testapp testapp.c
$gdb testapp core.34523
Using GDB under Emacs
• Emacs provides a much better interface to gdb
that saves a lot of typing and confusion.
– Executing Emacs command M-x gdb starts up a new
window running gdb.
– If you set a breakpoint in main function and then
run program with correct arguments, gdb will split
the window in two, with your source code on the
bottom and a gdb command line in the top.
–…