473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Retrieving function parameters

Hi List,

This question can be categorised under "C programming in Linux", but
as I didnt find any group of that sort, I post it here.

I have a small program to print the stack trace of a particular
function. Here is the code:
================================================== ==
#include <stdio.h>
#include <execinfo.h>
#include <stdlib.h>

void print_trace (void){
void *arr[20];
size_t asize, icnt;
char **str;

asize = backtrace (arr, 20);
str = backtrace_symbols (arr, asize);
for (icnt=0; icnt<asize; ++icnt)
printf ("%s\n",str[icnt]);
free (str);
}
int main(int argc, char **argv)
{
print_trace ();
return 0;
}
================================================== ==
By calling backtrace ( ) and backtrace_symbols ( ) I can get a trace
of the calling functions path.
What I would like to know is if I can find the contents of the calling
functions () s argument values too?

More precisely:

A(void) {
....
backtrace ( )
backtrace_symbols ( )
....
}

B (int foo, int bar){
A () ;
....
}

C (int foo, int bar, int baz){
B ();
....
}

int main (){
C ()
....
....
}
>From the backtrace_symbols we can generate a path of the function call
main() calling C ()-C() calling B() -B() calling A(). Can we also
get the values for the arguments used in C (foo, bar, baz) and B (foo,
bar)? If we could then how?

Thanks
Anirbid

Feb 9 '07 #1
5 2554
On 9 Feb, 13:11, "anirbid.baner...@gmail.com"
<anirbid.baner...@gmail.comwrote:
Hi List,

This question can be categorised under "C programming in Linux", but
as I didnt find any group of that sort, I post it here.
<Off-topic>
It's actually a question about the GNU libc library, and is strictly
speaking off-topic here.

One of the GNU mailing lists might be an appropriate place to ask.

http://www.gnu.org/software/libc/man...acktraces.html
seems to cover what it can do.

Accessing function arguments isn't on the list. It would be difficult
to provide in any optimised situation, I'd have thought.
</Off-topic>

Feb 9 '07 #2
an**************@gmail.com wrote:
Hi List,

This question can be categorised under "C programming in Linux", but
as I didnt find any group of that sort, I post it here.

I have a small program to print the stack trace of a particular
function. Here is the code:
================================================== ==
#include <stdio.h>
#include <execinfo.h>
#include <stdlib.h>

void print_trace (void){
void *arr[20];
size_t asize, icnt;
char **str;

asize = backtrace (arr, 20);
str = backtrace_symbols (arr, asize);
for (icnt=0; icnt<asize; ++icnt)
printf ("%s\n",str[icnt]);
free (str);
}
int main(int argc, char **argv)
{
print_trace ();
return 0;
}
================================================== ==
By calling backtrace ( ) and backtrace_symbols ( ) I can get a trace
of the calling functions path.
What I would like to know is if I can find the contents of the calling
functions () s argument values too?

More precisely:

A(void) {
...
backtrace ( )
backtrace_symbols ( )
...
}

B (int foo, int bar){
A () ;
...
}

C (int foo, int bar, int baz){
B ();
...
}

int main (){
C ()
...
...
}
>>From the backtrace_symbols we can generate a path of the function call
main() calling C ()-C() calling B() -B() calling A(). Can we also
get the values for the arguments used in C (foo, bar, baz) and B (foo,
bar)? If we could then how?

Thanks
Anirbid
Of course you can. You have just to write a debugger.

To know what the values in the stack *are* you have to know the
type of the arguments. THEN and only then, you can interpret
the bits in the stack in a meaningful fashion. If not, you can just
see some bits but you have no idea of what they represent.

Besides, you have to know that the values start at a certain offset from
the stack, the organization of stack/frame pointer, and several other
interesting things.

Writing a debugger is a lot of fun. Lcc-win32's debugger costed me some
pain, but it was worth.

It is not a completely general debugger of course. I have
never tested it outside lcc-win32, and I suppose that you have enough
time to write a differeent debugger for each compiler you will use.

Debug information formats vary a lot. Microsoft used NB09 in Windows 98
and changed to NB10 in windows 2000. I use NB09 since I did not see why
I should change.

Gcc uses a different format called "stabs", under windows, and in the
old versions of linux. Now the changed to DWARF, leaving the stabs format.

Under linux, I use the stabs format, and DWARF when there is no other
choice.

But there is also not only the debug information format that changes.
You have to know that the frame pointer organization
changes considerably if you are in optimized code or not.

This could throw off many of your offsets if you do not take care.

But it is a fun project.

Feb 9 '07 #3
ma**********@pobox.com wrote, On 09/02/07 13:29:
On 9 Feb, 13:11, "anirbid.baner...@gmail.com"
<anirbid.baner...@gmail.comwrote:
>Hi List,

This question can be categorised under "C programming in Linux", but
as I didnt find any group of that sort, I post it here.
<Off-topic>
It's actually a question about the GNU libc library, and is strictlybe a good place to look.
speaking off-topic here.
<snip>

The linux application development groups would probably be a good place
to ask after checking the relevant FAQ of course.
--
Flash Gordon
Feb 9 '07 #4
"an**************@gmail.com" wrote:
>
This question can be categorised under "C programming in Linux",
but as I didnt find any group of that sort, I post it here.

I have a small program to print the stack trace of a particular
function. Here is the code:
================================================== ==
#include <stdio.h>
#include <execinfo.h>
#include <stdlib.h>
.... snip ...
>
From the backtrace_symbols we can generate a path of the function
call main() calling C ()-C() calling B() -B() calling A().
Can we also get the values for the arguments used in C (foo, bar,
baz) and B (foo, bar)? If we could then how?
First, this is not a C question. Try comp.unix.programmer. There
is no such thing as <execinfo.hetc. in standard C.

Second, C parameters are passed by value, and may be freely
modified. Thus, even if you can see the present value of those
parameters, there is no guarantee that those are the passed-in
values.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 9 '07 #5
<an**************@gmail.comwrote in message
news:11*********************@j27g2000cwj.googlegro ups.com...
Hi List,

This question can be categorised under "C programming in Linux", but
as I didnt find any group of that sort, I post it here.
comp.unix.programmer would be a good start for anything POSIX. There's
also several Linux newsgroups under comp.os.linux.
From the backtrace_symbols we can generate a path of the function call
main() calling C ()-C() calling B() -B() calling A(). Can we also
get the values for the arguments used in C (foo, bar, baz) and B (foo,
bar)? If we could then how?
You probably can't do it, and if you can it certainly won't be portable.
There's no guarantee that the calling functions' arguments still exist,
as they may have been passed in registers (depending on the platform) or
their location on the stack may have been reused for other purposes.
You're lucky to get the function call path, and that isn't portable
either.

If you need those details, your best bet is a debugger.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
--
Posted via a free Usenet account from http://www.teranews.com

Feb 9 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Justin | last post by:
I have created a dataset with two tables and an insert command, I need to be able to retreive the Key Identity after inserting into table "A" for use in table "B". Should I use ExecuteScalar()...
6
by: Stu Lock | last post by:
Hi, I have a stored procedure: --/ snip /-- CREATE PROCEDURE sp_AddEditUsers ( @Users_ID int, @UserName nvarchar(80), @Password nvarchar(80),
0
by: Andy | last post by:
Hi All. I'm working for a company that has set out a guideline for retrieving data from a database. Nobody can explain to me the reason for the following. When retrieving a set of records...
2
by: Sakke | last post by:
Hello! We have written a GCryptoSvr.dll COM server in C++. Inside that resides WebClient COM component. WebClient CLSID is {8DC27D48-F94C-434B-A509-C3E1A3E75B9E}. When we are using that...
3
by: Susanne Klemm | last post by:
Hello! I use a procedure to insert a new row into a table with an identity column. The procedure has an output parameter which gives me the inserted identity value. This worked well for a long...
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
5
by: Sanjay Pais | last post by:
I have a table with over 1.3 million rows. I am retrieving only 20 at a time using the with - over clauses In query analyser, the data is retrieved in under a second. When retrieving using the...
4
by: smartin | last post by:
Hi, I'm having problem retrieving data from an SQL stored procedure. I tried debugging but it wont give a the reason for the error. it just throws an exception after executing cmd.ExecuteNonQuery...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.