[gdb/riscv-tdep] Avoid scan invalid prologue

Message ID a56cc693a28341ac226c3ef2697475dffaedf494.17784bf6.0c49.4f04.b1d5.4d3b265408ef@feishu.cn
State New
Headers
Series [gdb/riscv-tdep] Avoid scan invalid prologue |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_check--master-arm fail Patch failed to apply

Commit Message

梁镇 Nov. 2, 2023, 9:02 a.m. UTC
  When running gdb connect to openocd, I run into:
(gdb) set debug remote 1
(gdb) set *0x40000000=0
[remote] Sending packet: $m0,2#fb
[remote] Packet received: 7310
[remote] Sending packet: $m2,2#fd
[remote] Packet received: 4030
[remote] Sending packet: $X40000000,0:#72
[remote] Packet received: OK
[remote] check_binary_download: binary downloading supported by target
[remote] Sending packet: $X40000000,4:\000\000\000\000#76
[remote] Packet received: OK
[remote] Sending packet: $g#67
[remote] Packet received: 0000000000000000820a000000000000201e002000000000000000000000000000000000000000002500000000000000601f002000000000b11e0020000000000000000000000000781e0020000000008d01000000000000781e0020000000000100000000000000f40100000000000000040000000000000100000000000000070000000000000000000000000000000100000000000000ff2f000000000000f4010000000000001400011000000000000001100000000000000000000000000d000000000000004000000000000000000000000000000000000000000000006e89000000000000981f00200000000043000000000000000000000000000000 [16 bytes omitted]
[remote] Sending packet: $m0,2#fb
[remote] Packet received: 7310
[remote] Sending packet: $m2,2#fd
[remote] Packet received: 4030

It means that this operation has performed some invalid operations, and in some cases it will cause the system to slow down.

When I file and load xxx.elf into, run again:
(gdb) set *0x40000000=0
[remote] Sending packet: $X40000000,4:\000\000\000\000#76
[remote] Packet received: OK
[remote] Sending packet: $g#67
[remote] Packet received: 0000000000000000820a000000000000201e002000000000000000000000000000000000000000002500000000000000601f002000000000b11e0020000000000000000000000000781e0020000000008d01000000000000781e0020000000000100000000000000f40100000000000000040000000000000100000000000000070000000000000000000000000000000100000000000000ff2f000000000000f4010000000000001400011000000000000001100000000000000000000000000d000000000000004000000000000000000000000000000000000000000000006e89000000000000981f00200000000043000000000000000000000000000000 [16 bytes omitted]

When I debug gdb hit in riscv_frame_cache:
{
...
/* Scan the prologue, filling in the cache.  */
start_addr = get_frame_func (this_frame);
pc = get_frame_pc (this_frame);
riscv_scan_prologue (gdbarch, start_addr, pc, cache);

start_addr -> get_frame_func -> get_frame_func_if_available -> get_pc_function_start (pc = 0x40000000) -> return 0

So that scan invalid prologue, filling in the cache.

---
 gdb/riscv-tdep.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

--•
2.25.1


This message and any attachment are confidential and may be privileged or otherwise protected from disclosure. If you are not an intended recipient of this message, please delete it and any attachment from your system and notify the sender immediately by reply e-mail. Unintended recipients should not use, copy, disclose or take any action based on this message or any information contained in this message. Emails cannot be guaranteed to be secure or error free as they can be intercepted, amended, lost or destroyed, and you should take full responsibility for security checking. 
 
本邮件及其任何附件具有保密性质,并可能受其他保护或不允许被披露给第三方。如阁下误收到本邮件,敬请立即以回复电子邮件的方式通知发件人,并将本邮件及其任何附件从阁下系统中予以删除。如阁下并非本邮件写明之收件人,敬请切勿使用、复制、披露本邮件或其任何内容,亦请切勿依本邮件或其任何内容而采取任何行动。电子邮件无法保证是一种安全和不会出现任何差错的通信方式,可能会被拦截、修改、丢失或损坏,收件人需自行负责做好安全检查。
  

Patch

diff --git a/gdb/riscv-tdep.c b/gdb/riscv-tdep.c
index 3a2891c2c92..ebd1fc950e0 100644
--- a/gdb/riscv-tdep.c
+++ b/gdb/riscv-tdep.c
@@ -3723,6 +3723,8 @@  riscv_frame_cache (frame_info_ptr this_frame, void **this_cache)
•
   /* Scan the prologue, filling in the cache.  */
   start_addr = get_frame_func (this_frame);
+  if (start_addr == 0)
+    return NULL;
   pc = get_frame_pc (this_frame);
   riscv_scan_prologue (gdbarch, start_addr, pc, cache);
•
@@ -3776,7 +3778,8 @@  riscv_frame_this_id (frame_info_ptr this_frame,
   try
     {
       cache = riscv_frame_cache (this_frame, prologue_cache);
-      *this_id = cache->this_id;
+      if (cache)
+-->    *this_id = cache->this_id;
     }
   catch (const gdb_exception_error &ex)
     {