Adding a New Target
gdb/config/arch/ttt.mt
gdb/ttt-tdep.c
gdb/arch-tdep.c
gdb/arch-tdep.h registers, stack ...的描述, it is included by `ttt-tdep.h'.
如果是为一个新的os添加支持(cpu已存在),则只需要添加 `config/tm-os.h' ,描述此os的特殊facilities即可,比如extra symbol table info; the breakpoint instruction needed; 等等),然后写一个 `arch/tm-os.h',就include`tm-arch.h'和config/tm-os.h即可.
Target Descriptions
Target Descriptions Implementation
Adding Target Described Register Support
Target descriptions通过体系特定的支持函数,可以报告更多的寄存器到target.
一个target description或者没有寄存器,或者有完整的寄存器集.
如果 tdesc_has_registers 返回1, 代表这个description包含寄存器,architecture's gdbarch_init函数应该完成如下功能:
- 通过tdesc_data_alloc 分配内存(在搜索匹配的gdbarch后者分配新的之前进行)
- 通过tdesc_find_feature,根据name确定标准属性集.
- 通过tdesc_numbered_register 和 tdesc_numbered_register_choices 确定standard features需要的registers.
- 如果需要的feature缺失,返回NULL, 如standard feature缺失必要寄存器,返回NULL.
- 释放分配的数据,除非调用了tdesc_use_registers.
- Call set_gdbarch_num_regs as usual, with a number higher than any fixed number passed to tdesc_numbered_register.
- 返回新创建的gdbarch之前,调用 tdesc_use_registers.
调用tdesc_use_registers后, 这个architecture的register_name, register_type, 和 register_reggroup_p routines将不会被调用;这些信息将从target description获取. num_regs 或许增加,因为可能有新的寄存器加进来.
Pseudo-registers require some extra care:
- Using tdesc_numbered_register allows the architecture to give constant register numbers to standard architectural registers, e.g. as an enum in `arch-tdep.h'. But because pseudo-registers are always numbered above num_regs, which may be increased by the description, constant numbers can not be used for pseudos. They must be numbered relative to num_regs instead.
- The description will not describe pseudo-registers, so the architecture must call set_tdesc_pseudo_register_name, set_tdesc_pseudo_register_type, and set_tdesc_pseudo_register_reggroup_p to supply routines describing pseudo registers. These routines will be passed internal register numbers, so the same routines used for the gdbarch equivalents are usually suitable.
http://sourceware.org/ml/gdb-patches/2000-07/msg00127.html
Target Vector Definition
Managing Execution State
completely inactive
active but not running:pushed, but not connected to a fully manifested inferior
completely active
persistent connections to a target even when the target has exited or not yet started.如:
使用target sim连接到simulator是,并不创建进程,寄存器和内存在run前都不可访问.类似的, kill之后,程序也不能继续运行.但是这两种情况下GDB仍然连接到simulator.
如果target仅仅支持complete activation,则在其to_open函数中,应该使用push_target将自己压入堆栈(rfc), 在其to_mourn_inferior函数中通过unpush_target将自己出栈.
如果target支持partial和complete activation,就不应该在to_mourn_inferior中出栈(但是仍然需要在to_open中入栈),而应调用 target_mark_running或者target_mark_exited. 只要任何时候inferior fully active就应该调用target_mark_running(如在to_create_inferior和to_attach中), 而inferior inactive(to_mourn_inferior)时就掉用 target_mark_exited.在to_kill中,应该调用target_mourn_inferior确保target进入inactive 状态.
Existing Targets
File Targets
executables 和 core files 都有target vectors.
Standard Protocol and Remote Stubs
GDB 文件remote.c 通过串口和target system通讯.GDB 提供一系列的stubs: stub集成到target系统中作为GDB的通讯断点.其名称一般是*-stub.c.
stub中的trap处理代码假设trap_low的接口如下:
- %l1 和 %l2 分别包含pc 和 npc;
- traps 以禁止;
- 已经在正确的trap window(rfc).
满足以上条件时,可以从hw trap vector直接跳到stub.一般情况下stub使用的trap,操作系统是不使用的(一般是不可恢复性错误),也没有共享问题(有也可以支持). 最重要的trap之一ta 1(rfc):用于单步执行或者breakpoint.
这里有个remote stub的参考文档:http://sca.uwaterloo.ca/coldfire/gcc-doc/docs/porting_4.html
Native Debugging
如下文件控制native support的配置:
许多通用函数可以为很多系统共享,如果你的系统可以使用这些函数,使用NATDEPFILES定义所需要的.o即可.否则,你需要重新实现这些函数,并放到xyz-nat.c中.
`inftarg.c' 包含
Native core file Support
当 在一个新的OS上运行GDB native时,为了确保能够调试core file,就要写代码支持解析这个OS的core file,或者修改`bfd/trad-core.c'. 首先,定义一个寄存器结构来描述OS Core file中的寄存器(比如core file 的u-area),将定义core file header的头文件包含进来(u-area or a struct core). 然后,修改trad_unix_core_file_p 使用这些信息建立core file总的data segment, stack segment和其他段的信息(shared library ,control information), 以及"registers" segment(如果有两组寄存器,比如integer和float),即 "reg2" segment. 这些section 的信息使BFD知道如何从corefile读取这些信息. 然后,修改GDB:定义fetch_core_registers,使用通用的core-aout.c,或者自己的`xyz-nat.c'. GDB使用这个函数来将寄存器的值转移到GDB的"registers" array中.
`nm-system.h'中可能存在的configure:
[Note that this is incorrectly defined in `xm-system.h' files currently.]
Support Libraries BFD
access to sections of files
广泛用于binutils.提供反汇编支持.
readlinelibiberty
此库提供一系列的函数用于补足,替换和扩展操作系统提供的功能.GDB使用很多这个库提供的特性:如 C++ demangler,IEEE
obstacks in GDB
主要用于object files管理. 每个obj文件有一个对应的obstack,许多对象在obstack上分配:....(略)
Regex conditionals.
Array Containers
see `vec.h' (略)
例子:
DEF_VEC_P(tree); // non-managed tree vector.struct my_struct {
VEC(tree) *v; // A (pointer to) a vector of tree pointers.
};
struct my_struct *s;
if (VEC_length(tree, s->v)) { we have some contents }
VEC_safe_push(tree, s->v, decl); // append some decl onto the end
for (ix = 0; VEC_iterate(tree, s->v, ix, elt); ix++)
{ do something with elt }其他接口函数:
VEC_length
Cleanups
Cleanups 是GDB使用的一个通用机制.cleanup 会在以后的合适时间执行:命令完成,error处理等.
使用方式:
struct cleanup *old_chain;
Per-architecture module data
向gdbarch 添加module specific per-architecture data-pointers的机制:(see GDBINT)
Function:These functions return a struct gdbarch_data that is used to identify the per-architecture data-pointer added for that module.
The per-architecture data-pointer is accessed using the function:
Function:Wrapping Output Lines
GDB Coding Standards以下部分请参考GDBINT.
ISO C
GDB assumes an ISO/IEC 9899:1990 (a.k.a. ISO C90) compliant compiler.
GDB does not assume an ISO C or POSIX compliant C library.
GDB does not use the functions malloc, realloc, calloc, free and asprintf.
GDB uses the functions xmalloc, xrealloc and xcalloc....
Compiler Warnings
所有操作target的函数必须使用 target_ops vector. 不能假定host和target是同一台机器(除了native),尤其不能假定target机器的头文件可以在host上使用.
more detail see GDBINT在新的machine上编译GDB需要很多配置工作,需要修改许多头文件和配置脚本.假定新的host是xyz (e.g., `sun4'), 其配置名是 arch-xvend-xos (e.g., `sparc-sun-sunos4') :
- 在根目录,编辑 `config.sub' 添加 arch, xvend, and xos 到architecture列表.添加xyz作为arch-xvend-xos的别名.运行以下命令测试修改:
./config.sub xyz
./config.sub arch-xvend-xos
(或许需要port BFD)
- 配置GDB:编辑`gdb/configure.host'时期认识你的系统,设置 gdb_host 为 xyz, 编辑`gdb/configure.tgt',设置gdb_target为合适值,如xyz.
- 最后,开始写对应的host-, native-,
target-dependent的 `.h' 和 `.c' 文件.......(不少啊).
Other
为完整性,以下部分没有阅读,这些部分描述GDB的代码配置管理部分内容,以及代码阅读的一些提示:17. Start of New Year Procedure
A. GDB Currently available observers
B. GNU Free Documentation License
转自:http://hi.baidu.com/systemsoftware/blog/item/7b33333d69cf9e0ebba16772