RTEMS | Add GDB debug script section to executable (#5035)

Suraj Kumar (@the.m3chanic) gitlab at rtems.org
Wed Jun 26 21:46:38 UTC 2024




Suraj  Kumar commented: https://gitlab.rtems.org/rtems/rtos/rtems/-/issues/5035#note_108138


Using C to generate the object code for us seems to be the best way to go forward with this, to let us not worry about architecture specifics. 

Currently, the idea is to add the code snippet containing the Python code to a C file which is universally accessed by the linker for all links. A starting point is: https://gitlab.rtems.org/rtems/rtos/rtems/-/blob/main/cpukit/sapi/src/exinit.c

The following modifications can be made to the file to reflect the desired changes: 

1. Add macro definition 

   ```c
   #define DEFINE_GDB_PY() \
     asm( \
       ".pushsection \".debug_gdb_scripts\", \"MS\", at progbits,1\n" \
       ".byte 4\n" \
       ".ascii \"gdb.inlined-script\\n\"\n" \
       ".ascii \"import sys\\n\"\n" \
       ".ascii \"import os.path\\n\"\n" \
       ".ascii \"sys.path.append(os.path.join(gdb.PYTHONDIR, 'rtems'))\\n\"\n" \
       ".ascii \"print(sys.path)\\n\"\n" \
       ".ascii \"print(gdb.PYTHONDIR)\\n\"\n" \
       ".ascii \"import rtems.stdcxx as stdcxx\\n\"\n" \
       ".byte 0\n" \
       ".popsection\n" \
      )
   ```
2. Add macro call in function `rtems_initialize_data_structures()`

After these modifications, build the BSP again and build this application

```cpp
int main() {
  DEFINE_GDB_PY();
  std::cout << "Hello World" << std::endl;

  auto v = std::vector<int>({1, 2, 3, 5, 7, 8});
  for (auto i: v) {
    std::cout << i << ' ';
  }
  std::cout << std::endl;

  return 0;
}
```

After that, running this command shows us the existence of `.unexpected_sections` (which exists because we didn't tell the linker about the custom section). 

```
arm-rtems6-readelf -S <path_to_exe>
```

We can dump the section with 

```
 arm-rtems6-objdump -s -j .unexpected_sections <path_to_exe>
```

to verify the existence of said script.

To make the section show up as a valid section to test in GDB, add this line to `linkcmds.base` present in `{prefix}/rtems/6/arm-rtems6/xilinx_zynq_a9_qemu/lib/linkcmds.base`:

```
.debug_gdb_scripts 0 : { *(.debug_gdb_scripts) }
```

And rebuild the file

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/issues/5035#note_108138
You're receiving this email because of your account on gitlab.rtems.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/bugs/attachments/20240626/016c6a12/attachment-0001.htm>


More information about the bugs mailing list