如何通过C程序知道知道调用函数名称 [英] How to know the know calling function name by C programme

查看:51
本文介绍了如何通过C程序知道知道调用函数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在进行庞大的C编程项目。我只能通过printf进行调试

measn我没有任何dubugger。所以很难知道谁实际上正在调用一个函数,我陷入了一个问题,我已经看到一个函数被大约160个位置调用。我需要解决一些问题,从160个位置获取打印是非常可取的。那么,有没有办法知道谁在运行时调用该函数?是否有任何小程序或技巧将在那里,了解谁在运行时调用该函数。所以我可以在被调用函数或任何其他方式中打印该函数名称,我的工作将很容易。


请帮帮我。

我在等你的回复

谢谢

mcarthy

解决方案

这里有一个可以帮助你的黑客,它不会识别调用函数,但会识别调用代码行。


让我们假设您的功能定义如此

展开 | 选择 | Wrap | 行号


如果你能找到你的特定编译器的调用约定,那么你可以破解一个非常不可移植的机制打印调用树[谁叫谁到达你所在的位置]。


例如,也许你的编译器总是通过用堆栈的值加载一个特定的寄存器来创建堆栈帧进入每个函数时的指针。展开堆栈帧与解包函数调用相同。


此技术仅为您提供调用每个函数的地址;你将不得不查阅一个链接图来将其转换为函数名。


确保你的跟踪解包器将终止,所以你不能永远循环。

我建​​议安装一个日志文件。


我的意思是一个圆形文本文件,比如256个固定长度的记录。那是一个平面文件。第一个记录包含一个计数器,指示要写入下一个的记录。写入后,第一个记录中的计数器递增。当它达到255时,它被重置为1(记录0有计数器,记录1是第一个日志条目)。


这样文件永远不会超过256条记录可以无限期运行,无需任何维护。


此外,我还要添加一个系统设置,日志文件功能可以测试以确定是否打开日志记录。当日志记录关闭时,日志文件功能是空函数。通常,我有第二个日志文件路径名设置。


实现这个就像fprintf:

展开 | 选择 | Wrap | 行号

hi ,
I am working on huge C programming project.I am doin debugging only by printf
measn i dont have any dubugger .So it is very difficult know who is actually calling a function and i am stuck in a problem that , i have seen that one fucntion is get called by from some 160 postions. and i need to solve some problem and it is very probmatic to get print from 160 postions.So ,is there any way to get to know who is calling that function in runtime ? Is there any small programme or trick will be there , get to know who is calling that function in runtime.so i can print that function name inside that called function or any other way and my work will be easy .

Please help me on this.
I am waiting for your reply
Thanks
mcarthy

解决方案

Well here is a hack that might help you, it wont identify the calling function but will identify the calling line of code.

Lets assume that you function is defined like so

Expand|Select|Wrap|Line Numbers


If you can find out the calling conventions of your particular compiler then you can hack a very nonportable mechanism for printing the call tree [who called who to get to where you are].

For instance, perhaps your compiler invariably creates a stack frame by loading one particular register with the value of the stack pointer upon entry to each function. Unwrapping the stack frames is the same as unwrapping the function calls.

This technique only gives you the address from which each function was called; you will have to consult a link map to translate that to function name.

Be sure your trace unwrapper will terminate so you don''t loop forever.


I would recommend installing a logfile.

By this I mean a circular text file of, say 256 fixed-length records. That is a flat file. The first record contains a counter indicating the record to write to next. After writing, the counter in the first record is incremented. When it gets to 255, it is reset to 1 (record 0 has the counter, record 1 is the first log entry).

This way the file never has more than 256 records and can run indefinitely without requiring maintenance of any sort.

In addition, I would add a system setting that the logfile fuinction can test to determine whether logging is turned on or not. When logging is off, the logfile function is a null function. Usually, I have a second setting for the logfile pathname.

Implement this as you would fprintf:

Expand|Select|Wrap|Line Numbers


这篇关于如何通过C程序知道知道调用函数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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