Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging...

27
Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools

Transcript of Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging...

Page 1: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

Programming Tools

gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools

Page 2: 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

Page 3: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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.

Page 4: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

Using Make Macros

Can define macros using macro=val pairs.

Commonly defined macros CC OBJS CFLAGS LDLIBS

Page 5: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

Special Macros

$@ - The target of this rule $< - The first dependency of this

rule

Page 6: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 7: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

Pattern Rules

More powerful than suffix rules Can have dependencies

Matches patterns provided%.o : %.c

$(CC) –c $(CFLAGS) –o $@ $<

Page 8: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

Commonly Expected Targets

all install uninstall clean

Page 9: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

Phony Targets

Sometimes we need to specify a rule has no corresponding file

Use the .PHONY directive

.PHONY: target

Page 10: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

Open Source Code

GNU Build System Autoconf Automake Libtool

Page 11: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 12: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 13: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 14: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

Shared Libraries

One copy of code in library that multiple programs can use Smaller programs Easier to make updates Portability issues Backward compatibility

Page 15: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 16: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 17: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 18: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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)

Page 19: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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!

Page 20: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 21: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 22: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

GLIBC Memory Debugging

Common Problems Memory Leaks Buffer Over/Under flow

Tools env variable MALLOC_CHECK_ mcheck library

Page 23: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 24: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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!

Page 25: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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

Page 26: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

Memory Debugging

Tracing Memory Leaks Set env MALLOC_TRACE to a logfile Call mtrace from gdb

Page 27: Programming Tools gcc make utility Open Source code Static and Shared Libraries gdb Memory debugging tools.

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