0

I am trying to use a python script to read from memory through trace32. I've found the following document: https://www2.lauterbach.com/pdf/api_remote.pdf

I managed the following code:

local_buffer = ctypes.POINTER(ctypes.c_uint32)
t32api.T32_ReadMemory(byteAddress=addr, access=0x0, buffer=local_buffer, size=size)
print(local_buffer)

Of course there is an initialization of the t32api object - that works. But the code I pasted here causes the following python error:

Traceback (most recent call last):
  File "<path_to_python_script>", line 599, in <module>
    main()
  File "<path_to_python_script>", line 590, in main
    process()
  File "<path_to_python_script>", line 269, in process
    NumberOfEmpr = read_addr(0xf0083100)
  File "<path_to_python_script>", line 148, in read_addr
    return read_addr_t32(addr, size)
  File "<path_to_python_script>", line 137, in read_addr_t32
    t32api.T32_ReadMemory(byteAddress=addr, access=0x0, buffer=local_buffer, size=size)
OSError: exception: access violation writing 0xXXXXXXXX

Of course 0xXXXXXXXX is a placeholder to some address, I am guessing it is the address of local_buffer.

If anyone knows how to fix this I will be thankful.

Hezi Yovel
  • 105
  • 1
  • 7
  • Here is a guide how to *write* to memory via the classic remote API from Python: https://stackoverflow.com/a/47717395/4727717 (Reading goes basically the same way and is briefly shown in the bottom of the answer.) – Holger Sep 25 '20 at 10:41

1 Answers1

0

The problem is that the buffer pointer you give to T32_ReadMemory() should not only be a pointer but should be a pointer to existing memory.

So you need to change

local_buffer = ctypes.POINTER(ctypes.c_uint32)
t32api.T32_ReadMemory(byteAddress=addr, access=0x0, buffer=local_buffer, size=size)
print(local_buffer)

to

local_buffer = (ctypes.c_ubyte * size)()
t32api.T32_ReadMemory(byteAddress=addr, access=0x0, buffer=local_buffer, size=size)
print(local_buffer)

Two remarks independent of your problem:

  1. I would suggest to use T32_ReadMemoryObj() instead of T32_ReadMemory().
  2. Check trace32_and_python.pdf. New TRACE32 version include a Python module which you can just import.
dev15
  • 665
  • 3
  • 14
  • I wish I had this new TRACE32 version. Looks like it would have made life much easier. – Hezi Yovel Sep 27 '20 at 05:45
  • Also, can you add an example of how to use T32_ReadMemoryObj()? I tried it but couldn't figure out how to use it. – Hezi Yovel Sep 27 '20 at 05:46
  • There are C examples on how to use it in api_remote.pdf (or api_remote_c.pdf). If you have any problems post your code in a new question and I'll try to help you. – dev15 Sep 29 '20 at 06:05
  • I know about those examples, I just don't know how to "translate" them to python. Especially the internal t32 objects. Anyway I'll post a new question. – Hezi Yovel Sep 29 '20 at 08:11