469,602 Members | 1,990 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

Need help building a software profiler.

78
Hi guys. i'm trying to build a software profiler in C++. What i want for now is to get a C code as input to my program, and produce a that code but added to it some stuff.
An example is getting input "x;" and putting output "x; stmt[0]++;"
This allows me to know how many times a statement in this code as been executed.
The strategy is to insert a stmt[i]++; anywhere i see a ';', but i'm afraid it won't work if i encounter a ';' found in the for loop parameters. And if i want to solve this and check if this statement ends with a new line, then place a "stmt[i]++", i'll be missing out on lines which include more than one statement such as "x; y;" (on the same line).

Can anyone suggest a possible solution? I've been thinking about it for hours, but i'm unable to solve this problem.

Thank you.
Apr 15 '07 #1
4 1281
Savage
1,764 Expert 1GB
Hi guys. i'm trying to build a software profiler in C++. What i want for now is to get a C code as input to my program, and produce a that code but added to it some stuff.
An example is getting input "x;" and putting output "x; stmt[0]++;"
This allows me to know how many times a statement in this code as been executed.
The strategy is to insert a stmt[i]++; anywhere i see a ';', but i'm afraid it won't work if i encounter a ';' found in the for loop parameters. And if i want to solve this and check if this statement ends with a new line, then place a "stmt[i]++", i'll be missing out on lines which include more than one statement such as "x; y;" (on the same line).

Can anyone suggest a possible solution? I've been thinking about it for hours, but i'm unable to solve this problem.

Thank you.
Hi,

if it contains more than one statement in one line then how about to search the line for:
Expand|Select|Wrap|Line Numbers
  1. for(
and if it contains it you will need to skip all ';' until you get to ;).
and then after every ';' add a stmt[i]++;

Savage
Apr 15 '07 #2
JosAH
11,448 Expert 8TB
Hi,

if it contains more than one statement in one line then how about to search the line for:
Expand|Select|Wrap|Line Numbers
  1. for(
and if it contains it you will need to skip all ';' until you get to ;).
and then after every ';' add a stmt[i]++;

Savage
At least you have to do that after preprocessing; have a look at this sick little puppy:
Expand|Select|Wrap|Line Numbers
  1. #define for(x) while(1)
  2. ...
  3. for (ever;and;ever)
  4. ...
Note that the preprocessor doesn't know about reserved words nor anything
at all about the C (or C++) language.

kind regards,

Jos
Apr 15 '07 #3
Sebouh
78
Hi,

if it contains more than one statement in one line then how about to search the line for:
Expand|Select|Wrap|Line Numbers
  1. for(
and if it contains it you will need to skip all ';' until you get to ;).
and then after every ';' add a stmt[i]++;

Savage
Yeh i think that would work.
Thanks mate. Now to deal with adding { for every 1 lined for loop.
Apr 15 '07 #4
JosAH
11,448 Expert 8TB
Yeh i think that would work.
Thanks mate. Now to deal with adding { for every 1 lined for loop.
Note my reply just above yours though ... If you want a bullet proof profiler better
start reading about coff and elf object file formats then which contain statement
and line information for compiled C (or C++) code. The compiler itself has done
its evil deeds and optimization tricks then already so it can't fool you anymore.

kind regards,

Jos
Apr 15 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by jw56578 | last post: by
22 posts views Thread by Rafia Tapia | last post: by
3 posts views Thread by Maansi Gupta | last post: by
5 posts views Thread by HotRod | last post: by
3 posts views Thread by Gordon | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.