Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging...
-
Upload
james-manning -
Category
Documents
-
view
232 -
download
0
Transcript of Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging...
Programming Tools
gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools
GCC gcc – compile and
link Compile options
-o outfile -c -Dname -Idir -Ldir
-lname -Wall -On -Os -g
Make Utility “easy” compilation of complex
programs Requires creation of a makefile Usage: make [target] Looks for Makefile or makefile or
GNUMakefile. Use –f to specify a makefile filename
If no target given when running make, it will use the first one.
Using Make Macros
Can define macros using macro=val pairs.
Commonly defined macros CC OBJS CFLAGS LDLIBS
Special Macros
$@ - The target of this rule $< - The first dependency of this
rule
Suffix Rules
Make has built in rules for compiling certain things based on the suffix of the file
.c.o:$(CC) –c $(CFLAGS) –o $@ $<
Adding custom suffixes.SUFFIXES: .dat
Pattern Rules
More powerful than suffix rules Can have dependencies
Matches patterns provided%.o : %.c
$(CC) –c $(CFLAGS) –o $@ $<
Commonly Expected Targets
all install uninstall clean
Phony Targets
Sometimes we need to specify a rule has no corresponding file
Use the .PHONY directive
.PHONY: target
Open Source Code
GNU Build System Autoconf Automake Libtool
Installing Open Source Software
Download the tarball tar ztvf bash.tar.gz tar zxvf bash.tar.gz tar jxvf bash.tar.bz2
Compile and install ./configure make – compile only make install – compile and install
Libraries
Static Libraries (lib*.a files) Collection of object (*.o) files Maintains a table of what symbols are
defined by what object files Created using archiver utility
ar rcs libfoo.a obj1.o obj2.o obj3.o
Compiling Against Static Libraries
Functions copied from library into binary during linking
gcc –c –o hello.o hello.car rcs libhello.a hello.ogcc –Wall –c main.c –o main.ogcc –o hello main.o –L. –lhello./hello
Shared Libraries
One copy of code in library that multiple programs can use Smaller programs Easier to make updates Portability issues Backward compatibility
Creating Shared Libraries
Position Independent Code –fPIC Don’t use –fomit-frame-pointergcc –fPIC –c hello.c –o hello.ogcc –shared –Wl,-soname,libfoo.so.1
–o libfoo.so.1.5 hello.o
Compiling Against The Shared Library
System must know where to find the library. /usr/lib /lib Otherwise, add to LD_LIBRARY_PATH Define during program execution with
LD_LIBRARY_PATH = /path/to/lib ./progname Compile against the library
gcc main.c –o main –L. -lhello
GNU Debugger (gdb) Command Line Debugger Compile program with –g flag Run program through debugger
gdb progname gdb can examine core files
gdb progname corefile
gdb commands list / l
Lists lines of source code. Default is 10 lines around current address.
Can specify range: l 5,10 break / b
Sets a breakpoint on a given line clear / delete
Remove breakpoint(s)
gdb commands (cont) run / r
Starts the program to be debugged running from the begging until the first breakpoint
next / n Executes the next instruction. Does not
step into functions! step / s
Executes the next instruction. Does step into functions!
gdb commands (cont)
command breakpointnum Allows a list of commands to be run
when the breakpoint is encountered display
Displays some value at every breakpoint
undisplay Cancels the display command
gdb commands (cont) print / p
Displays the value of an expression“p option” will print the value of the variable “option”
set Sets the value of a variable
set option=10 where / w
Prints a backtrace. continue / c
Continues executing the program
GLIBC Memory Debugging
Common Problems Memory Leaks Buffer Over/Under flow
Tools env variable MALLOC_CHECK_ mcheck library
GLIBC Memory Debugging MALLOC_CHECK_
MALLOC_CHECK_=1 ./badmem prints any warning to stderr
MALLOC_CHECK_=2 ./badmem prints warning to stderr and calls
abort()
GLIBC Memory Debugging mcheck
Link against mcheck library gcc –g badmem.c –o badmem –lmcheck
Through gdb Set breakpoint for main Add a command for that breakpoint
“call mcheck(0)” “continue”
In all cases, mcheck must be called before any calls to malloc!
Memory Debugging
Preventing overflows with “electric fence” Link with –lefence Provides new form of malloc that
allocates additional space after requested space
Any attempt to write to that space will cause the Kernel to kill the process with SIGSEGV
Memory Debugging
Tracing Memory Leaks Set env MALLOC_TRACE to a logfile Call mtrace from gdb
Memory Debugging Other tools
mpr – Memory allocation profiler gcov – Coverate test tool strace – Trace system calls during execution ltrace – Trace library calls during execution mtrace – Trace memory allocation valgrind – debugging suite of tools