Unix: Development Tools - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Unix: Development Tools

Description:

printf does not check argument's type, an integer 4 is printed out as a float ... all objects and executables. Back to program debugging. Runtime error ... – PowerPoint PPT presentation

Number of Views:255
Avg rating:3.0/5.0
Slides: 40
Provided by: stormCis
Category:
Tags: an | development | error | how | is | message | not | null | object | or | stop | to | tools | unix

less

Transcript and Presenter's Notes

Title: Unix: Development Tools


1
Unix Development Tools
  • CSRU3130, Spring 2008
  • Ellen Zhang

2
Today
  • A command a day find
  • A Script a day
  • Make
  • gdb
  • Profiling
  • memcheck

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

4
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 or '' \
  • 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 printed to standard output.

5
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

6
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

7
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

8
Compiling
Object files
9
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/inde
x.html
10
Warning messages are important
  • include ltstdio.hgt
  • int main (void)
  • printf ("Two plus two is f\n", 4)
  • return 0
  • Any problem ?

11
What you need to know about compiler ?
  • gcc o myProg main.c
  • ./myProg
  • Two plus two is -0.693111
  • Why ?
  • printf does not check arguments 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.

12
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.c6 warning
    double format, different type arg (arg 2)

13
More about runtime errors after we learn some
basics about make
14
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

15
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.

16
Dependency graph
Allow one to identify what needs to be compiled
17
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.

18
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.

19
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.

20
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.

21
Example of makefile
  • SRC_FILESmain.c app.c bar.c lib.c
  • OBJ_FILES(patsubst .c, .o, SRC_FILES)
  • CFLAGS -c g
  • LDFLAGS -g
  • appexp (OBJ_FILES)
  • gcc (LDFLAGS) o appexp OBJ_FILES
  • .o.c
  • gcc CFLAGS o _at_
  • Make features
  • Variables
  • String manipulation functions

Pattern rule how to transform .c files into
corresponding .o files
_at_ Filename matched for the left side of the
rule filename matched for the right side
of the rule
22
Example of makefile (contd)
  • clean
  • rm .o appexp
  • MAIN_HDRSlib.h app.h
  • LIB_HDRSlib.h
  • main.o MAIN_HDRS
  • app.o (MAIN_HDRS
  • bar.oLIB_HDRS
  • lib.o LIB_HDRS
  • A target without source
  • type make clean to delete
  • all objects and executables

23
Back to program debugging
24
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

25
Runtime error (contd)
  • 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.

26
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.

27
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?

28
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

29
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

30
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

31
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

32
Changing data
  • Change the data in a operating program
  • (gdb) set op3

33
Set breakpoints
  • make the program stop at a given statement
  • break place
  • place can be function, filefunction,line,
    fileline, 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

34
Set breakpoints (contd)
  • set breakpoints at a particular line in a file
  • (gdb) break 20Breakpoint 2 at 0x2290 file
    main.c, line 20
  • Conditional breakpoint
  • (gdb) break operator if op2
  • Break at function operator if op argument
    equals to 2
  • When hitting a breakpoint, you'll get a message
    like this.
  • Breakpoint 1, Traverse(head0x6110,
    NumNodes4)at main.c16(gdb)

35
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

36
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
  • Ctrlc
  • Stop the current running program

37
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

38
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

39
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.
Write a Comment
User Comments (0)
About PowerShow.com