-1

As usual, I have some code chain starting at the main method. Is there any elegant coding pattern, with which I could easily dump the outputs of all methods ever being called at runtime? I have one idea:

a) Copy the source code into an auxiliary aux:: namespace. For each method in aux:: run its copy of the code from the original:: namespace and after that dump its expected output).

Example pseudo-code:

// Source code at some repository
namespace original {
  int B(int input){
    output = input + 5;
    return output;
  }
  int A(int input){
    int output = methodB(input);
    output--;
    return output;
  }
}

// Create auxiliary repo
namespace aux{
  int B(int input){
    output = input + 5;
    // No other dependencies so only dump B output here
    dump(output);
    return output;
  }
  int A(int input){
    int output = methodB(input); // would call aux::methodB
    output--;
    // Dump output of A
    dump(output);
    return output;
  }
}

int main() {
  // Normally I would run original::A() but now I am dumping
  aux:A();
  return 0;
}

This would work, but I would need to replace source content everytime the code in original:: changes. I'd like to device something nicer like reusing the definitions in original:: without copying into aux:: but I am kind of stuck there.

Any help would be much appreciated!

  • Could you tag the question with the language you're using? There are tools to trace program execution, but they usually have a particular purpose such as profiling or debugging. What are you going to use this for? – Schwern Apr 14 '21 at 17:53
  • C++, I updated the tags. I want to export intermediate results during runtime with json or any other format into the same file for example. – tricostume Apr 14 '21 at 22:24
  • For what purpose? For example, if it's to find performance bottlenecks you'd use a [profiler](https://en.wikipedia.org/wiki/Profiling_(computer_programming)). – Schwern Apr 14 '21 at 23:16
  • No, the purpose is only that one for now, just generating a file that includes the outputs of all functions from a specific run. One can see it as a ground truth generation process against which the code will be tested afterwards – tricostume Apr 15 '21 at 07:44
  • If you mean "tested" as in "ensure it still works", a huge stack dump can only tell you its working in *exactly* the same way. That isn't particularly useful, why test the code unless you changed it? Instead, you want to [test that it gives the same results](https://stackoverflow.com/questions/242926/comparison-of-c-unit-test-frameworks). There is a thing called [Design By Contract](https://duckduckgo.com/?t=ffab&q=c%2B%2B+design+by+contract&ia=web) where you test your program's requirements within the code. Either way, dtrace will get you stack traces. – Schwern Apr 15 '21 at 18:59

2 Answers2

0

There are any number of existing tools to do this in C++ without modifying your code. Instead, tools can inject themselves into running code. Which you use depends on your purpose.

dtrace is a fairly generic tool to report on function calls in a running process. See Is it possible to probe the entry in a C++ class using Dtrace? for an example.

If your intent is to find a performance bottleneck, use a profiler. There are many out there, one is Orbit.

Finally, some IDEs will have this functionality built in. Eclipse, CLion, and Visual Studio all have tools to analyze your code while it runs.

Schwern
  • 153,029
  • 25
  • 195
  • 336
-1

You may use a global variable as a kind of flag. Incorporate this global flag in all of your functions to switch to the aux-mode.

Like so:

bool aux_switch = false;
//bool aux_switch = true;

namespace original
{
  int B(int input){
    output = input + 5;
    if(aux_switch) dump(output);
    return output;
  }
  int A(int input){
    int output = methodB(input);
    output--;
    if(aux_switch) dump(output);
    return output;
  }
}
paladin
  • 765
  • 6
  • 13
  • I would like to keep original:: and aux:: loosely coupled, I do not want to include dumping capabilities in the original source code – tricostume Apr 14 '21 at 22:38
  • Then just write a script which copies all your data and does the needed changes. You may use comments in your source code to tell your scripts some extra commands, like a custom preprocessor. BTW you should answer the question "What are you going to use this for?". – paladin Apr 14 '21 at 22:39