find out the origin of function calls - c

I wonder if there is a software that can help us determine all possible origins of a function call.
For example:
/* in file f1.c */
int f1() {
x_func();
}
/* in file f2.c */
int f2() {
x_func();
}
If we want to trace the origin of all function calls to x_func(), the output will be:
f1.c:f1()
f2.c:f2()
This is very useful when reading the source code.
All answers are appreciated. Thank in advance :D

cscope can help here

If you want to the this at runtime any debugger will be able to do that: just place a breakpoint within x_func and print the stack trace any time it pauses.

Doxygen will do that for you (with pictures!).
Also many IDEs also incorporate such capabilities Visual Studio for example will generate textual call graphs as well as having a "find all references" search. You can do this with the free VC++ Express Edition, the project need not be a VC++ project to use its code navigation features, just create a makefile project and add the header file paths to the preprocessor settings.

Tagfile programs, such as ctags and etags, do this. They're written to work with editors rather than to be specifically human readable. Emacs's M-. key looks things up in the tagfile.

Related

How to stop GDB stepping in to system calls? [duplicate]

I have some C++ code like this that I'm stepping through with GDB:
void foo(int num) { ... }
void main() {
Baz baz;
foo (baz.get());
}
When I'm in main(), I want to step into foo(), but I want to step over baz.get().
The GDB docs say that "the step command only enters a function if there is line number information for the function", so I'd be happy if I could remove the line number information for baz.get() from my executable. But ideally, I'd be able to tell GDB "never step into any function in the Baz class".
Does anyone know how to do this?
Starting with GDB 7.4, skip can be used.
Run info skip, or check out the manual for details: https://sourceware.org/gdb/onlinedocs/gdb/Skipping-Over-Functions-and-Files.html
Instead of choosing to "step", you can use the "until" command to usually behave in the way that you desire:
(gdb) until foo
I don't know of any way to permanently configure gdb to skip certain symbols (aside from eliding their debugging information).
Edit: actually, the GDB documentation states that you can't use until to jump to locations that aren't in the same frame. I don't think this is true, but in the event that it is, you can use advance for the same purpose:
(gdb) advance foo
Page 85 of the GDB manual defines what can be used as "location" arguments for commands that take them. Just putting "foo" will make it look for a function named foo, so as long as it can find it, you should be fine. Alternatively you're stuck typing things like the filename:linenum for foo, in which case you might just be better off setting a breakpoint on foo and using continue to advance to it.
(I think this might be better suited as a comment rather than an answer, but I don't have enough reputation to add a comment yet.)
So I've also been wanting to ignore STL, Boost, et al (collectively '3rd Party') files when debugging for a while. Yesterday I finally decided to look for a solution and it seems the nearest capability is the 'skip' command in GDB.
I found the 'skip' ability in GDB to be helpful, but it's still a nuisance for me because my program uses a lot of STL and other "3rd Party" template code. In this case I have to mark a bunch of files as skip. After the 2nd time doing so I realized it would be more helpful to be able to skip an entire directory--and most helpful to skip a directory and all subdirectories. That way I can skip, for example, /usr since none of my code lives there and I typically have no interest in debugging through 3rd party code. So I extended the 'skip' command in gdb to support a new type 'dir'. I can now do this in gdb:
skip dir /usr
and then I'm never stopped in any of my 3rd party headers.
Here's a webpage w/ this info + the patch if it helps anyone: info & patch to skip directories in GDB
It appears that this isn't possible in GDB. I've filed a bug.
Meanwhile, gdb has the skip function command. Just execute it when you are inside the uninteresting function and it will not bother you again.
skip file is also very useful to get rid of the STL internals.
As Justin has said, it has been added in gdb 7.4. For more details, take a look at the documentation.

How to find the callers and callee of a function in C code in vi/vim?

I want to know how can I easily click (or maybe use some easy shortcuts) on a function name and find all its callee or open where it has been defined. Most of the web manuals in web are really hard to follow or don't happen to work out. Say I want to click on allocuvm and see where it has been defined?
uint newstk=allocuvm(pgdir, USERTOP-PGSIZE, USERTOP);
cscope minimal example
Ingo mentioned it, here is an example.
First you should set on your .vimrc:
set cscopequickfix=s-,c-,d-,i-,t-,e-
Then to the base directory of your project and run:
cscope -Rb
This generates a cscope.out file which contains the parsed information. Generation is reasonably fast, even for huge projects like the Linux kernel.
Open vim and run:
:cs add cscope.out
:cs find c my_func
c is a mnemonic for callers. The other cscope provided queries are also possible, mnemonics are listed under:
help cscope
This adds a list of the callers to the quickfix list, which you can open with:
:copen
Go to the line that interests you and hit enter to jump there.
To find callers of the function name currently under the cursor, add to your .vimrc:
function! Csc()
cscope find c <cword>
copen
endfunction
command! Csc call Csc()
and enter :Csc<enter> when the cursor is on top of the function.
TODO:
do it for the current function under cursor with a single command. Related: Show function name in status line
automatically add the nearest database (parent directories) when you enter a file: how to auto load cscope.out in vim
interactively open the call graph like Eclipse. Related: Generate Call-Tree from cscope database
A word of advice: I love vim, but it is too complicated for me to setup this kind of thing. And it does not take into account classes e.g. in C++. If a project matters enough to you, try to get the project working on some "IDE". It may involve some overhead if the project does not track the IDE configuration files (which are auto-changing blobs that pollute the repo...), but it is worth it to me. For C / C++, my favorite so far was KDevelop 4.
For that, Vim integrates with the cscope tool; see :help cscope for more information.
vi / . --- / is the search function in vi, and . will repeat the same command.
you could also use sed ( stream editor ) if it is a large file
sed
grep can get you the line numbers
read the man page

What is the easiest way to parse an INI File in C?

There are some similar questions about C++, Java and C# so now my question is about C. If I have this config file
[BBDD]
user=**
password=***
database=***
IPServidor=*
port=3***
[Device]
dev=8
Temperatura=5=1001
Humedad=7=1002
Link=8=1003
Volt=9=1004
[Device]
dev=10
Temperatura=5=1012
Humedad=7=1013
Link=8=1014
Volt=9=1015
what is the best way to read the values of Device. I am a linux user. I used glib but I had some problems because there is the same key (Device) so it returns me as the tutorial says only the values of the last Device array. Also Boost as I know has libraries for C++, libconfig also I think is not used for this kind of config files. Finally iniparser has a difficult installation guide for me. Do you think that some solutions like sscanf, fprintf are good?
Finally iniparser has a difficult installation guide for me. Do you think that some solutions like sscanf, fprintf are good?
The iniparser may have a difficult installation, but that's a small tradeoff for code that already works, has already been tested, and handles cases that you haven't thought of.
What problems are you having with using iniparser? I just tried it. I first did make in the iniparser directory, and the code was built. To use the library, I did the following:
gcc test.c ./libiniparser.a
This was because I had created the test program in the same directory as the library. When you include iniparser.h in C++, make sure to do the following:
extern "C"
{
#include "src/iniparser.h"
}

Using multiple tag files at once in vim / Tag organisation in general

(Apologies for the C tag, I did it for the syntax highlighting. This is more of a vim question. If someone more learned than I thinks the tag should be removed please do so)
Say I've got this directory structure:
Directory ~/Code/Test/ containing file1.c file2.c file4.c and Sub
Directory ~/Code/Test/Sub/ containing file3.c
file1.c:
#include <stdio.h>
#include "file2.c"
#include "Sub/file3.c"
void function1();
int main (int argc, char *argv[]) {
function1();
function2();
function3();
return 0;
}
void function1() {
printf("1\n");
}
file2.c:
#include <stdio.h>
void function2();
void function2() {
printf("2\n");
}
Sub/file3.c:
#include "../file4.c"
void function3();
void function3() {
printf("3\n");
function4();
}
file4.c:
#include <stdio.h>
void function4();
void function4() {
printf("4\n");
}
In any one of those files it should be possible to jump to the definition of the functions it uses from the other files. So for example, file1 should be able to jump across to file2, file1 should be able to jump down a directory to file3, file3 should be able to jump up a directory to file4, and here's the kicker; all of the files should be able to jump to the definition of printf. I also shouldn't have to copy the tags for the c library implementation into the Test directory in order to do this.
I'm wondering how I could go about doing this. I'm really not keen on monolithic tags files. Having a vim-wide tags file horrifies me. A tags file per directory annoys me. A tags file per project is bearable. But what I'd really like is a tags file per source file, and then a way to specify which tags files vim should be referring to.
Ideally I'd like to be able to just ctrl-] on anything and have vim jump to the correct definition based on what's in scope, just like visual studio. I'm beginning to suspect this can't be done, and if it is (via some combination of plugins) it would be extremely slow, which is really annoying because I was totally on the "Vim can do anything your newfangled IDEs can do" bandwagon for a couple of weeks there. Yes it's definitely the most powerful text editor I've come across, but as an IDE it's extremely unpolished. When I use the "Go to definition" command I expect to be taken to the correct definition whether it's a local variable, in a different file, in a standard library etc. Vim so far has given me hilarious results such as jumping across from a java file to a c file. And you have to use a separate command to jump to the definition of a local variable... What? (If there's a reason behind this I'd be interested in knowing)
I'm aware of wacking set tags=./tags in my .vimrc and that's what I've done so far. But this won't scale if I work on something massive that links separate assemblies and source files from separate projects together.
(To be fair to vim, visual studio doesn't let you jump across assemblies to find definitions either, but it does at least have the good grace to serve up a header file from which you can "load assembly" and navigate to the actual source code you're looking for)
First things first: Vim has never been, is not and will probably never be a proper alternative to an IDE. Whoever made you believe that should be shot and you should be shot too for believing such nonsense.
I'm only half-joking.
Vim is a very powerful programming-oriented text editor but the simple fact that you need to run a dumb external code indexer to get a dumb "jump to definition" or another code indexer to get another dumb "jump to usage" should be a hint that Vim can't realistically be compared to an IDE. Hint: the I in IDE means "Integrated" and the E means "Environment". Since you can't get proper integration and would be hard-pressed to consider Vim as an environment, there's no IDE, here. Only a text editor with many plugins doing different things in different ways and, above all, no serious way to understand your code which is the #1 feature of a descent IDE.
Many users/bloggers claim they are using "Vim as an IDE" or that you could, too, turn Vim into a Python or whatever IDE but the truth is that Vim lacks all the low-level features that would make such a thing possible. You can turn it into something that looks like an IDE, if you are somehow able to believe in your own lies, but it will probably never be an IDE.
Whatever…
The default behavior (which can't be altered in your configuration) of <C-]> or :tag foo is to jump to the first hit in your tags file(s). Neither Vim nor Ctags know about scope. At best, you can be treated with a list from which to choose the correct tag (:ts foo or g]) but that's how far you can go.
Depending on the languages you work with, Cscope might be better at indexing your code but the general principle is the same as with Ctags. Cscope offers a "jump to usage" feature so it might be worth switching just for it.
Making sure the correct tags file(s) are used can be a pain and the doc is surprisingly not very helpful in that regard. There are a bunch of plugins designed to make it simpler that you could try, EasyTags comes to mind.
I admit I don't work on very large projects and not even in C so maybe this won't seem useful but this line in my ~/.vimrc makes working with tags easier:
set tags=./tags;/,tags;/
With this setting, Vim looks up and up (non-recursively) until / for tags files. The main point of this is to have a single tags file at the root of each of my projects that can be used from every file in that project without ever needing to tell Vim where to look for a tags file.
One way to deal with your Java/C mixups could be to put your projects into language-specific directories:
C/
c.tags
proj1/
tags
…
proj2/
tags
…
Java/
j.tags
proj3/
tags
…
proj4/
tags
…
and put you "global" tags files at their root as well as project-specific tags files at the root of their respective projects.
Another way to deal with that issue could be to instruct Vim to load specific tags files depending on the filetype:
autocmd FileType c setlocal tags=/path/to/your/global/c/tags,./tags;/,tags;/

Graph of included files

When I work on someone else's code, I tipically need to abuse of grep in order to find data types declarations etc, and this usually makes me confused.
I'd like to have some tool which analyzes the source code and produces some graphviz-like drawing and allows me to follow dependencies.
Also I've found this on the internet, but I think is taylored for the linux kernel only.
Have you tried doxygen?
Doxygen can produce dot files, and you can build the documentation without changing the source code with the right options set in the Doxyfile.
Do you use an editor that can take advantage of tags ? In Emacs, I just type M-. to go to the definition of a symbol, and M-* to go back to where I was once I have read it. This also enables the command tags-search to grep among the files of the software project (very convenient if they are in multiple directories).

Resources