确定函数调用地址 [英] Determining a functions calling address
问题描述
我试图想办法在函数内部打印一个名为
某个函数的地址。
我假设这个信息在某个地方的堆栈上。但是可以
有人给我一些
的建议吗?我不知道去哪里查看这些信息。
我是否需要访问堆栈,因此进行某种内联汇编
代码获取呼叫地址?
例如:
int main()
{
myFunction(); //我需要在myFunction函数中打印此位置的地址或下一行
行。
I am trying to figure out a way to print the address of what called a
certain function once inside the function.
I am assuming that this information is on the stack somewhere. But can
someone give me some
advice? I don''t know where to go to look this information up.
Do I need to access the stack, and hence do some kind of inline assembly
code to get the calling address?
For example:
int main()
{
myFunction(); // I need the address of this location or the next
line to be printed in the myFunction function.
推荐答案
johny smith < PR ************** @ charter.net>在消息中写道
新闻:10 ************* @ corp.supernews.com ...
[snip]
"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
[snip]
void myFunction()
{
//打印出调用函数地址在main或地址
调用此函数的位置。
//基本上,我试图找出谁根据地址调用此函数
。这用于调试
//所以如果我在这个函数中出现
错误,我可以追踪谁(什么位置)进行了调用。
}
void myFunction()
{
// print out the calling functions address in main or the address of
the location that made this function call.
// basically, I am trying to figure out who called this function based on the address. This is used in debugging
// so I can track down who (what location) made the call if I get an
error in this function.
}
没有可移植的方式去做你想做的事。我建议
为您的代码提供信息输出/日志声明。例如:
log(" In main(),第一次调用myFunction()。);
myFunction();
log("在main()中,第二次调用myFunction()。");
myFunction();
>
这种方法可能会使你的日志更具可读性,代码
会更便携。
-
David Hilsee
There is no portable way to do what you want to do. I would recommend
sprinkling your code with informative output/log statements. For example:
log("In main(), calling myFunction() the first time.");
myFunction();
log("In main(), calling myFunction() the second time.");
myFunction();
That approach would probably make your logs more readable, and the code
would be more portable.
--
David Hilsee
" johny smith" < PR ************** @ charter.net>在消息中写道
新闻:10 ************* @ corp.supernews.com ...
"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
我想弄清楚一种在函数内部打印某个函数的地址的方法。
I am trying to figure out a way to print the address of what called a
certain function once inside the function.
你不能只是将调用者的地址传递给功能?对于
示例,以下显示了两个版本:模板版本和普通
函数版本。希望这会有所帮助。
#include< iostream>
使用std :: cout;
void doThatStuff(void * vp);
template< class T>
void doThisStuff(T * ptr){
cout<< "错误。 doStuff(T * ptr)从地址调用 << ptr<< " \ n";
}
int main(){
doThisStuff (& main);
doThatStuff(& main);
返回0;
}
void doThatStuff(void * vp){
cout<< "错误。 doStuff(void * vp)从地址调用 << vp<< " \ n";
}
// mike tyndall
Couldn''t you just pass the address of the caller to the function? For
example, the following shows two versions: a template version and a normal
function version. Hope this helps.
#include <iostream>
using std::cout;
void doThatStuff(void* vp);
template<class T>
void doThisStuff(T* ptr) {
cout << "Error. doStuff(T* ptr) called from address " << ptr << "\n";
}
int main() {
doThisStuff(&main);
doThatStuff(&main);
return 0;
}
void doThatStuff(void* vp) {
cout << "Error. doStuff(void* vp) called from address " << vp << "\n";
}
//mike tyndall
斯蒂芬廷德尔 < SW ******* @ hotmail.com>在消息中写道
news:L _ ******************** @ comcast.com ...
"Stephen Tyndall" <sw*******@hotmail.com> wrote in message
news:L_********************@comcast.com...
" ;约翰·史密斯< PR ************** @ charter.net>在消息中写道
新闻:10 ************* @ corp.supernews.com ...
"johny smith" <pr**************@charter.net> wrote in message
news:10*************@corp.supernews.com...
我试图找出一种打印方式在函数内部一次调用某个函数的地址。
I am trying to figure out a way to print the address of what called a
certain function once inside the function.
你不能只是将调用者的地址传递给函数吗?对于
示例,以下显示了两个版本:模板版本和普通
功能版本。希望这会有所帮助。
#include< iostream>
使用std :: cout;
void doThatStuff(void * vp);
void doThisStuff(T * ptr){
cout<< "错误。 doStuff(T * ptr)从地址调用 << ptr<< " \ n";
}
int main(){
doThisStuff(& main);
doThatStuff(& main);
返回0;
}
void doThatStuff(void * vp){
cout<< "错误。 doStuff(void * vp)从地址调用 << vp<< " \ n";
}
// mike tyndall
Couldn''t you just pass the address of the caller to the function? For
example, the following shows two versions: a template version and a normal
function version. Hope this helps.
#include <iostream>
using std::cout;
void doThatStuff(void* vp);
template<class T>
void doThisStuff(T* ptr) {
cout << "Error. doStuff(T* ptr) called from address " << ptr << "\n";
}
int main() {
doThisStuff(&main);
doThatStuff(&main);
return 0;
}
void doThatStuff(void* vp) {
cout << "Error. doStuff(void* vp) called from address " << vp << "\n";
}
//mike tyndall
BTW,我意识到这是对于任何大型项目来说都是不切实际的,但
也许它会以某种方式提供帮助。也许你可以将#ifdef DEBUG指令放在
代码中,如果定义了DEBUG,还会添加额外的函数参数。
祝你好运!
BTW, I realize this is totally impractical for any large-scale project, but
maybe it''ll help somehow. Maybe you could put #ifdef DEBUG directives in
the code, where extra function arguments are added if DEBUG is defined.
Good luck!
这篇关于确定函数调用地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!