Need help building a software profiler.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebouh
    New Member
    • Feb 2007
    • 77

    Need help building a software profiler.

    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.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by Sebouh
    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:
    Code:
    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

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Savage
      Hi,

      if it contains more than one statement in one line then how about to search the line for:
      Code:
      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:
      Code:
      #define for(x) while(1)
      ...
      for (ever;and;ever)
      ...
      Note that the preprocessor doesn't know about reserved words nor anything
      at all about the C (or C++) language.

      kind regards,

      Jos

      Comment

      • Sebouh
        New Member
        • Feb 2007
        • 77

        #4
        Originally posted by Savage
        Hi,

        if it contains more than one statement in one line then how about to search the line for:
        Code:
        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.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Sebouh
          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

          Comment

          Working...