请考虑以下代码:
#include <stdio.h>
int main()
{
static int counter=5;
printf ("Counter = %d\n", counter);
if (counter--)
{
main();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译:
gcc test.c -ansi -Wall –pedantic
Run Code Online (Sandbox Code Playgroud)
执行:
[root@mars home]# ./a.out
Counter = 5
Counter = 4
Counter = 3
Counter = 2
Counter = 1
Counter = 0
Run Code Online (Sandbox Code Playgroud)
这里main()调用自己().
似乎main()每次main()调用函数的原始堆栈帧时都会被覆盖.
但回信地址是什么?函数可以返回自己的堆栈框架吗?
请帮我澄清这个疑问.
谢谢.
在对iPhone 5S设备/ Xcode的7运行的演示应用,我试过让frame pointer一个的任意线程使用thread_get_state,但总是导致不正确的一项:
- (BOOL)fillThreadState:(thread_t)thread intoMachineContext:(_STRUCT_MCONTEXT *)machineContext {
mach_msg_type_number_t state_count = MACHINE_THREAD_STATE_COUNT;
kern_return_t kr = thread_get_state(thread, MACHINE_THREAD_STATE, (thread_state_t)&machineContext->__ss, &state_count);
if (kr != KERN_SUCCESS) {
char *str = mach_error_string(kr);
printf("%s\n", str);
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
我阅读了这样的帧指针:uintptr_t fp = machineContext.__ss.__fp;根据Apple Doc(ARMv6和ARM64),
寄存器R7用作ARMv6上的帧指针
而x64在ARM64上
帧指针寄存器(x29)必须始终寻址有效的帧记录,尽管某些功能(例如叶函数或尾部调用)可能选择不在此列表中创建条目。结果,即使没有调试信息,堆栈跟踪也将始终有意义。
_STRUCT_ARM_THREAD_STATE64
{
__uint64_t __x[29]; /* General purpose registers x0-x28 */
__uint64_t __fp; /* Frame pointer x29 */
__uint64_t __lr; /* Link register …Run Code Online (Sandbox Code Playgroud) 我想从调用方法中获取一个 MethodInfo 对象,以确定该方法是否设置了特殊属性。
具有调用方法 Run() 的 Programm 类
class Program
{
private static RestHandler _handler = new RestHandler();
static void Main(string[] args)
{
Run();
}
[Rest("GET")]
static void Run()
{
_handler.Handler(typeof(Program));
}
}
Run Code Online (Sandbox Code Playgroud)
我想确定自定义属性的类
public class RestHandler
{
public void Handler(Type t)
{
StackFrame frame = new StackFrame(1);
var method = frame.GetMethod();
MethodInfo methodInfo = t.GetMethod(method.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
var attributes = methodInfo.GetCustomAttributes<RestAttribute>();
}
}
Run Code Online (Sandbox Code Playgroud)
属性类
public class RestAttribute : Attribute
{
public RestAttribute(string method)
{
Method = …Run Code Online (Sandbox Code Playgroud) 如何在子类的重写方法中访问超类方法的局部变量?
class Foo(object):
def foo_method(self):
x = 3
class Bar(Foo):
def foo_method(self):
super().foo_method()
print(x) # Is there a way to access x, besides making x an attribute of the class?
Run Code Online (Sandbox Code Playgroud)
下面的代码给出了 NameError: name 'x' is not defined
bar = Bar()
bar.foo_method()
Run Code Online (Sandbox Code Playgroud)
这不足为奇,可以通过创建x实例属性来修复它,但是可以x直接访问Bar.foo_method吗?
在汇编中制作函数时,使用这么少的寄存器会遇到问题-这些寄存器内部有什么阻止我使用它们的?我可以在编码时将其内容复制到参数中并在退出功能之前重置它以不破坏其用途吗?
假设我有一个名为 func 的函数:
PROC func:
;Bla bla
ret
ENDP func
Run Code Online (Sandbox Code Playgroud)
现在,假设我使用 register ax,bx例如,为了保存它们的初始值,我将它们推送到函数内部的堆栈中。
现在的问题是:在创建堆栈帧之前推送寄存器之间是否有很大的不同:
PROC func:
push bp
push ax
push bx
mov bp, sp
;Bla bla
ret
ENDP func
Run Code Online (Sandbox Code Playgroud)
还是之后?
PROC func:
push bp
mov bp, sp
push ax
push bx
;Bla bla
ret
ENDP func
Run Code Online (Sandbox Code Playgroud)
我应该在我的程序中使用什么?一种方法比另一种更好或更“正确”吗?因为我目前使用第一种方法。
我有由 frameCount 调用的汇编代码,需要返回 frameCount,但不确定如何检索(然后导航)前一帧的指针引用。
获取FP.s
.globl getFP
getFP:
movq %rbp, %rax
ret
Run Code Online (Sandbox Code Playgroud)
帧数
int frameCount() {
int count = 0;
uint64_t fp = getFP();
uint64_t *sp = &fp;
// how do I get the pointer/offset to pointer to the previous stack frame from here?
return count;
}
Run Code Online (Sandbox Code Playgroud)
我已经更新了 frameCount 函数以包含一个遍历堆栈帧链接列表的循环,但是在调用 frameCount 时出现分段错误。
主文件
#include <stdio.h>
#include <inttypes.h>
#include "framecount.c"
int main() {
printf("Number of Frames: %d\n", frameCount());
return(0);
}
Run Code Online (Sandbox Code Playgroud)
帧数
#include <stdio.h>
#include <inttypes.h>
uint64_t* getFP();
int frameCount() { …Run Code Online (Sandbox Code Playgroud) 这是一个简单的函数
#include <stdio.h>
int foo() {
int a = 3;
int b = 4;
int c = 5;
return a * b * c;
}
int main() {
int a = foo();
}
Run Code Online (Sandbox Code Playgroud)
foo() 的程序集看起来像
foo:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-4], 3
mov DWORD PTR [rbp-8], 4
mov DWORD PTR [rbp-12], 5
mov eax, DWORD PTR [rbp-4]
imul eax, DWORD PTR [rbp-8]
imul eax, DWORD PTR [rbp-12]
pop rbp
ret
Run Code Online (Sandbox Code Playgroud)
从 中可以看出rbp - N,内部堆栈框架正在被修改。那么,为什么没有 …
我正在使用LLVM并且对它有些新意.
我无法通过降低堆栈帧来确定LLVM的含义.有人可以解释一下它是什么吗?
任何帮助表示赞赏
给定堆栈框架和变量名称,如何判断该变量是否为非局部变量?例:
import inspect
def is_nonlocal(frame, varname):
# How do I implement this?
return varname not in frame.f_locals # This does NOT work
def f():
x = 1
def g():
nonlocal x
x += 1
assert is_nonlocal(inspect.currentframe(), 'x')
g()
assert not is_nonlocal(inspect.currentframe(), 'x')
f()
Run Code Online (Sandbox Code Playgroud) stack-frame ×10
assembly ×4
c ×3
callstack ×2
python ×2
python-3.x ×2
x86 ×2
x86-64 ×2
backtrace ×1
c# ×1
ios ×1
llvm ×1
methodinfo ×1
namespaces ×1
red-zone ×1
reflection ×1
scope ×1
stack-memory ×1
stack-trace ×1