我试图使用python自动化Trace32函数.我正在尝试使用T32_WriteMemory()函数将值写入内存地址.有人可以帮助我如何继续这个功能吗?
以下是T32 Api pdf(api_remote.pdf)的参考:
int T32_WriteMemory(
uint32_t byteAddress
int access,
uint8_t *buffer,
int byteSize
);
Run Code Online (Sandbox Code Playgroud)
byteAddress:要开始写入的目标内存地址
access:内存访问说明符
缓冲:输出
byteSize:要读取的字节数
要通过TRACE32从python脚本写入内存,请执行以下操作:
详细地:
1.在TRACE32上启用远程控制端口
将以下行添加到TRACE32配置文件(config.t32):
RCL=NETASSIST
PORT=20000
Run Code Online (Sandbox Code Playgroud)
在该块之前和之后必须有空行.当然,您也可以为端口选择其他号码.使用这些设置启动的TRACE32 GUI将打开UDP/IP端口,以侦听对远程控制TRACE32的可能请求.
2.获取用于TRACE32远程访问的已编译共享库
您可以在TRACE32安装中找到所需的共享库<T32>/demo/api/capi/dll(在Windows上,这通常是C:\ t32\demo\api\capi\dll)您也可以在http://www.lauterbach.com/scripts下载它. html(在那里搜索"capi"或"python")
对于Windows,有t32api.dll和t32api64.dll.对于Linux,有t32api.so或t32api64.so.(t32api64.*用于64位python解释器,而t32api.*用于32位python解释器.)
在下面我假设您将t32api库放在与python脚本相同的目录中.
3.在python脚本中加载t32api库
import platform
import ctypes
ostype = ctypes.sizeof(ctypes.c_voidp) * 8
if (platform.system()=='Windows') or (platform.system()[0:6]=='CYGWIN') :
# WINDOWS
t32api = ctypes.CDLL("./t32api64.dll" if ostype==64 else "./t32api.dll")
elif platform.system()=='Darwin' :
# Mac OS X
t32api = ctypes.CDLL("./t32api.dylib")
else :
# Linux
t32api = ctypes.CDLL("./t32api64.so" if ostype==64 else "./t32api.so")
Run Code Online (Sandbox Code Playgroud)
在t32api-library与python脚本不在同一目录下,你必须适应路径.
4.通过t32api库连接到TRACE32 GUI
# Declare UDP/IP socket of the TRACE32 instance to access
t32api.T32_Config(b"NODE=",b"localhost")
t32api.T32_Config(b"PORT=",b"20000")
# Connect to TRACE32
error = t32api.T32_Init()
if error != 0 :
sys.exit("Can't connect to TRACE32!")
# Select to debugger component of TRACE32 (B:: prompt)
t32api.T32_Attach(1)
Run Code Online (Sandbox Code Playgroud)
如果您在步骤1中选择了另一个端口,则必须相应地更改该行t32api.T32_Config(b"PORT=",b"20000").
5.声明T32_WriteMemory的参数类型
t32api.T32_WriteMemory.argtypes = [ctypes.c_uint32, ctypes.c_int, ctypes.c_char_p, ctypes.c_int]
t32api.T32_WriteMemory.restype = ctypes.c_int
Run Code Online (Sandbox Code Playgroud)
第一行告诉python T32_WriteMemory是一个C函数,它有四个参数,类型为uint32_t,int,char*和int.第二行告诉python返回值是int类型.
6.创建一个字节缓冲区,其中包含要在目标字节序中写入的数据
wdata = 0x12345678 # <- Your own value here !
wbuffer = wdata.to_bytes(4, byteorder='little')
Run Code Online (Sandbox Code Playgroud)
这里,0x12345678是我选择写的值.我通过TRACE32调试的目标CPU使其内存以little-endian字节顺序组织.所以我在第二行选择了"byteorder ='little'"来创建字节缓冲区.
7.从t32api库中调用T32_WriteMemory
# Set parameters for the memory access
byteAddress = 0x46c8 # <- Your address here !
access = 0x20
byteSize = 4 # amount of bytes to write (e.g. 4 bytes)
# Write data to memory via TRACE32
error = t32api.T32_WriteMemory(byteAddress, access, wbuffer, byteSize)
if error != 0 :
print("write failed")
Run Code Online (Sandbox Code Playgroud)
access = 0x20 在CPU运行时启用内存访问(如果在TRACE32中启用了SYStem.MemAccess且CPU支持运行时访问),否则将其设置为0,或者查看TRACE32 API文档(api_remote.pdf)以获取其他值.
8.在结束脚本之前关闭与TRACE32的连接
t32api.T32_Exit()
Run Code Online (Sandbox Code Playgroud)
读内存是这样的:
# Declare argument types of T32_T32_ReadMemory
t32api.T32_T32_ReadMemory.argtypes = [ctypes.c_uint32,ctypes.c_int, ctypes.c_char_p,ctypes.c_int]
t32api.T32_T32_ReadMemory.restype = ctypes.c_int
# Create a buffer for the result
rbuffer = ctypes.create_string_buffer(byteSize)
# Request memory content via TRACE32
error = t32api.T32_ReadMemory(byteAddress, access, rbuffer, byteSize)
if error == 0 :
# Extract 32-bit value in little endian order from the buffer
data32 = int.from_bytes(rbuffer[0:4], byteorder='little')
print("read 0x%08X from D:0x%08X" % (data32, byteAddress))
else:
print("read failed")
Run Code Online (Sandbox Code Playgroud)