Recent

Author Topic: Call graph  (Read 969 times)

julkas

  • Sr. Member
  • ****
  • Posts: 382
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Call graph
« on: September 21, 2019, 01:45:05 pm »
How generate Call Graph - https://en.wikipedia.org/wiki/Call_graph and Dependency Graph - https://en.wikipedia.org/wiki/Dependency_graph in Lazarus?
« Last Edit: September 21, 2019, 02:01:29 pm by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

simone

  • Full Member
  • ***
  • Posts: 245
Re: Call graph
« Reply #1 on: September 21, 2019, 02:03:48 pm »
I'm not sure to undestand your need. If you want to build a program with Lazarus that is able to generate flow charts and similar diagrams, as call graphs, I suggest to you the TEvsSimpleGraph component, available from https://github.com/taazz/EvsSimpleGraph or via Online Package Manager. It is very flexible and mature.

If you are searching for a tool that generates a static/run time call graph for a Lazarus program, I don't know if such a tool exists.


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5628
    • wiki
Re: Call graph
« Reply #3 on: September 21, 2019, 02:31:29 pm »
The IDE has a unit dependency graph: Menu View > Unit dependencies
(and a package graph)

That is as far as it currently gets.

As for callgraph...
If you are on linux you can get a runtime call graph with kcachegrid and valgrind.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 821
Re: Call graph
« Reply #4 on: September 21, 2019, 02:54:51 pm »
I'm not sure to understand your need. If you want to build a program with Lazarus that is able to generate flow charts and similar diagrams, as call graphs, I suggest to you the TEvsSimpleGraph component, available from https://github.com/taazz/EvsSimpleGraph or via Online Package Manager. It is very flexible and mature.

If you are searching for a tool that generates a static/run time call graph for a Lazarus program, I don't know if such a tool exists.

https://forum.lazarus.freepascal.org/index.php/topic,38983.0.html
https://forum.lazarus.freepascal.org/index.php/topic,34040.0.html

The IDE has a unit dependency graph: Menu View > Unit dependencies
(and a package graph)

That is as far as it currently gets.

As for callgraph...
If you are on linux you can get a runtime call graph with kcachegrid and valgrind.
Interesting.

julkas

  • Sr. Member
  • ****
  • Posts: 382
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: Call graph
« Reply #5 on: September 21, 2019, 02:55:02 pm »
As for callgraph...
If you are on linux you can get a runtime call graph with kcachegrid and valgrind.
I am on Windows. I need only compile time functions/procedures calls(deps).
Example -
Code: Pascal  [Select]
  1. unit u1;
  2.  
  3. function fun1();
  4. begin
  5. ...
  6.    x := fun2();
  7.    ...
  8.    y := fun3();
  9.    ...
  10. end;
  11.  
  12. function fun4();
  13. begin
  14. ...
  15.    z := fun2();
  16.    ...
  17.    w := fun3();
  18.    ...
  19. end;
  20.  
  21. function fun2();
  22. begin
  23.    ....
  24. end;
  25.  
  26. function fun3();
  27. begin
  28.    ....
  29. end;
  30.  
  31.  
Call graph for unit u1:
Code: Text  [Select]
  1. fun1
  2.    |
  3.    |---fun2
  4.    |---fun3
  5.  
  6. fun4
  7.    |
  8.    |---fun2
  9.    |---fun3
  10.  
  11. fun2
  12.  
  13. fun3
  14.  
I want XML, JSON or simple plain text repr. as above.
« Last Edit: September 21, 2019, 03:10:59 pm by julkas »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5628
    • wiki
Re: Call graph
« Reply #6 on: September 21, 2019, 03:27:14 pm »
A few others. Not tested any of them... And they might be more for class structures....

https://sourceforge.net/projects/essmodelforlaza/
https://github.com/JuhaManninen/Laz-Model

Not a graph, but in the IDE you have: "Find Identifier references" (somewhere in the menu / also in the context menu)
It can find places where a function is called.
With virtual/overriden methods it may not find all....

simone

  • Full Member
  • ***
  • Posts: 245
Re: Call graph
« Reply #7 on: September 21, 2019, 04:05:04 pm »
As far as I know, none of the proposed tools is conceived to generate call graphs, i.e. graph that shows execution control flow. Pudgdb examines units dependencies. LazProfile is a performance profiler. EssModel and LazModel generates class diagram in UML style.
« Last Edit: September 21, 2019, 06:38:03 pm by simone »

zamtmn

  • Sr. Member
  • ****
  • Posts: 365
Re: Call graph
« Reply #8 on: September 21, 2019, 04:24:09 pm »
LazProfiler has information about runtime procedure calls. It only needs to be taken out
« Last Edit: September 21, 2019, 04:27:48 pm by zamtmn »

simone

  • Full Member
  • ***
  • Posts: 245
Re: Call graph
« Reply #9 on: September 21, 2019, 07:22:23 pm »
I did not know PudGdb and I want to try it, but when I open the project, the IDE fails to download following packages: ag_graph, ag_vector, zobjectinspector. Where can I find them?

zamtmn

  • Sr. Member
  • ****
  • Posts: 365

440bx

  • Hero Member
  • *****
  • Posts: 1123
Re: Call graph
« Reply #11 on: September 21, 2019, 11:30:02 pm »
How generate Call Graph and Dependency Graph
From the answers you've received above and, from my limited knowledge of Lazarus, I don't think Lazarus generates a procedure/function call graph for a program.

If you are familiar with AWK, it is fairly simple to generate the dependency sets from each function/procedure.  From the dependency sets it's trivial to generate a call graph and a dependency graph. 

There is one thing to be careful about, a topological sort is used to generate an ordered dependency graph, choose an algorithm that detects circular dependencies (most do) and handles them "gracefully" (some just stop, others can represent the cycle and continue sorting - the latter is obviously the kind of algorithm you want.)

Just FYI, way way back, TurboPower software had a product called Turbo Analyst which produced call graphs.  I don't know if that is one of the products they open sourced after they closed shop.  If it is available somewhere, it's likely it would need "improvements" to handle the FPC dialect but, for old Turbo Pascal code (circa TP6 and 7), it's likely to work without any changes.


using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

julkas

  • Sr. Member
  • ****
  • Posts: 382
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: Call graph
« Reply #12 on: September 22, 2019, 10:49:16 am »
Thanks all for replies.
I want simple and powerful static code analysis feature -
Quote
A call graph (also known as a call multigraph) is a control flow graph, which represents calling relationships between subroutines in a computer program. Each node represents a procedure and each edge (f, g) indicates that procedure f calls procedure g. Thus, a cycle in the graph indicates recursive procedure calls.
My code is not over complicated, so I will proceed manually.
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

Thaddy

  • Hero Member
  • *****
  • Posts: 8880
Re: Call graph
« Reply #13 on: September 22, 2019, 12:08:02 pm »
Note LazModel/EssModel works even on a Raspberry Pi with some slight adjustments:
The QueryInterface/_AddRef/_Release need to be explicitly cdecl on Linux and QueyInterface must be adapted to take constref instead of const for recent FPC versions.
Maybe Juha reads this?
Most people that want to use threading should learn to patch their jeans first: use a needle.

julkas

  • Sr. Member
  • ****
  • Posts: 382
  • KISS principle / Lazarus 2.0.0 / FPC 3.0.4
Re: Call graph
« Reply #14 on: September 24, 2019, 06:04:17 pm »
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;