[PATCH] gdb: LoongArch: Add show-debug-regs maintenance command

Hui Li lihui@loongson.cn
Tue Jul 30 11:41:05 GMT 2024


This patch register the command "maint set show-debug-regs on/off"
and make it settable by the user. If show-debug-regs is enabled,
the debug register values are shown when GDB inserts or removes a
hardware breakpoint or watchpoint. This is helpful for the use and
development of hardware watchpoints.

With this patch, the effect of this maintenance command as follows:

lihui@bogon:~$ cat test.c
int a = 0;
int main()
{
	a = 1;
	return 0;
}
lihui@bogon:~$ gcc -g test.c -o test
lihui@bogon:~$ gdb test
...
(gdb) watch a
Hardware watchpoint 1: a
(gdb) maint set show-debug-regs on
(gdb) r
Starting program: /home/lihui/test
...
...

prepare_to_resume thread 41525
...
insert_watchpoint (addr=0x12000803c, len=4, type=hw-write-watchpoint):
	BREAKPOINTs:
	BP0: addr=0x0, ctrl=0x00000000, ref.count=0
	BP1: addr=0x0, ctrl=0x00000000, ref.count=0
	BP2: addr=0x0, ctrl=0x00000000, ref.count=0
	BP3: addr=0x0, ctrl=0x00000000, ref.count=0
	BP4: addr=0x0, ctrl=0x00000000, ref.count=0
	BP5: addr=0x0, ctrl=0x00000000, ref.count=0
	BP6: addr=0x0, ctrl=0x00000000, ref.count=0
	BP7: addr=0x0, ctrl=0x00000000, ref.count=0
	WATCHPOINTs:
	WP0: addr=0x0, ctrl=0x00000000, ref.count=0
	WP1: addr=0x0, ctrl=0x00000000, ref.count=0
	WP2: addr=0x0, ctrl=0x00000000, ref.count=0
	WP3: addr=0x0, ctrl=0x00000000, ref.count=0
	WP4: addr=0x0, ctrl=0x00000000, ref.count=0
	WP5: addr=0x0, ctrl=0x00000000, ref.count=0
	WP6: addr=0x0, ctrl=0x00000000, ref.count=0
	WP7: addr=0x12000803c, ctrl=0x00000610, ref.count=1
...
remove_watchpoint (addr=0x12000803c, len=4, type=hw-write-watchpoint):
	BREAKPOINTs:
	BP0: addr=0x0, ctrl=0x00000000, ref.count=0
	BP1: addr=0x0, ctrl=0x00000000, ref.count=0
	BP2: addr=0x0, ctrl=0x00000000, ref.count=0
	BP3: addr=0x0, ctrl=0x00000000, ref.count=0
	BP4: addr=0x0, ctrl=0x00000000, ref.count=0
	BP5: addr=0x0, ctrl=0x00000000, ref.count=0
	BP6: addr=0x0, ctrl=0x00000000, ref.count=0
	BP7: addr=0x0, ctrl=0x00000000, ref.count=0
	WATCHPOINTs:
	WP0: addr=0x0, ctrl=0x00000000, ref.count=0
	WP1: addr=0x0, ctrl=0x00000000, ref.count=0
	WP2: addr=0x0, ctrl=0x00000000, ref.count=0
	WP3: addr=0x0, ctrl=0x00000000, ref.count=0
	WP4: addr=0x0, ctrl=0x00000000, ref.count=0
	WP5: addr=0x0, ctrl=0x00000000, ref.count=0
	WP6: addr=0x0, ctrl=0x00000000, ref.count=0
	WP7: addr=0x0, ctrl=0x00000000, ref.count=0

Hardware watchpoint 1: a

Old value = 0
New value = 1
main () at test.c:5
5		return 0;
(gdb)

Signed-off-by: Hui Li <lihui@loongson.cn>
---
 gdb/loongarch-linux-nat.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/gdb/loongarch-linux-nat.c b/gdb/loongarch-linux-nat.c
index 873628775e2..bc9927dd751 100644
--- a/gdb/loongarch-linux-nat.c
+++ b/gdb/loongarch-linux-nat.c
@@ -18,6 +18,7 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#include "cli/cli-cmds.h"
 #include "elf/common.h"
 #include "gregset.h"
 #include "inferior.h"
@@ -765,4 +766,19 @@ _initialize_loongarch_linux_nat ()
 {
   linux_target = &the_loongarch_linux_nat_target;
   add_inf_child_target (&the_loongarch_linux_nat_target);
+
+  /* Add a maintenance command to enable printing the LoongArch internal
+     debug registers mirror variables.  */
+  add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
+			   &show_debug_regs, _("\
+Set whether to show the LoongArch debug registers state."), _("\
+Show whether to show the LoongArch debug registers state."), _("\
+Use \"on\" to enable, \"off\" to disable.\n\
+If enabled, the debug registers values are shown when GDB inserts\n\
+or removes a hardware breakpoint or watchpoint, and when the inferior\n\
+triggers a breakpoint or watchpoint."),
+			   NULL,
+			   NULL,
+			   &maintenance_set_cmdlist,
+			   &maintenance_show_cmdlist);
 }
-- 
2.38.1



More information about the Gdb-patches mailing list