是否有可能以编程方式找出方法的调用者 [英] is it possible to find out the caller of a method programmatically

查看:63
本文介绍了是否有可能以编程方式找出方法的调用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下

foo1 ---调用---> foo2

我可以打印 - 当控件在foo2内时 -

foo2的来电者


信息显然在堆栈中 - 我想知道是否通过

组装和指针操作的某种组合 - 我可以打印

方法调用者的名称

感谢您的任何建议

in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2

the information is obviously there in the stack - am wondering if by
some combination of assembly and pointer manipulation - I can print
the name of the caller of the method
Thanks for any suggestions

推荐答案

" 2pac" < CA ****** @ yahoo.com>在消息中写道

news:d0 ************************** @ posting.google.c om ...
"2pac" <ca******@yahoo.com> wrote in message
news:d0**************************@posting.google.c om...
在这种情况下
foo1 ---调用---> foo2
是否有可能打印出来 - 当控件在foo2内时 -
foo2的调用者
信息显然在堆栈中 - 我想知道是否通过
汇编和指针操作的一些组合 - 我可以打印
方法调用者的名字
感谢您的任何建议
in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2

the information is obviously there in the stack - am wondering if by
some combination of assembly and pointer manipulation - I can print
the name of the caller of the method
Thanks for any suggestions




不幸的是,可以给你的唯一建议是

C中没有对这种功能的标准支持。

(除非foo1明确接收有关的信息)调用者,

作为函数参数或在某些全局变量中。


但是,是的,在特定于平台的方式和调试模式中编译,

通常可以通过调用堆栈从

获取所需的信息。有时甚至会提供专用的API。

但你必须在特定于你的平台的论坛中询问

(操作系统,ISA和编译器)...


问候,

-
http://www.post1.com/~ivec <> Ivan Vecerina



Unfortunately, the only advice that can be given to you is that
there is no standard support for such a functionality in C.
(except if foo1 explicitly receives information about the caller,
as a function parameter or in some global variable).

But yes, in platform-specific ways and in ''debug mode'' compiles,
it is typically possible to obtain the information you need from
the call stack. Sometimes a dedicated API is even provided.
But you will have to ask in a forum specific to your platform
(OS, ISA & compiler)...

Regards,
--
http://www.post1.com/~ivec <> Ivan Vecerina


2003年9月6日12:46:09 -0700,在comp.lang.c中, ca ****** @ yahoo.com

(2pac)写道:
On 6 Sep 2003 12:46:09 -0700, in comp.lang.c , ca******@yahoo.com
(2pac) wrote:
在这种情况下
foo1 ---调用---> foo2
是否可以打印出来 - 当控件在foo2内时 -
foo2的调用者
in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2




您的调试器可以执行此操作,显然它是可能的。但是在标准C中它不可能是b $ b,你需要使用特定于平台的调试

调用。阅读你的编译手册,或者在专业的小组中询问

吧。


-

Mark McIntyre

CLC FAQ< http://www.eskimo.com/~scs/C-faq/top.html>

CLC自述文件:< http://www.angelfire。 com / ms3 / bchambless0 / welcome_to_clc.html>



Your debugger can do this, so obviously its possible. However its not
possible in standard C, you''d need to use platform specific debugging
calls. Read your compiler manual, or ask in a group specialising in
it.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


2pac写道:
2pac wrote:
在这个场景中
foo1 - -calls ---> foo2
是否有可能打印出来 - 当控件在foo2内时 -
foo2的调用者
信息显然在堆栈中 - 我想知道是否通过
汇编和指针操作的一些组合 - 我可以打印
方法调用者的名字
感谢您的任何建议


cat foo。 c
#include< stdio.h>


int foo(const char * function,const char * file,int line){

fprintf(stderr,

" Function foo(const char *,const char *,int)\ n"

函数,文件,行);

返回0;

}


int main(int argc,char * argv []){

foo(__ func __,__ FILE __,_ _ _ _ 1);

返回0;

}

gcc -Wall -std = c99 -pedantic -o foo foo.c
./foo
in this scenario
foo1 ---calls---> foo2
is it possible for me to print out - when the control is within foo2 -
the caller of foo2

the information is obviously there in the stack - am wondering if by
some combination of assembly and pointer manipulation - I can print
the name of the caller of the method
Thanks for any suggestions
cat foo.c #include <stdio.h>

int foo(const char* function, const char* file, int line) {
fprintf(stderr,
"Function foo(const char*, const char*, int)\n"
"called from function %s in file %s at line %d.\n",
function, file, line);
return 0;
}

int main(int argc, char* argv[]) {
foo(__func__, __FILE__, __LINE__);
return 0;
}
gcc -Wall -std=c99 -pedantic -o foo foo.c
./foo



功能foo(const char *,const char *,int)

c从第12行的文件foo.c中的函数main进行调整。


Function foo(const char*, const char*, int)
called from function main in file foo.c at line 12.


这篇关于是否有可能以编程方式找出方法的调用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆