app*_*app 6 c linux gdb bfd dladdr
是否可以在 C 程序中在运行时知道函数参数和变量的名称类型?例如,如果我有一个函数:
int abc(int x, float y , somestruct z ){
char a;
int b ;
}
Run Code Online (Sandbox Code Playgroud)
我可以知道在这个函数内部abc(),参数和变量的名称是什么,即在这种情况下它的x, y, z, a,b它们的类型是int, float, somestruct, char, int。
说是否还有另一个功能:
float some_func(specialstruct my_struct, int index){
}
Run Code Online (Sandbox Code Playgroud)
我应该知道参数 name 是my_struct,index类型是specialstruct, int。
我在运行时需要这些信息?
我可以访问基指针和返回地址,是否可以使用上述指针获取所需信息。
我能够使用返回地址和dladdr()函数提取函数名称。
我明白了GDB,所以应该可以提取这些信息?
C 中没有反射或类似的东西。如果你想要这样的设施 - 你应该为此目的设计一些实用程序、宏,并使用特殊的编码规则来达到所需的效果。但 IMO - 它不会是可读且可理解的 C 代码。