Download presentation
Presentation is loading. Please wait.
Published byJames Manning Modified over 9 years ago
1
Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools
2
GCC gcc – compile and link Compile options -o outfile -c -Dname -Idir -Ldir -lname -Wall -On -Os -g
3
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.
4
Using Make Macros Can define macros using macro=val pairs. Commonly defined macros CC OBJS CFLAGS LDLIBS
5
Special Macros $@ - The target of this rule $< - The first dependency of this rule
6
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
7
Pattern Rules More powerful than suffix rules Can have dependencies Matches patterns provided %.o : %.c $(CC) –c $(CFLAGS) –o $@ $<
8
Commonly Expected Targets all install uninstall clean
9
Phony Targets Sometimes we need to specify a rule has no corresponding file Use the.PHONY directive.PHONY: target
10
Open Source Code GNU Build System Autoconf Automake Libtool
11
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
12
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
13
Compiling Against Static Libraries Functions copied from library into binary during linking gcc –c –o hello.o hello.c ar rcs libhello.a hello.o gcc –Wall –c main.c –o main.o gcc –o hello main.o –L. –lhello./hello
14
Shared Libraries One copy of code in library that multiple programs can use Smaller programs Easier to make updates Portability issues Backward compatibility
15
Creating Shared Libraries Position Independent Code –fPIC Don’t use –fomit-frame-pointer gcc –fPIC –c hello.c –o hello.o gcc –shared –Wl,-soname,libfoo.so.1 –o libfoo.so.1.5 hello.o
16
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
17
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
18
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)
19
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!
20
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
21
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
22
GLIBC Memory Debugging Common Problems Memory Leaks Buffer Over/Under flow Tools env variable MALLOC_CHECK_ mcheck library
23
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()
24
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!
25
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
26
Memory Debugging Tracing Memory Leaks Set env MALLOC_TRACE to a logfile Call mtrace from gdb
27
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.