2

I'm working on a project where I have to divide a C program into modules and find the time taken for the execution of each module. I have to use C or C++ to do this.

For example: the first module will be main() then inside you may have other modules like for, or while or if etc.

It's actually like generating a syntax tree for the program, in a textual form.

So what I'm looking for is a way to do this, or any framework which can assist me with this, or any suggestions, anything would be great!

Thanks in advance :)

EDIT:

and ok, il make it more clear, by giving the sample input and the expected output as mentioned by our institute,

Input:

  void main() { 
       int h=2,j=5,k=1;
       while (h>j) {
          h++; 
          for (k=100;k>0;k/=2) { 
             if (i<=5) {
                d=n*10;
                p=n/10;
             } 
          }
        }
   }

Output:

  main 
  begin
  h=2
  j=5
  k=1
  *************
  while 
  h>j 
  0.2087
  ************* 
  begin
  h++
  *************
  for 
  k=100 
  k>0 
  k/2
  0.8956
  ************* 
  begin
  if i<=5
  *************
  begin
  d=n*10
  p=n/10
  0.1153
  end
  *************
  end
  end

sorry for the output, its vertical, i think it gives every1 a clear idea ! :) –

11
  • 1
    what is your definition of module? Shall this work on source or binary or both? What timeframe do you have for a solution? Commented Mar 31, 2011 at 9:11
  • 2
    I believe you're looking for a profiler. Commented Mar 31, 2011 at 9:13
  • @tobias and @Eugen , thanks for the reply :) and the input wil be a sample C source file, this is for my final semester project and so the input file is assumed to hav less than 100 lines of code!! Commented Mar 31, 2011 at 16:49
  • 1
    I think OP wants to write his own profiler. Commented Mar 31, 2011 at 20:09
  • ... I transcribed (see EDIT) Hari's comment on expected output into his question since he's a newbie. If he is to be beleived, he doesn't want a profiler, he wants an execution trace. oowever, I don't know what the floating point number in the starred lines represents (maybe that's runtime profile data?) Commented Apr 3, 2011 at 9:05

2 Answers 2

1

Being less than 100 lines doesn't help you much; people can write a program that uses every feature of C language (and the compiler extensions!) in a program that size. So you are saying you have to deal with the entire C language (statements, expressions, functions, structures, typedefs, macros, preprocessor conditionals, ...). If this is a school project, I think that's too hard; you will need to limit your project to a interesting subset, say, functions, assignments, while statements and function calls.

What you need to do this straightforwardly is

  • A parser for the C language
  • The ability to climb over the AST
  • The ability to patch the AST
  • The ability to emit the AST as source text

Program transformations system are nearly ideal engines to accomplish this kind of work. These allow you to additionally do pattern-directed changes to the AST, which makes this much easier to implement.

You can probably do this with TXL or Stratego; I'm sure both have C parsers that will handle the limited subset of C that you should be willing to do.

You could do with this our DMS Software Reengineering Toolkit, although this is probably not suited for student use in short period of time. (TXL and Stratego are free, but might have steep learning curves, too).

But you will find this document on instrumenting code to build test coverage tools probably an ideal introduction to what you need to do to accomplish your task with a transformation system.

EDIT: I transcribed Hari's comment with an example into his question.

I think he wants a tracer not a profiler.

He can do that using instrumentation, but he'll have a lot of instrumentation to insert because it looks like the goal is tracing every action.

In that case, it would be easier if he had an interpreter, but he needs one that keeps the code structure around so that he can report the code structure as it runs (so CINT which compiles to a pcode wouldn't be the right answer).

What he needs is full C parsing to AST with symbol tables, an interpreter that executes the ASTs, and a prettyprinter so that he can convert appropriate parts of the AST he is currently executing back into text to report as progress.

DMS is still a very good foundation for this, as it has all the machinery needed to support this task too. Parse the C text, build the symbol table (all built into DMS for C) and then run a custom interpreter. This is easily coded as an interpreter over the AST; a big case statement over the tree node types would be the interpreter core, with node-type specific cases carrying out the action implied by the AST node (including climbing up or down the tree, updating symbol table entries with new values, computing expression intermediate results, and of course reporting progress.

ANTLR might work; it has a C parser but I'm not so sure about a prettyprinter. The interpreter would work pretty much the same way.

TXL and Stratego would likely be awkward for this, as it isn't clear how you'd use their pure-transformational style to alternatate interpreting and printing trace data.

Sign up to request clarification or add additional context in comments.

6 Comments

For examples of short, complex code one might look at codegolf.SE (and the [code-golf] tag on Stack Overflow), other code golfing sites, or ioccc.
@ira thank u, will check that out... and ya the subset which u mentioned is actually enough.. Will try those tools and write back..
and ok, il make it more clear, by giving the sample input and the expected output as mentioned by our institute, Input: void main() { int h=2,j=5,k=1; while (h>j) { h++; for (k=100;k>0;k/=2) { if (i<=5) { d=n*10; p=n/10; } } } } Output: main begin h=2 j=5 k=1 ***************** while h>j 0.2087 ************** begin h++ ************* for k=100 k>0 k/2 0.8956 ************* begin if i<=5 ************* begin d=n*10 p=n/ 0.1153 end ************* end end
sorry for the output, its vertical, i think it gives every1 a clear idea ! :)
@Hari: you should put additional information about the question, into your original question. You should be able to do that using the EDIT hyperlink directly under your question. I did it for you this time (maybe incorrectly?); yours to do in the future.
|
0

What you're describing is a profiler. Which one is suitable very much depends on the platform which you're using.

For Window and Visual C++ I've found LTProf to be lightweight, inexpensive and usable http://www.lw-tech.com/index.php?option=com_content&task=view&id=25&Itemid=65. Alternatively the higher end Visual Studio products include a profiler and Intel sell various profilers.

Alternatively are you trying to write your own profiler? That's quite a tricky piece of code to write and very platform dependent. You may find open source examples to use as starting points.

EDIT: NB: Using ltprof may require you to turn off compiler optimization to get reliable profiling results. Be aware that this can potentially change the results.

7 Comments

@persiflage , thanks a lot, yes this is almost what i want, but ya i want to design such an application on my own, but the advantage is, the input prog is a C rog less than 100 lines of code without recursion! finally no GUI required a cmd line tool is enough.. Suggest if u know something like this, but anyways thanks a lot again, i din kno its called a profiler, so was struggling to search for it !!
That's certainly very difficult for a school project.
Perhaps you could extend a C interpreter to give amount of interpreter 'time' spent in each area of the AST. There's an article here from an old book I used to have on writing an small C interpreter drdobbs.com/cpp/184408184 - it's an interesting read and will get you started thinking about the problem. There's also a working interpreter code.google.com/p/picoc which is 4000 lines for interpreting a subset of C. There are quite a few C interpreters out there on the web.
@persiflage , thanks again, wil check that out and comment back, and its a UG level project :)
Profiling with optimizations off is, in general, completely useless, as the profile without optimization may be completely different from the profile with optimization, and trying to manually optimize your program based on that un-optimized profile will often end up making things run slower.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.