Thus Spake Rama

A small attempt at blogging!! I will mostly be writing down funny incidents in my life and travels.

Sunday, November 22, 2009

Code aborting due to unhandled instruction error

The standard string class defined in string.h must not be used inside printf family of functions (eg fprintf, sprintf). If you do, the compiler will warn you as shown below. At runtime, the call aborts with a possible error message being "Program received signal SIGILL, Illegal instruction". Unless you look at the warnings, you wont know why the error is coming. This post is meant to help you save some trouble in such cases.

Code snippet from strings.C

Line 100: string str = "my string";
Line 101: fprintf (stderr, "%s :: No category implemented for this keyword\n", str);

$>g++ strings.C
strings.C:101: warning: cannot pass objects of non-POD type ‘const struct std::basic_string, std::allocator >’ through ‘...’; call will abort at runtime
strings.C:101: warning: format ‘%s’ expects type ‘char*’, but argument 3 has type ‘int’

Sure enough, When you run a.out, the execution aborts, but with an obscure error msg as shown below.

$>./a.out
Illegal instruction

If you run valgrind, you will get error message that would look like:

vex x86->IR: unhandled instruction bytes: 0xF 0xB 0x55 0x89
valgrind: Unrecognised instruction at address 0x405aea9.

Process terminating with default action of signal 4 (SIGILL)
Illegal opcode at address 0x405AEA9

Solution: The solution is to replace "str" as third argument of fprintf with str.c_str()!!
Compiling and running the code will give print the following o/p
my string :: No category implemented for this keyword.

Note that the compilation warning will not exist anymore.

Moral: If code aborts due to obscure error msg, look at your compiler warnings!

As usaul, any questions/clarifications, post them in comments section and I will answer them.

--rags

Labels: , , , ,

C++ and Lex/Yacc

This is some notes on lex and yacc. I think I have decent bit of knowledge in debugging linux, C/C++, lex/yacc and some related stuff, which I have accumulated over the years. I find myself telling people same stuff over and over. So this is an attempt to decrease amount of talking I do and yet share stuff with people.

For last three to four days, I have been playing with bison, bison++ and lex. The problem is like this.

I have two files, categories.lex, categories.yy. The categories.y file has some C++ code in it and my yyparse() will be called from C++ code in file test.C. If everything was in 'C' language, compilation would be straight forward. The problem is how do I compile each of these files, how do I call g++/gcc on the files so I can make a usable executable called "test". I have two bison's installed on my system. One is called "bison" which generates parser in 'C', another is called bison++ which generates parser in C++. See below for differences between bison and bison++

The solution is to export yyparse, yylex, yywrap, yyerror functions from *yy file using the phrase 'extern "C"'. Then .lex file which will be compiled with lex/gcc will get the function names in a unmangled manner.

So here are the commands I used and various error msgs I got during the process. just FYI, I have used all combinations of bison, bison++ and also tried to compile lex o/p with g++ as well as gcc. In the end, gcc for lex o/p succeeded.

$>lex -Pcategories categories.lex

This generated lex.categories.c file. Note that now yylex will be renamed to categorieslex because of the directive "-Pcategories"

$>gcc -c lex.categories.o
(note: Its GCC not G++)

$>bison -d -pcategories categories.yy

This generates file categories.tab.hh and categories.tab.cc. You must include categories.tab.hh in categories.lex. The main parse routine will now be categoriesparse() instead of yyprase (because of '-pcategories' directive).

$>g++ -c categories.tab.cc

Finally in test.C, add the following line
int categoriesparse();

You can all categoriesparse() from test.C just like anyother function.

$>g++ categories.tab.cc lex.categories.o test.o -o test

This should give you final output.

Using bison++ instead of bison in the command "bison -d -pcategories categories.yy" will generate two files categories.yy.tab.h and categories.yy.tab.c. A class of (default) name parse is created and yyparse is a member of this class. I donot really know how to use it with categories.lex file. Note that bison create a C++ file but doesnt create a class (typically named parse), where parsing routines are included. It generates non object oriented code. However, bison++ uses OOPS and creates a wrapper class for parsing.

Here are some errors I got and possible solutions to look for. Also, find my categories.lex and categories.y files attached(final working versions)

$>g++ -g -L/home/romit/java/contentanalyzer lex.categories.cc categories.tab.o test.C
lex.categories.o: In function `categorieslex':
lex.categories.c:(.text+0x227): undefined reference to `categorieslval'
categories.tab.o: In function `parse::parse()':
categories.tab.c:(.text+0x12): undefined reference to `vtable for parse'
categories.tab.o: In function `parse::parse()':
categories.tab.c:(.text+0x20): undefined reference to `vtable for parse'

Solution: You are using bison++ for categories.y. You may want to change it to bison. Also, you generated C++ code fromm categories.lex file. Generate 'C' code and use gcc to generate lex.categories.o file.

$>g++ -g -L/home/romit/java/contentanalyzer lex.categories.cc categories.tab.o test.C
categories.lex: In member function ‘virtual int categoriesFlexLexer::yylex()’:
categories.lex:11: error: ‘categorieslval’ was not declared in this scope
categories.lex:13: error: ‘STRING’ was not declared in this scope
categories.lex:17: error: ‘SEMICOLON’ was not declared in this scope
categories.lex:21: error: ‘COLON’ was not declared in this scope

You are using bison++ for categories.y and g++ for lex.categories.c
Instead use bison and gcc respectively. Explore generating lex code in 'c' instead of C++.

If you have any comments/clarifications, please post post them in comments sections and I will be happy to respond.

--Rags

Labels: , ,

Sunday, February 03, 2008

What it takes to be a super power??

Well.....This post is in response to Times of India repeatedly saying India is a super power.......

--- Your country's market will be big and apple of every entrepreneur across the world.

--You should be able to make your own military equipment and sell it to others if required.

--Your have a powerful army and an defence bases across the world.

--You have control important energy resources (read oil)

--- I would expect the education levels to be such that students across the world will want to study in your universities.

--Top quality people from across the world will be interested in working in your country.

--Your country dominates and dictates world politics.

--There wont be poverty, hunger in ur country....

-- your movies and art are mimicked across the world. They set a benchmark for others of their kind. Does Bollywood do that??

--Your country should do extra ordinarily well in world wide sporting events.

--Your country gives large amounts of aid, monetary and technological to other countries.

--Every one wants to invest in your countries' markets


Do we see that happening anytime soon w.r.t India?? Nice try when some news paper says we are about to become a super power.

--rags.

Tuesday, January 15, 2008

Places I want to visit in 2008.

So......I am in the middle of reading a blog post by rashmi bansal.
http://youthcurry.blogspot.com/2008/01/resolve-and-it-will-happen.html

Reading that post, suddenly I got an enthu list places i want to visit or things i want to do. The main idea in posting is
1) To list things at one place what all I want to do and therefore plan better.

2) To show friends and hopefully get them to come with me.

So here goes my list.

--- Kabini: I want to do coracling, rafting (if it is there and in general enjoy the quietness+greenery of the place. Target times: Jan/Feb/Monsoons/post monsoons. Time required: 1 weekend.

I visited kabini on Jan26th!!!! Not a great time to go.....Shud go back here during monsoons!

--Kerala: I want to take my OWN boat/raft and go into the backwaters of kerala (Alleppey?) I am really fond of someone else driving the boat and all I have to do is sit idle and stare at the water. Target time: Any time of the year. Time Required: 1 weekend or 3 days.

--Hampi: Heck this has been pending since ages. Target time: Any time other than summer. Time required: One day

--Srisailam: Its in Andhra Pradesh. Guess this has to wait till monsoons. Time Required: Two days.

--Bike trip to Goa/Pune: I know. Its crazy. But then, its a childhood dream. So I want to do it atleast once. Time Required : 3-4 days. Biking will be one way. Other way, bike can be shipped by train. Target Time: Before march or after september.

--Rafting in Rishikesh: I dont think south india has great rafting places. Hence I want to go to north india. Target time: Summer. Time Required: 1 Week(?). Should be clubbed with something else.

--Ladakh: Haa.....The cold desert. To be clubbed with the one above.

--Desert in Rajasthan: Needs no explanation. Target time: September to November.
--Guwahati: I want to see the brahmaputra river, hope to go to kaziranga national park and do some trek there. From there I want to go to north east. Time required: 5 days.

--Coastal Andhra pradesh: Believe me, East godavari district in Andhra Pradesh is like kerala....full of coconut groves. I want to do a car ride in this area. Of course, i want to do a (country )boat ride on godavari. Target time: Any time of the area. It can be clubbed with one of the festivals. Time Required: 2 days.

--Sindhudurg district of Maharashtra. I hope to do a trek there. I also want to see the the golden coloured grass on mumbai-nasik road. Target time: Dunno.

International:
==========
---Singapore:
Nothing to be said here. Target time: Any time of the year. Time required: 4 days
---Amsterdam: A friend of mine lives here. I want to visit him there. Heard its among the best places in europe.
--US: Disneyland, Universal studios, las vegas, niagara
--Brazil.
Just to see the amazon river.

.........and the list goes on. I will update it as and when I get new ideas.

--rags

Labels:

Wednesday, November 28, 2007

funny happening at humayun tomb...


I was in delhi on 25th Nov and had planned to visit humayun tomb.(HT) I was staying with a friend near H.nizamuddin station. I asked an autowala to take me to HT. he said the route is a long one and will cost more money. Instead of that, he offered to drop me on one side of nizamuddin station and advised me to cross the station and walk from there (which apparently was 5 min rastaa)......i diligently followed him......got down at the station, walked to the other side and then enquired how to get to HT. Ppl there pointed me to a gate in the station parking lot and asked me to jump over it. I cursed the auto wala and these ppl for making me do such stupid things. But as I had no choice, I did exactly as they said. The other side of the gate has a road adjacent to it. The road leads to a side entrance of HT. So I took the road and reached the said side entrance. There was no one to stop me there, so i happily entered HT, went around the place for 2 hours, enjoying the majestic monument. I congratulated myself that for the first time ever, i am gettting away without paying for tix(i know.....that bad)

At 11:00 am i decided to leave the place and walked towards the gate (not the one i entered thru but the one thru which everyone enters), all along smiling that i am not paying the money i needed to. To my utter disbelief I noted a turnstile (look at the pic if u dont know wat it is) at the exit gate. All smiles had disappeared. I thought this is god's way of asking me to pay up. I went to the security guy and said "Bhaiya, I lost my tix somewhere. Kyaa karoo? App mere liye naye tix lenge?" The fellow gave me a 2-min lecture on how careful i shud be and bought a tix for me(with my money that is). Just when I was about the enter the turnstile........i noted the others who were going thru it...and to my disappointment.....none of them had a tix....and the turnstile is not working the way it shud....u just had to push it and it rotated allowing everyone to go thru!!!!!!

life is so funny sometimes..........

camera@bangalore airport.

slightly longish....but please bear with me.........
==================================

aaaaa.....the thing is I was supposed to board a plane for delhi last tuesday (20th Nov). so i went to the airport, handed over my luggage at the check in counter and waited for security check. I had only my
camera as hand luggage.....When my turn for frisking came, I put my camera in the x-ray machine and went to the police wala so that he cud frisk me. To my horror, by the time my frisking was over, i found that the my camera was no where to be seen in the vicinity..........i discovered that some one stole it.......for twenty minutes, i searched the whole area trying to figure which stole it......the only ppl whom i cud suspect, given the circumstances were the security police stationed there. i tried to see if there was any place near by where they cud hide it....when i found a small desk where i saw something blue (colour of my camera pouch) i asked the security ppl to show me wat it is.....it turned out to be his cap!

so a couple of security guys came to my rescue and took me to the CC-tv section. There they told me to write a letter explaining the situation and asking them for permission to view the CC-tv footage and
x-ray machine footage. After this episode, they re-played the cc-tv to show how I entered and how i was frisked and I started searching for the camera and didnt find it. But it was not evident from the footage
as to wat happnd to the camera..didnt appear any where!! i was really pained at the way things were going. i was already pained that the flight got delayed by 3 hours......i started considering packing the
whole trip (which was planned for 5 days). somehow i managed to tell myself that these things happen and tht hopefully, i will forget it and enjoy the trip. when i asked their names, they started getting hostile, saying showing cc-tv is a big security threat....that they will loose their jobs if authorities know of this and all such crap. surprisingly, not one of them revealed their names.

i just lodged a rewritten complaint, cribbing about indian bureaucracy, abt the security agencies, police ppl and boarded the plane at 9:00pm (scheduled time-5:00pm) and landed in delhi at 11:30 pm. i also started feeling nostalgic abt my camera and how I shud I hav put fight to understand it better....i vowed not to buy another camera until i read up all books on digital photography...

the next day, i traveled from delhi to jaipur to kota, all the while cursing that i cudnt take pics of rajasthan.......i switched off my mobile till 12:00 in the afternoon.....when i switched on the phone at
12:00 noon, i got a phone from an unknown number. the caller said "sir, i am vinay, head of CISF in bangalore airport.....we hav traced your mobile camera and have been trying to contact you since 12
hours...please come and collect it".....yeyeye...hehehoho..gud heavens....i thought....i was over joyed and felt thankful to all those security ppl....i also thanked myself for not giving up the trip.

it seems some other passenger has somehow taken the camera along with his bag. after 2 hours (thank god...his flight is also delayed)....he realised it and returned it to the police!!!

yesterday, when i came back to bangalore, i went to the airport and collected my camera.
this, is why I dont have photos of a very memorable trip to the north.

--raghu

Labels:

Sunday, December 25, 2005

Gen Stuff

well...me too has a blog now!

uhhh...i am excited!! hope this enthusiam lasts atleast till tomorrow


actually, i want a place where i can write down my cribs on various aspects of life in indian cities.....about the pain of using systems and processes that one has to undergo in india. i hope to share my experiences abt others and discuss the same with others.


about "thus spake rama"....
in my childhood, we used to have a book in my home with jthe above title...it contained all the good stuff lord rama had to say to various ppl...as my name too is (raghu)rama, i have decided to adopt "thus spake rama" for my blog