Can anyone suggest a way to trace every call of a function? - c

I want to trace each path of function calls.
For eg:
int a()
{
b();
return 1;
}
void b()
{
}
int main()
{
int x=a();
return 0;
}
So my call trace is main->a->b
In this manner I want to trace each set paths of calls.
I have thought of a depth first search. But i am not sure how this would go.
Can anyone suggest me any concrete method to be implemented in perl?
I will have C program file and will run a perl script over it to get call traces.

There are a number of free call-graph programs listed in this article, including egypt which is a small Perl script that uses gcc and Graphviz to generate the static call graph of a C program.

One way is automatically instrument the source code with probes that collect the information you want as the program runs. You can use a program transformation tool to do that.
Here's a paper on how do collect information abouth "which blocks" get executed, using a transformation system to insert such probes. A very small change to the specification of where to put probes, and some minor work to capture the current function would accomplish what you want in a relaible way.

I believe Doxygen can do just that.

Related

Is there a way to display all possible backtraces of a function using some static code analyzer?

I have quite a big source code written in C and would like to generate all possible backtraces of a function. There are tools to obtain similar result during execution of the application, for example: STAT. Instead, I am in need of getting this information with just the source code, like with example some kind of static code analysis.
One can obtain this information manually one function at a time using CSCOPE.
I also came across another thread in here: how can I display all function name from cscope database? that uses CSCOPE to list all functions recursively in all directories. I hope there should be some way to use the CSCOPE command line to automatically retrieve all backtraces.
I'm not sure if this does exactly what you want, but https://github.com/ruben2020/codequery can read in cscope databases and do a bunch of manipulations with them so it might get you closer at least.

How to include the command "wget" on my C source code?

I need to run a program that crawls websites and I already have an algorithm and some parts of the code. Problem is, I do not know how to insert wget into my source code. Our student assistant hinted that some kind of keyword or function shall be used before the wget( system, I think or something but I'm not so sure).
when to not use system:
1.) when you want to distribute the program to different environment, where the program you call via system is not available
2.) in a security relevant environment, where you have to make sure that the program you call is really the program you want it to be
3.) when the thing you want to do can easily be accomplished in 10-20 lines of C code
4.) in performance-critical applications
so, you should use system virtually never.
instead, to accomplish the same thing, you could use libcurl, as David suggested (his answer seems to be gone...), or do some socket programming (it's C, after all).
In a real-world scenario, I'd probably just default to writing the crawler in a different language. web requests and complex string processing are not necessarily the strong sides of C, and most definitely not very convenient to use :)
You can use the system() command.
In your case (possibly):
system("/bin/wget");
But if you want really call wget with parameters, so you should use execl().
execl("/bin/wget", "http://anyadress.com/file");
Whenever , you want to run shell commands from your C program , you use system("shell command").In your case
system("wget");
Note - wget is an executable , whose location is added to the path variable, so there is no need to specify the path explicitly.
-- Example --
#include <stdio.h>
#define BUFFLEN 2500
int main()
{
char web_address[BUFFLEN] = "www.google.com";
system("wget 'web_address' ");
return 0;
}
The system command is used to execute a shell command. man system

run picoC non-recursively as an iterated function

I've been playing around with a few C interpreters and have found
picoC to look like it meets all my needs.
to kick off a script you call
void PicocCallMain(int argc, char **argv); which recursively calls the internal
parser etc..
Is it possible to recode picoC so that I could run scripts iteratively.
for example.
while(1)
{
picoCyield(&script1);
picoCyield(&script2);
}
each call to picoCyield would call the token reader no more than required to
execute the smallest possible block of script.
I could run picoC as is with threads, but I the enviorment I am working in
prohibits it..
Any help, or pointers to a similair interpreter that can do this, would
be greatly appreciated.
I would look at the top-level code for interactive mode. Where it currently prints the prompt and waits for input, I would replace with a callback to your program which you would use to provide the next statement. Then all the line-by-line execution is already done for you.

Is there a compiler feature to inject custom function entry and exit code?

Currently coding on Windows with VS2005 (but wouldn't mind knowing if there are options for other compilers and platforms. I'm most interested in OSX as an alternative platform.) I have a C (no C++) program and I'd like to do the following...
Given a function, say...
int MyFunction(int myparam)
{
// Entry point.
...
// Exit point.
return 1;
}
I'd like to put a snippet of code at the entry point and at the exit point. BUT, I'd rather not have to modify the 100's of functions that are already out there. Is there a way to define function entry and exit code that the compiler will inject for all my functions without having to modify them all?
Most solutions I've found or tried will require me to edit every single function, which is a lot of work. I figure someone else must have hit something like this already and solved it. I can't be unique in this request I suspect.
It's Microsoft-specific, but you can hook into the _penter and _pexit functions to do something when entering and exiting a function -- you'll have to compile your project with some special flags.
There's a little bit of a tutorial here, and you can find a few more results on how to use them on Google. Also, this blog post goes into some detail on the assembly that you need to do to avoid messing up the stack on entry and exit.
GCC has the -finstrument-functions flag which allows you to define two functions that will be called at the beginning and end of each function call:
void __cyg_profile_func_enter(void *this_fn, void *call_site);
void __cyg_profile_func_exit(void *this_fn, void *call_site);
You're looking for something called aspect oriented programming or AOP.
This isn't something that's supported natively in C (or C++). There are some library based implementations listed on the linked page for C (though I don't know how mature / useful these are)
OpenWatcom C and C++ compilers have -ee and -ep parameters for that:
-ee call epilogue hook routine
-ep[=<num>] call prologue hook routine with <num> stack bytes available
They will cause the compiler to emit calls to __EPI and __PRO user-defined hook routines.
There is also
-en emit routine names in the code segment
that will emit the function name into the object code as a string of characters just before the function prologue sequence is generated. May be useful for the __PRO routine.
More information in these and other compiler options can be found in the C/C++ user guide available among other manuals at http://openwatcom.org/index.php/Manuals

How do I extract a specific function from a C/C++ source code file for subsequent processing

I am looking for an easy way to print out a specific function from within some C/C++ source code. For example, assume that test.c has several functions defined within it. I want to be able to print out the source code associated with only one of those functions.
Edit: Sorry, I should be a bit more clear about my end goal. I want the function printed to the screen so I can use wc to grab the word count of this specific function. Also, I want this be part of a command line tool-chain so it isn't an option to manually enter files and select the text.
You can run your project through doxygen. It will index all your functions (and classes, structs etc) and can make them available in multiple formats (including PDF and HTML, both easily printable).
What is your end goal with printing out a function?
Do you want to use this as such:
if (error == Foo())
{
PrintFunction(foo);
exit(1);
}
There are easier ways to output where errors are. I could maybe help more if I had a better idea of the problem you are trying to solve with this.
For a idea of how to implement such a PrintFunction():
Have a data struct that wraps around a function and contains: function line start, function line end, and maybe a pointer to the function.
Write a function that prints out a line base on number of the
source file. __FILE__ gives you the source file name.
With knowing the start and end of where the function lies in the
code, printing the function would be trivial.
This has an annoying pitfall of needing to update the line numbers of where your function lies in the file. But this could maybe be solved with a macro.
I generally use the print-region (or preferably print-region-with-faces) from within emacs. However, it is not automated, I have to select the region by hand.
Works in other languages as well.
The following due to Tom Smith in the comments:
(defun print-fn (interactive)
(save-excursion (mark-defun)
(print-region)))
If you liked this, follow the link to Tom's user-page and see if he deserves your vote...
Making this CW, so I won't benefit from people voting up Tom's good thinking. Cheers.
Edit after clarification: This doesn't seem to be pointed at the OP's actual question. Alas.

Resources