Sunday, January 24, 2010

Detect 'if' statement

Aim:
1)To compile gcc version 4.4.2,
2)To detect simple 'if-else' statements after reading source file c-parser.c of gcc.
3)Generate exception to handle 'if detection'.
4)Use git commands to point out difference made in newer compiled gcc from existing gcc on system.
To Detect 'if' Statement.

1)WRITE a simple helloworld.c code.

// helloworld.c file contents
#include
FILE *fp;
fp=fopen("/home/swati/temp.txt","w");
fprintf(fp,"\nthis is if detection\n");


2) INCLUDE helloworld.h FILE IN THE SOURCE DIRECTORY
/newgcc/gcc-4.4.2/gcc/.
2.1) Use #include "helloworld.h" in c-parser.c file in this directory.

3) RECOMPILE USING FOLLOWING COMMANDS.
3.1) Change the current directory to 'build' directory.

[swati@localhost ~]$ cd /home/swati/newgcc/objdir/build-i686-pc-linux-gnu/gcc/build/

3.2) Configure with --disable-bootstrap modification.

[swati@localhost build]$ /home/swati/newgcc/gcc-4.4.2/configure --disable-bootstrap--prefix=$insdir

OUTPUT on successful configuration looks like:
configure: creating ./config.status
config.status: creating Makefile

3.3) 'Make' command in BUILD directory will re-make all necessary changes.(.o files created from .c files)

[swati@localhost build]$ make all-gcc


3.4) Select newly configured gcc.

[swati@localhost build]$ export PATH=$insdir/bin/:$PATH

2.5) Compile.

[swati@localhost build]$ gcc /home/swati/hello.c -v
[swati@localhost build]$ ./a.out

OUTPUT on Console:
hello world

OUTPUT of included helloworld.h file:
A new text file temp.txt created in home folder with a newly added line 'this is if detection'.


Hence, 'if' statement is successfully detected ,source code modified and recompiled to reflect the manipulations.
Now, we can proceed to exception generation.




Wednesday, January 13, 2010

Exception Generation and Handling

To generate exception, i used following 2 functions contained in header file setjmp.h:

1) setjmp()

2) longjmp()

These two routines require a datatype defined in setjmp.h:

3) jmp_buf

i) It specifies the buffer used by the routines to save and restore the program environment.
ii) The jmp_buf type is defined as: typedef char jmp_buf[_JBLEN];
iii) Example: jmp_buf env; where env- Variable in which environment is stored.

Description of 2 routines used:

1)int setjmp(env)

i)Sets up the local jmp_buf buffer and initializes it for the jump.
ii)Saves the program's calling environment in the environment buffer for later use by longjmp.
iii)If the return is from a direct invocation, setjmp returns 0.
iv)If the return is from a call to longjmp, setjmp returns a nonzero value.

2)longjmp(env,1)

i)Restores stack environment and execution locale previously saved in env by setjmp.


Generation of exception can follow the construct:

#include

jmp_buf env; <-----jump environment (must be global)
bit error_flag;

void trigger (void)
{
. . .
/* processing code here */
/* . . .
if (error_flag != 0)
{
longjmp (env, 1); <----return 1 to setjmp
}
. . .
}

Here is a simple program to generate and handle exception using setjmp() and longjmp() functions using above construct.

#include
#include

static jmp_buf buf;

void exception_handler()
{
printf("'if'detected\n"); <-----prints
longjmp(buf,1); <-----jumps back to where setjmp was called - making setjmp return 1
}

int main()
{
if (!setjmp(buf) )
{
exception_handler(); <-----when executed, setjmp returns 0
}
else
{ <-----when longjmp jumps back, setjmp returns 1
printf("Back to main block"); <-----prints
}

return 0;
}

OUTPUT is as follows:

[swati@localhost ~]$ gcc exc.c

[swati@localhost ~]$ ./a.out
'if'detected
Back to main block



Saturday, January 2, 2010

Installing gcc

Aim:
1)To compile gcc version 4.4.2,
2)To detect simple 'if-else' statements after reading source file c-parser.c of gcc.
3)Generate exception to handle 'if detection'.
4)Use git commands to point out difference made in newer compiled gcc from existing gcc on system.


A) Prerequisite: MPFR Library

1) DOWNLOAD MPFR tar file.
It can be downloaded from http://www.mpfr.org/.

2) EXTRACT it in home.
2.1) tar -xvf MPFR-4.2.tar.gz

3) CONFIGURE it and install.
3.1)cd mpfr-4.2
3.2) ./configure
3.3) make
3.4) make install (in root)

B)To compile/install gcc

1) DOWNLOAD gcc-4.4.2 tar ball file.
It can be downloaded from ftp://gd.tuwien.ac.at/gnu/gcc/releases/

2) EXTRACT gcc-4.4.2 from tar file.
2.1) mkdir gcc1
2.2) cd gcc1
2.3) tar -xvf gcc-4.4.2.tar.gz
2.4) set srcdir = "/home/swati/gcc1/gcc-4.4.2"
set objdir = "/home/swati/gcc1/gcc-bin"
set insdir = "/home/swati/gcc1//usr/local"
2.5) mkdir -p srcdir
mkdir -p objdir
mkdir -p insdir
2.6) cd objdir

2.7)CONFIGURE : /home/swati/gcc1/gcc-4.4.2/configure --prefix=$insdir
2.8) make bootstrap -j3
2.9) make install (in root)
2.10) cd gcc1
2.11) SELECT NEW INSTALLED gcc: export PATH=$insdir/bin/:$PATH

3) COMPILE this new version gcc1
3.1) gcc hello.c -v

References:

http://www.faqs.org/docs/ldev/0130091154_78.htm
http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/install_gcc.html


Friday, January 1, 2010

Use of git commands

Aim:
1)To compile gcc version 4.5.0,
2)To detect simple 'if-else' statements after reading source code of gcc.
3)Generate exception to handle 'if'.
4)Use git commands to point out difference made in newer compiled gcc from existing gcc on system.

3)Use of git commands using test.c file

[swati@localhost ~]$ mkdir repo

[swati@localhost ~]$ cd repo


[swati@localhost repo]$ cp /home/swati/test.c /home/swati/repo/

[swati@localhost repo]$ ls -l
total 4
-rw-rw-r--. 1 swati swati 61 2010-01-10 19:59 test.c

cp: Copy the file from home to the repository

[swati@localhost repo]$ git init

Initialized empty Git repository in /home/swati/repo/.git/

init: To initiliaze or re-initialize git repository

[swati@localhost repo]$ git add test.c

add: To add a new file to the index.

[swati@localhost repo]$ vi test.c
[swati@localhost repo]$ git commit
[master (root-commit) 9bb9f1e] make changes in test.c
1 files changed, 7 insertions(+), 0 deletions(-)
create mode 100644 test.c

commit:To store the current changes made to the file along with log messages.

[swati@localhost repo]$ git log

commit 9bb9f1e86dab976d215e38b65236be7d750908ff
Author: swatiraina
Date: Sun Jan 10 20:00:57 2010 +0530

make changes in test.c

added if stmt to test.c

log: To show the commit logs

[swati@localhost repo]$ git show

commit 9bb9f1e86dab976d215e38b65236be7d750908ff
Author: swatiraina
Date: Sun Jan 10 20:00:57 2010 +0530

make changes in test.c

added if stmt to test.c

diff --git a/test.c b/test.c
new file mode 100644
index 0000000..939db90
--- /dev/null // expected ---a/test.c
+++ b/test.c
@@ -0,0 +1,7 @@
+
+#include
+void main()
+{
+int a;
+printf("\nlinux");
+}

show: To display the differences between the original and new file

[swati@localhost repo]$ git diff /home/swati/test.c test.c

diff --git a/home/swati/test.c b/test.c
index 939db90..e7d7b33 100644
--- a/home/swati/test.c
+++ b/test.c
@@ -2,6 +2,7 @@
#include
void main()
{
-int a; //'-' to indicate deleted statements
+int a=10; //'+' to indicate committed statements
+if(a>10)
printf("\nlinux");
}