一.参考文章
GDB 单步调试汇编 - 张雅宸 - 博客园 (cnblogs.com)https://www.cnblogs.com/zhangyachen/p/9227037.html
二.技术总结
1.rbp/rsp寄存器
2.数据断点 —— watch *0x7fffffffe4a8 或者 watch var_name 监控对应内存位置被修改。
3. fs寄存器
-
FS and GS are clones of ES, the extra segment.
-
FS and GS both are just additional segments, no specialty here.
-
Names FS and GS come from the fact that they were created after ES: E, F, G.
-
They exist only in the 386 and later x86 CPUs.
-
Extra segments ES, FS, and GS can be used for both data or code.
三.场景复现 —— 源代码编写
1.顶层main.c
extern void func1(); // defined in libcomp1.so
void main() {
func1();
}
2.顶层CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(test)
#set(CMAKE_BUILD_TYPE DEBUG)
#指定编译选项
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall")
# 指定头文件路径
set(INC_DIR /home/ldeng/Documents/stack_test/comp1)
#指定头文件目录
include_directories(${INC_DIR})
#生成目标文件
add_executable(test main.c)
target_link_libraries(test
"/home/ldeng/Documents/stack_test/comp1/build/libcomp1.so"
"/home/ldeng/Documents/stack_test/comp2/build/libcomp2.so"
)
3.comp1/lua.h
#ifndef __LUA_H__
#define __LUA_H__
#define LUA_IDSIZE 60
struct lua_Debug{
int event;
const char *name;
const char *namewhat;
const char *what;
const char *source;
int currentline;
int nups;
int linedefined;
int lastlinedefined;
char short_src[LUA_IDSIZE];
int i_ci; //4字节
};
#endif
4.comp1/comp1.c
#include "lua.h"
extern void func2(struct lua_Debug *a, char *b, int c); // defined in libcomp2.so
void func1() {
struct lua_Debug a = {0};
char *b = 0x12345678;
int c = 0xFFFFFFFF;
func2(&a, b, c);
return;
}
5.comp1/CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(comp1)
add_library(comp1 SHARED comp1.c lua.h)
6.comp2/lua.h
#ifndef __LUA_H__
#define __LUA_H__
#define LUA_IDSIZE 60
struct lua_Debug{
int event;
const char *name;
const char *namewhat;
const char *what;
const char *source;
int currentline;
int nups;
int linedefined;
int lastlinedefined;
char short_src[LUA_IDSIZE];
int *i_ci; //8字节
};
#endif
6.comp2/comp2.c
#include "lua.h"
void func2(struct lua_Debug *a, char *b, int c) {
a->i_ci = 1;
return;
}
7.comp2/CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(comp2)
add_library(comp2 SHARED comp2.c lua.h)
四.场景复现 —— gdb调试
五.堆栈结构分析实例
1.源代码
int sum(int x,int y){
return x+y;
}
int main(){
int x=10;
int y=20;
int c=sum(x,y);
return 0;
}
2.进入sum后的堆栈布局
3.main函数即将结束时的堆栈