c programming tools
Download
Skip this Video
Download Presentation
C Programming Tools

Loading in 2 Seconds...

play fullscreen
1 / 36

C Programming Tools - PowerPoint PPT Presentation


  • 84 Views
  • Uploaded on

C Programming Tools. Lecturer: Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954. “UNIX for Programmers and Users” Third Edition, Prentice-Hall, GRAHAM GLASS, KING ABLES Slides partially adapted from Kumoh National University of Technology (Korea) and NYU.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'C Programming Tools' - landen


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
c programming tools

C Programming Tools

Lecturer: Prof. Andrzej (AJ) Bieszczad

Email: [email protected]

Phone: 818-677-4954

“UNIX for Programmers and Users”

Third Edition, Prentice-Hall, GRAHAM GLASS, KING ABLES

Slides partially adapted from Kumoh National University of Technology (Korea) and NYU

slide2

• Tools that support the various stages of program development:

compilation,

debugging,

maintaining libraries,

profiling,

and source code control.

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide3

 The C Languages

- K&R C

- ANSI C

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide4

 SINGLE-MODULE PROGRAMS

We will analyze a simple C program:

- performs a simple task: reversing a string.

- how to write, compile, link, and execute a program that

solves the problem using a single source file.

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide5

- source-code listing of the first version of

the “reverse” program (“reversefirst.c” on the Web)

/* REVERSE.C */

#include <stdio.h>

/* Function Prototype */

void reverse();

/*****************************************/

main()

{

char str[100]; /* Buffer to hold reversed string */

reverse(“cat”, str); /* Reverse the string “cat” */

printf(“reverse (“cat”)=%s\n”, str); /* Display Result */

reverse(“noon”, str); /* Reverse the string “noon” */

printf(“reverse (“noon”) = %s\n”, str); /* Display Result */

}

/****************************************/

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide6

void reverse(before, after)

char *before; /* A pointer to the source string */

char *after; /* A pointer to the reversed string */

{

int i;

int j;

int len;

len = strlen( before );

for( j=len-1; i=0; j>=0; j--; i++) /* Reverse loop */

after[i] = before[j];

after[len] = NULL; /* NULL terminate reversed string */

}

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide7

 Compiling a C Program

- To create and run the “reverse” program,

1) create a subdirectory called “reverse” inside your home

directory.

2) create the file “reversefirst.c” using the vi editor

3) compile the C program using the cc utility.

- By default, cc creates an executable file called “a.out”

in the current directory.

To run the program, type “./a.out”.

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide8

 Compiling a C Program ( Continue )

- Any errors that are encountered are sent to the standard

error channel,

which is connected by default to your terminal’s screen.

$ mkdir reverse

$ cd reverse

$ vi reversefirst.c

$ cc reversefirst.c

“reversefirst.c”, line 16: syntax error at or near variable name “cat”

“reversefirst.c”, line 18: syntax error at or near variable name “noon”

“reversefirst.c”, line 35: syntax error at or near symbol ;

“reversefirst.c”, line 35: syntax error at or near symbol )

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide9

 Compiling a C Program ( Continue )

- cc found a number of compile-time errors:

 The errors on line 16 and 18

due to inappropriate use of double quotes within

double quotes.

 The errors one line 35 were due to an illegal use of

a semicolon(;).

-

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide10

 A Listing of the Corrected “Reverse” Program

/* REVERSE.C */

#include <stdio.h>

/* Function Prototype */

void reverse();

/******************************************/

main()

{

char str[100]; /* Buffer to hold reversed string */

reverse(“cat”, str); /* Reverse the string “cat” */

printf(“reverse (\” cat \” ) = %s \n”, str); /* Display */

reverse(“noon”, str); /* Reverse the string “noon” */

printf(“reverse (\” noon \” ) = %s \n”, str); /* Display */

}

/*****************************************/

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide11

void reverse(before, after)

char *before; /* A pointer to the source string */

char *after; /* A pointer to the reversed string */

{

int i;

int j;

int len;

len = strlen(before);

for( j=len -1, i=0; j>=0; j--,j++) /* Reverse loop */

after[i] = before[j];

after[len]=NULL; /* NULL terminate reversed string */

)

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide12

 Running a C Program

- After compiling the second version of “reverse.c”,

by typing the name of the executable file, “./a.out”.

$ cc reverse.c

$ ls -l reverse.c a.out

-rwxr-xr-x 1 glass 24576 Jan5 16:16 a.out*

-rw-r--r-- 1 glass 439 Jan5 16:15 reverse.c

$ ./a.out

reverse (“cat”) = tac

reverse (“noon”) = noon

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide13

 Overriding the Default Executable Name

- to use the “-o” option with cc,

which allows you to specify the name of the executable file

that you wish to create:

$ cc reverse.c -o reverse

$ ls -l reverse

-rwx-xr-x 1 glass 24576 Jan 5 16:19 reverse*

$ ./reverse

reverse(“cat”) = tac

reverse(“noon”) = noon

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide14

 MULTIMODULE PROGRAMS

- The trouble with the “reverse” program,

the “reverse” function cannot easily be used in other programs.

- to write a function that

returns a value of 1 if a string is a palindrome

and a value of 0 otherwise.

- A palindrome

a string that reads the same forward and backward;

for example, “noon” is a palindrome, but “nono” is not.

- use the “reverse” function to implement our “palindrome” function.

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide15

 Reusable Functions

- A better strategy for sharing “reverse()”,

1) compile it separately,

2) and then link the resultant object module into whichever

programs wish to use it.

- this technique allows the function to be used in many different

programs.

Functions with this property are termed reusable functions.

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide16

 Preparing a Reusable Function

- To prepare a reusable function,

1) create a source-code module that contains the source code of

the function,

together with a header file that contains the function’s prototype.

2) compile the source-code module into an object module by

using the “-c” option of cc.

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide17

 Preparing a Reusable Function

- reverse.h

/* REVERSE.H */

void reverse(); /* Declare but do not define this function */

- reverse.c

/* REVERSE.C */

#include <stdio.h>

#include “reverse.h”

/********************************************/

void reverse( before, after )

char *before; /* A pointer to the original string */

char *after; /* A pointer to the reversed string */

{

int i;

int j;

int len;

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide18

len = strlen(before);

for( j=len-1, i=0; j>=0; j--, j++) /* Reverse loop */

after[i] = before[j];

after[len] = NULL; /* NULL terminate reversed string */

}

- main1.c

/* MAIN1.C */

#include <stdio.h>

#include “reverse.h” /* Contains the protype of reverse() */

/***********************************************/

main()

{

char str[100];

reverse(“cat”, str); /* Invoke external function */

printf(“reverse (\”cat\”) = %s\n”, str);

reverse(“noon”,str); /* Invoke external function */

printf(“reverse (\”noon\”) = %s\n”, str);

}

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide19

 Separately Compiling and Linking Modules

- To compile each source-code file separately,

use the “-c” option for cc.

creates a separate object module for each source-code file,

each with a “.o” suffix.

$ cc -c reverse.c

$ cc -c main1.c

$ ls -l reverse.o mail1.o

-rw-r--r-- 1 glass 311 Jan 5 18:24 main1.o

-rw-r--r-- 1 glass 181 Jan 5 18:08 reverse.o

$ -

- place all of the source-code files on one line.

$ cc -c reverse.c main1.c

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide20

 Separately Compiling and Linking Modules

- To link them all together into an executable called “main1”,

list the names of all of the object modules after the cc command:

$ cc reverse.o main1.o -o main1

$ ls -l main1

-rwxr-xr-x 1 glass 24576 Jan 5 18:25 main1*

$ ./main1

reverse (“cat”) = tac

reverse (“noon”) = noon

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide21

 The Stand-alone Loader: ld

- When cc is used to link several object modules,

it transparently invokes the UNIX stand-alone loader,

ld, to do the job.

- The loader is better known as the linker.

ld -n {-Lpath}* {objModule}* {library}* {-lx}* [-o outputFile]

ld links together the specified object and library modules to produce

an executable file.

-n : if you wish to create a stand-alone executable

-o : override the default name of the executable, “a.out”,

-lx : searches the standard directories “/lib”, “/usr/lib”,

and “/usr/local/lib” for a library with the name “libx.a”.

-Lpath : to insert the directory path into this search path.

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide22

 Separately Compiling and Linking Modules

- If you link a C program manually,

it’s important to specify the C runtime object module, “/lib/crt0.o”,

$ ld -n /lib/crt0.o mina1.o reverse.o -lc -o main1

$ main1

reverse (“cat”) = tac

reverse (“noon”) = noon

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide23

 Reusing the Reverse Function

- Here’s the header and source-code listing of

the “palindrome” function:

palindrome.h

/* PALINDROME.H */

int palindrome(); /* Declare but do not define */

palindrome.c

/* PALINDROME.C */

#include “palindrome.h”

#include “reverse.h”

#include <string.h>

/***********************************************/

int palindrome(str)

char *str;

{

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide24

char reverseStr[100];

reverse( str, reversedStr ); /* Reverse original */

return ( strcmp(str, reversedStr ) == 0 ); /* Compare the two */

}

- the program main2.c that tests “palindrome()”

/* MAIN2.C */

#include <stdio.h>

#include “palindrome.h”

/*************************************************/

main()

{

printf(“palindrome (\”cat\”) = %d \n”, palindrome(“cat”) );

printf(“palindrome (\”noon\”) = %d \n”, palindrome(“noon”) );

}

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide25

- To combine the “reverse”, “palindrome”, and “main2” modules

Compile the object modules and then link them.

$ cc -c palindrome.c

$ cc -c main2.c

$ cc reverse.o palindrome.o main2.o -o main2

-rwxr-xr-x 1 glass 24576 Jan 5 19:09 main2*

-rw-r--r-- 1 glass 306 Jan 5 19:00 main2.o

-rw-r--r-- 1 glass 189 Jan 5 18:59 palindrome.o

-rw-r--r-- 1 glass 181 Jan 5 18:08 reverse.o

$ main2

palindrome (“cat”) = 0

palindrome (“noon”) = 1

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide26

 Maintaining Multimodule Programs

- Several different issues must be considered when maintaining

multimodule systems:

Q1. What ensures that object modules and executables are kept

up to date?

Q2. What stores the object modules?

Q3. What tracks each version of source and header files?

A1. make, the UNIX file-dependency system

A2. ar, the UNIX archive system

A3. cvs, the Concurrent Version System

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide28

 THE UNIX PROFILER: PROF

- To see where a program is spending its time.

- If an amount of time that is greater than expected is being spent

in a particular function,

it might be worth optimizing the function by hand for

better performance.

Utility : prof -ln [ executableFile [profileFile] ]

prof is the standard UNIX profiler.

It generates a table of time and repetitions of each function in the

executable executableFile based on the performance trace stored

in the file profileFile.

if profileFile is omitted, the filename “mon.out” is assumed.

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide29

 THE UNIX PROFILER: PROF

- The executable file must have been compiled using the -p option of

cc , which instructs the compiler to generate special code that

writes a “mon.out” file when the program runs.

$ cc -p main2.c

$ main2---> execute the program.

palindrome (“cat”) = 0 ---> program output.

palindrome (“noon”) = 1

$ ls -l mon.out---> list the file for monitor output.

-rw-r-xr-x 1 gglass 1472 Jan 8 17:19 mon.out

$ prof main2 mon.out---> profile the program.

%Time Seconds Cumsecs #Calls msec/call Name

42.9 0.05 0.05 rdpcs

42.9 0.05 0.10 2002 0.025 reverse

14.3 0.02 0.12 2002 0.008 palindrome

0.0 0.00 0.12 1 0. main

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide30

 THE UNIX PROFILER: PROF

$ prof -l main2---> order the profile by name.

%Time Seconds Cumsecs #Calls msec/call Name

0.0 0.00 0.05 1 0. main

14.3 0.02 0.07 2002 0.008 palindrome

42.9 0.05 0.05 rdpcs

42.9 0.05 0.12 2002 0.025 reverse

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide31

 DOUBLE-CHECKING PROGRAMS: LINT

- checks your program more thoroughly than cc does:

Utility : lint { fileName }*

lint scans the specified source files and displays any potential errors

that it finds.

$ lint reverse.c---> check “reverse.c”.

reverse defined ( reverse.c(12) ), but never used

$ lint palindrome.c---> check “palindrome.c”.

palindrome defined ( palindrome.c ( 12 ) ), but never used

reverse used ( palindrome.c(14) ), but not defined

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide32

 DOUBLE-CHECKING PROGRAMS: LINT

$ lint main2.c---> check “main2.c”.

main2.c(11) : warning: main() returns random value to invocation

environment

printf returns value which is always ignored

palindrome used ( main2.c(9) ), but not defined

$ lint main2.c reverse.c palindrome.c---> check all modules together.

main2.c:

min2.c(11): warning: main() returns random value to invocation

environment

reverse.c:

palindrome.c:

Lint pass2:

printf returns value which is always ignored

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide33

 When you’re done: STRIP

- The debugger and profiler, utilities both require that you compile

a program using special options,

each of which adds code to the executable file.

remove this extra code after debugging and profiling are done with,

Utility: strip { fileName }+

strip removes all of the symbol table, relocation, debugging,

and profiling information from the named file(s).

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide34

 When you’re done: STRIP

- an example of how much space you can save by using strip:

$ ls -l main2---> look at original file.

-rwxr-xr-x 1 gglass 5904 Jan 8 22:18 main2*

$ strip main2---> strip out spurious information.

$ ls -l main2---> look at stripped version.

-rwxr-xr-x 1 gglass 3373 Jan 8 23:17 main2*

$ _

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

tracing system calls
Tracing System Calls
  • Most operating systems contain a utility to monitor system calls:
    • HPUX: tusc, MAC OS X: ktrace, Linux: strace, Solaris: truss, SGI: par

27mS[ 1] : close(0) OK

27mS[ 1] : open("try.in", O_RDONLY, 017777627464)

29mS[ 1] : END-open() = 0

29mS[ 1] : read(0, "1\n2\n|/bin/date\n3\n|/bin/sleep 2", 2048) = 31

29mS[ 1] : read(0, 0x7fff26ef, 2017) = 0

29mS[ 1] : getpagesize() = 16384

29mS[ 1] : brk(0x1001c000) OK

29mS[ 1] : time() = 1003207028

29mS[ 1] : fork()

31mS[ 1] : END-fork() = 1880277

41mS[ 1] (1864078): was sent signal SIGCLD

31mS[ 2] : waitsys(P_ALL, 0, 0x7fff2590, WTRAPPED|WEXITED, 0)

42mS[ 2] : END-waitsys(P_ALL, 0, {signo=SIGCLD, errno=0, code=CLD_EXITED, pid=1880277, status=0}, WTRAPPED|WEXITED, 0) = 0

42mS[ 2] : time() = 1003207028

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

slide36

 CHAPTER REVIEW

In this chapter, we learned how to:

 Compile C programs

 manage the compilation of multimodule programs

 maintain archives

 maintain multiple versions of source code

 profile executable files

 debug executable files

Prof. Andrzej (AJ) Bieszczad Email: [email protected] Phone: 818-677-4954

ad