Application that shows what functions are referenced by in C [duplicate] - c

This question already has answers here:
Tools to get a pictorial function call graph of code [closed]
(7 answers)
Closed 8 years ago.
I would really like to know if there are any methods or applications which can show me which functions are referencing which functions. So say I would like to see from where a function change_state() is called/referenced, I get something like:
/--app_init()<--main()
|
change_state() <--|
|
\--afile.c |<--trigger()
/ line 100|
| line 156|
|
\--bfile.c|
line 26|<--|--button_event()<--process_event()
line 30| |
\--move_event()
EDIT: I am using the Keil compiler in Windows 7.

You can use gperf for that type of use.
It will show you a call graph, which is basically what you want, including performance mesurement.
First, assuming you're using gcc, compile with the option -gp.
Then, run your binary normally. It will output a gmon.out file. You can then use gperf to analyse that file, which contains the data you're requesting.

Related

how to compile a program with a given file.a [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 1 year ago.
I did a program that need some functions from a file that the staff gives us.
the file is libmap.a.
I developed some modules that use functions from the libmap file.
how can I compile it?
Some friends said me that I need to change something to the Cmake file.
is someone know what I need to change?
You do not compile, only link with the library file.
for gcc you need to use -l option in your case -lmap and if needed set the path using -L option.
in Cmake you need to set your library search path:
LINK_DIRECTORIES(your_target "directory")
and the add the library
TARGET_LINK_LIBRARIES(your_target map)

How to access all the files before compilation? [duplicate]

This question already has an answer here:
How do I save preprocessor output using the Dev-C++ IDE?
(1 answer)
Closed 2 years ago.
There are several steps involved from the stage of writing a C program to the stage of getting it executed.
when I compile the code I only get an .exe file. But I want to get all the files which are being made before the compilation (preprocess ones), an intermediate code file
where all those macros with are replaced with their actual values and preprocessor are replaced with their actual header files.
in general can we get all those files (preprocess one, compile one and linker one) separately?
To access all the intermediate files use the command (Ubuntu):
gcc –Wall –save-temps filename.c –o filename. This command generates all the intermediate files in current working directory.

How can I fix my problem with colored output in C [duplicate]

This question already has answers here:
How to make win32 console recognize ANSI/VT100 escape sequences?
(14 answers)
Closed 2 years ago.
I want to print a colored sentence,
My code:
#include<stdio.h>
int main()
{
printf("\033[0;34m");
printf("This is Blue");
return 0;
}
And this is output:
[0;34mThis is Blue
How can I fix this problem?
Your program does exactly what it is supposed to do: output what you tell it to to the console.
However, if you are expecting to see colors when sending ANSI/VT100 codes to the console the console must understand these codes.
You didn't mention what operating system and console you are using.
If it's Windows for example the console doesn't support ANSI/VT100 codes by default (to enable see: https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences?redirectedfrom=MSDN).
If it Unix or Linux you may need to check what the TERM environment variable is set to and if that is supported by the console application you're using.
If you are using a remote console (like PuTTY on Windows) you may need to set the terminal type to xterm-color.

What are .o files and to open them on windows? [duplicate]

This question already has answers here:
What's an object file in C?
(5 answers)
Closed 3 years ago.
I'm a beginner in programming, I wonder what is inside the .o files and want to see it, but can't open the files in windows because they give some output with unrecognized symbols. Please suggest something !
They are object files, produced by the compiler, which the linker will combine into an executable.
They are not intended to be human readable.

Tools to get a pictorial function call graph of code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I have a large work space which has many source files of C code. Although I can see the functions called from a function in MS VS2005 using the Object browser, and in MSVC 6.0 also, this only shows functions called from a particular function in a non-graphical kind of display. Additionally, it does not show the function called starting from say main(), and then the functions called from it, and so on, deeper inside to the leaf level function.
I need a tool which will give me a function call graph pictorially with functions callee and caller connected by arrows or something like that, starting from main() to the last level of function, or at least showing a call graph of all functions in one C source file pictorially. It would be great if I could print this graph.
Any good tools to do that (need not be free tools)?
Egypt (free software)
ncc
KcacheGrind (GPL)
Graphviz (CPL)
CodeViz (GPL)
Dynamic analysis methods
Here I describe a few dynamic analysis methods.
Dynamic methods actually run the program to determine the call graph.
The opposite of dynamic methods are static methods, which try to determine it from the source alone without running the program.
Advantages of dynamic methods:
catches function pointers and virtual C++ calls. These are present in large numbers in any non-trivial software.
Disadvantages of dynamic methods:
you have to run the program, which might be slow, or require a setup that you don't have, e.g. cross-compilation
only functions that were actually called will show. E.g., some functions could be called or not depending on the command line arguments.
KcacheGrind
https://kcachegrind.github.io/html/Home.html
Test program:
int f2(int i) { return i + 2; }
int f1(int i) { return f2(2) + i + 1; }
int f0(int i) { return f1(1) + f2(2); }
int pointed(int i) { return i; }
int not_called(int i) { return 0; }
int main(int argc, char **argv) {
int (*f)(int);
f0(1);
f1(1);
f = pointed;
if (argc == 1)
f(1);
if (argc == 2)
not_called(1);
return 0;
}
Usage:
sudo apt-get install -y kcachegrind valgrind
# Compile the program as usual, no special flags.
gcc -ggdb3 -O0 -o main -std=c99 main.c
# Generate a callgrind.out.<PID> file.
valgrind --tool=callgrind ./main
# Open a GUI tool to visualize callgrind data.
kcachegrind callgrind.out.1234
You are now left inside an awesome GUI program that contains a lot of interesting performance data.
On the bottom right, select the "Call graph" tab. This shows an interactive call graph that correlates to performance metrics in other windows as you click the functions.
To export the graph, right click it and select "Export Graph". The exported PNG looks like this:
From that we can see that:
the root node is _start, which is the actual ELF entry point, and contains glibc initialization boilerplate
f0, f1 and f2 are called as expected from one another
pointed is also shown, even though we called it with a function pointer. It might not have been called if we had passed a command line argument.
not_called is not shown because it didn't get called in the run, because we didn't pass an extra command line argument.
The cool thing about valgrind is that it does not require any special compilation options.
Therefore, you could use it even if you don't have the source code, only the executable.
valgrind manages to do that by running your code through a lightweight "virtual machine". This also makes execution extremely slow compared to native execution.
As can be seen on the graph, timing information about each function call is also obtained, and this can be used to profile the program, which is likely the original use case of this setup, not just to see call graphs: How can I profile C++ code running on Linux?
Tested on Ubuntu 18.04.
gcc -finstrument-functions + etrace
https://github.com/elcritch/etrace
-finstrument-functions adds callbacks, etrace parses the ELF file and implements all callbacks.
I couldn't get it working however unfortunately: Why doesn't `-finstrument-functions` work for me?
Claimed output is of format:
\-- main
| \-- Crumble_make_apple_crumble
| | \-- Crumble_buy_stuff
| | | \-- Crumble_buy
| | | \-- Crumble_buy
| | | \-- Crumble_buy
| | | \-- Crumble_buy
| | | \-- Crumble_buy
| | \-- Crumble_prepare_apples
| | | \-- Crumble_skin_and_dice
| | \-- Crumble_mix
| | \-- Crumble_finalize
| | | \-- Crumble_put
| | | \-- Crumble_put
| | \-- Crumble_cook
| | | \-- Crumble_put
| | | \-- Crumble_bake
Likely the most efficient method besides specific hardware tracing support, but has the downside that you have to recompile the code.
Understand does a very good job of creating call graphs.
Our DMS Software Reengineering Toolkit has static control/dataflow/points-to/call graph analysis that has been applied to huge systems (~~25 million lines) of C code, and produced such call graphs, including functions called via function pointers.
You may try CScope + tceetree + Graphviz.
You can check out my bash-based C call tree generator here. It lets you specify one or more C functions for which you want caller and/or called information, or you can specify a set of functions and determine the reachability graph of function calls that connects them... I.e. tell me all the ways main(), foo(), and bar() are connected. It uses graphviz/dot for a graphing engine.
Astrée is the most robust and sophisticated tool out there, IMHO.

Resources