Trace task (constructing a function graph)

 

Suppose we have some code (functions, classes, etc.)

There is a task - to create a call stack. But not just a linear one - as a tree .

Where the tree node will be the function entry and the branches will be the calls of the following functions from the current function node.

For example, there is such a structure of functions:

void Main() {  A(); B(); }

void A() { /*вошли в А*/ a(); b(); c(); }
void B() { /*Вошли в B*/ a(); b(); c(); }

void a() { /*Вошли в а */ }
void b() { /*Вошли в b */ }
void c() { /*Вошли в c */ }

The result will look like this


Obligatory condition:

You can add just one function to existing code functions to arrange tracing - right after "{".
Do not change in any way neither input parameters of source functions nor their results or code inside.

This condition is needed in order not to make large code modifications, but just to insert one and the same tracing function (for example let's name it _TRACE_IN_) into all existing code functions. To get all the passes of the source code and build the call tree.


I'd be glad to hear any ideas or variants.
No matter how I spin it, I come across a vicious circle, when I have to call not one but two functions to form such a tree. But I may need only one. :)

 

So, you want to make a program for detecting the algorithm of the source.

I used to do something similar for myself when I was breaking other people's programs in Asm, but I don't understand your approach...

why in the form of a tree?

or maybe I don't get it.

 
FLET:

I.e. you want to make a program for detecting the algorithm of the source.
I did something similar for myself when I was breaking other people's programs in Asm, but I don't understand your approach...
why as a tree?
or something I don't understand.


I wasn't thinking about "breaking". But you got it right. This is for building a logical code structure.

That is, my task - when I switch on a working code (my own or someone else's or, for example, from standard delivery of MT5) - in a separate window I get the entire stack of called functions as a tree. That is, at the root - function start() and from there it goes up and over....

I have implemented the tree long ago. It has analysis of time spent in functions (for analysis of code quality), number of calls to functions, tracing of variable values and so on. I also implemented three options of displaying this tree - as what is shown on the screenshot and as a circle and line chart. But I did it all for small things.

The main task I faced was filling the tree structure itself.

I need an idea how using one function (or perhaps several assignments, but the main thing is that they are in a row), added to the source code function, to build such a structure as on the screenshot.

 
sergeev:

I need an idea how to build a structure like the one on the screenwith just one function, added to the beginning of the code functions.

Proof of impossibility for an idea will do?
 
MetaDriver:
Proof of impossibility for an idea will do the trick?

will.... But only after 3 pages of discussion of options :))

complex implementations, extra variables, use of global, or whatever. accept all :)

 
sergeev:

will.... but only after 3 pages of discussion of options :))
Lan. Waiting for page four ;-)
 
sergeev:

it's worth it.... but only after 3 pages of discussion of variants :))


so the task came down to filling the topic with 3 pages of text? :), ok, let's do it ;)

1. work with strings: enter function, add function name, delete function exit, log/trace permanently outputs complete string as a linear list:

start(enter)-func1(enter)-func2(enter)

start(enter)-func1(enter)-func2(exit)

...

this approach, imho, is a waste of time

2. manually count the number of functions in the code and this number will be a numbering system, i.e. in a code of 10 functions = base 10, in a code of 3 functions = base 3, using Boolean algebra, or maybe easier using matrices, perform exclusion operations or include in the input/output of a function

roughly so, but then again, imho - is it worth the trouble? it's a lot of work to be done.

 

IgorM:

So the task was reduced to filling the topic with 3 pages of text? :), OK, here we go ;)

hey, not so hot!!! we're counting every page here :)

1. working with strings: enter a function, add a function name, exit the function, delete it,

it doesn't fit the condition. You only need one call - on entry. It's unrealistic on exit. There can be several exit points in a function. But there is only one input. Therefore, the "addition" must only be at the input.
 
sergeev:

hey hey, not so hot!!! we have every page counted here :)

too late, your wish was voiced by you ;)

on the point - they still need to work with matrices/multidimensional arrays - then there will be a mathematical model of your trace, and how you will handle input/output events from the function is another question

 
IgorM:

late, your wish was voiced by you ;)

You can always bury a branch. And this is not a wish.

for the point - I think you still need to work with matrices/multidimensional arrays - then there will be a mathematical model of your trace, and how you will handle input/output events from the function is another question

I don't understand it too well. Can you explain on your fingers how matrixes fit here?

The idea is to put the functions into a graph (a tree in the vernacular). But in a clear chronological order of calls and identification of what was called. One and the same function can be called both from start and from init. This is what we need to fix.

(I am developing it for/on MQL5, therefore I use classes for graph)

 
sergeev:

graph (tree in the vernacular).

A tree is a special case of a graph.
Reason: