public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/6] gdb: Add basic support for LoongArch
@ 2021-12-09 11:35 Tiezhu Yang
  2021-12-09 11:35 ` [PATCH 1/6] gdb: LoongArch: Add initial target description support Tiezhu Yang
                   ` (7 more replies)
  0 siblings, 8 replies; 22+ messages in thread
From: Tiezhu Yang @ 2021-12-09 11:35 UTC (permalink / raw)
  To: gdb-patches
  Cc: Xuefeng Li, Chenghua Xu, Zhensong Liu, Qing zhang, Youling Tang,
	Tiezhu Yang

About one month ago, my workmate Zhensong Liu submitted a single gdb
patch [1] to support LoongArch which is a new RISC architecture, thanks
Zhensong for his great work.

It seems that the patch is too big to review, a patch series would be
easier to review after internal discussion.

This patchset only adds the minimal changes as simple as possible, the
basic command "run", "break", "continue", "next", "step" and "quit" can
be used to debug.

This is the first step, we will submit other more patches step by step
in the future, these small patches make an easily understood change that
can be verified by reviewers, any comments will be much appreciated.

Here are some test results:

    $ make check-gdb TESTS="gdb.base/a2-run.exp"
    $ cat gdb/testsuite/gdb.sum
    [...]
    PASS: gdb.base/a2-run.exp: run "a2-run" with no args
    PASS: gdb.base/a2-run.exp: no spurious messages at program exit
    PASS: gdb.base/a2-run.exp: run "a2-run" with arg
    PASS: gdb.base/a2-run.exp: run "a2-run" again with same args
    PASS: gdb.base/a2-run.exp: set args
    PASS: gdb.base/a2-run.exp: run after setting args to nil
    PASS: gdb.base/a2-run.exp: set args 6
    PASS: gdb.base/a2-run.exp: run "a2-run" again after setting args
    PASS: gdb.base/a2-run.exp: run "a2-run" with shell

		    === gdb Summary ===

    # of expected passes		9
    [...]

    $ make check-gdb TESTS="gdb.base/access-mem-running.exp"
    $ cat gdb/testsuite/gdb.sum
    [...]
    PASS: gdb.base/access-mem-running.exp: all-stop: continuing
    PASS: gdb.base/access-mem-running.exp: all-stop: get global_counter once
    PASS: gdb.base/access-mem-running.exp: all-stop: get global_counter twice
    PASS: gdb.base/access-mem-running.exp: all-stop: value changed
    PASS: gdb.base/access-mem-running.exp: all-stop: print global_var before writing
    PASS: gdb.base/access-mem-running.exp: all-stop: write to global_var
    PASS: gdb.base/access-mem-running.exp: all-stop: print global_var after writing
    PASS: gdb.base/access-mem-running.exp: all-stop: write to global_var again
    PASS: gdb.base/access-mem-running.exp: all-stop: b maybe_stop_here
    PASS: gdb.base/access-mem-running.exp: all-stop: breakpoint hits
    PASS: gdb.base/access-mem-running.exp: non-stop: continuing
    PASS: gdb.base/access-mem-running.exp: non-stop: get global_counter once
    PASS: gdb.base/access-mem-running.exp: non-stop: get global_counter twice
    PASS: gdb.base/access-mem-running.exp: non-stop: value changed
    PASS: gdb.base/access-mem-running.exp: non-stop: print global_var before writing
    PASS: gdb.base/access-mem-running.exp: non-stop: write to global_var
    PASS: gdb.base/access-mem-running.exp: non-stop: print global_var after writing
    PASS: gdb.base/access-mem-running.exp: non-stop: write to global_var again
    PASS: gdb.base/access-mem-running.exp: non-stop: b maybe_stop_here
    PASS: gdb.base/access-mem-running.exp: non-stop: breakpoint hits

		    === gdb Summary ===

    # of expected passes		20
    [...]

The LoongArch documents:
https://loongson.github.io/LoongArch-Documentation/README-EN.html

The LoongArch ELF ABI Documents:
https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html

The LoongArch binutils has been merged into trunk:
https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=e9a0721f8274

[1] https://sourceware.org/pipermail/gdb-patches/2021-November/183353.html

Tiezhu Yang (6):
  gdb: LoongArch: Add initial target description support
  gdb: LoongArch: Add initial baremetal support
  gdb: LoongArch: Add initial linux target support
  gdb: LoongArch: Add initial native linux support
  gdb: LoongArch: Add Makefile and configure
  gdb: LoongArch: Update ChangeLog and NEWS

 gdb/ChangeLog-2021                |  20 +++
 gdb/Makefile.in                   |   9 ++
 gdb/NEWS                          |   4 +
 gdb/arch/loongarch.c              |  47 +++++++
 gdb/arch/loongarch.h              |  30 +++++
 gdb/configure.host                |   3 +
 gdb/configure.nat                 |   4 +
 gdb/configure.tgt                 |  11 ++
 gdb/doc/gdb.texinfo               |  10 ++
 gdb/features/Makefile             |   4 +
 gdb/features/loongarch/base32.c   |  47 +++++++
 gdb/features/loongarch/base32.xml |  45 +++++++
 gdb/features/loongarch/base64.c   |  47 +++++++
 gdb/features/loongarch/base64.xml |  45 +++++++
 gdb/loongarch-linux-nat.c         | 175 ++++++++++++++++++++++++
 gdb/loongarch-linux-tdep.c        | 147 +++++++++++++++++++++
 gdb/loongarch-tdep.c              | 270 ++++++++++++++++++++++++++++++++++++++
 gdb/loongarch-tdep.h              |  62 +++++++++
 18 files changed, 980 insertions(+)
 create mode 100644 gdb/arch/loongarch.c
 create mode 100644 gdb/arch/loongarch.h
 create mode 100644 gdb/features/loongarch/base32.c
 create mode 100644 gdb/features/loongarch/base32.xml
 create mode 100644 gdb/features/loongarch/base64.c
 create mode 100644 gdb/features/loongarch/base64.xml
 create mode 100644 gdb/loongarch-linux-nat.c
 create mode 100644 gdb/loongarch-linux-tdep.c
 create mode 100644 gdb/loongarch-tdep.c
 create mode 100644 gdb/loongarch-tdep.h

-- 
2.1.0


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 1/6] gdb: LoongArch: Add initial target description support
  2021-12-09 11:35 [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
@ 2021-12-09 11:35 ` Tiezhu Yang
  2022-01-06 21:19   ` Tom Tromey
  2022-01-10 14:04   ` Andrew Burgess
  2021-12-09 11:35 ` [PATCH 2/6] gdb: LoongArch: Add initial baremetal support Tiezhu Yang
                   ` (6 subsequent siblings)
  7 siblings, 2 replies; 22+ messages in thread
From: Tiezhu Yang @ 2021-12-09 11:35 UTC (permalink / raw)
  To: gdb-patches
  Cc: Xuefeng Li, Chenghua Xu, Zhensong Liu, Qing zhang, Youling Tang,
	Tiezhu Yang

This commit adds initial target description support for LoongArch.

Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
Signed-off-by: Qing zhang <zhangqing@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 gdb/arch/loongarch.c              | 47 +++++++++++++++++++++++++++++++++++++++
 gdb/arch/loongarch.h              | 30 +++++++++++++++++++++++++
 gdb/doc/gdb.texinfo               | 10 +++++++++
 gdb/features/loongarch/base32.c   | 47 +++++++++++++++++++++++++++++++++++++++
 gdb/features/loongarch/base32.xml | 45 +++++++++++++++++++++++++++++++++++++
 gdb/features/loongarch/base64.c   | 47 +++++++++++++++++++++++++++++++++++++++
 gdb/features/loongarch/base64.xml | 45 +++++++++++++++++++++++++++++++++++++
 7 files changed, 271 insertions(+)
 create mode 100644 gdb/arch/loongarch.c
 create mode 100644 gdb/arch/loongarch.h
 create mode 100644 gdb/features/loongarch/base32.c
 create mode 100644 gdb/features/loongarch/base32.xml
 create mode 100644 gdb/features/loongarch/base64.c
 create mode 100644 gdb/features/loongarch/base64.xml

diff --git a/gdb/arch/loongarch.c b/gdb/arch/loongarch.c
new file mode 100644
index 0000000..bddabc7
--- /dev/null
+++ b/gdb/arch/loongarch.c
@@ -0,0 +1,47 @@
+/* Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   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 "defs.h"
+#include "arch/loongarch.h"
+
+#include <../features/loongarch/base32.c>
+#include <../features/loongarch/base64.c>
+
+target_desc *
+loongarch_create_target_description (int rlen)
+{
+  int regnum = 0;
+  target_desc_up tdesc = allocate_target_description ();
+
+  set_tdesc_architecture (tdesc.get (), rlen == 64 ? "loongarch64" : "loongarch32");
+
+  if (rlen == 64)
+    regnum = create_feature_loongarch_base64 (tdesc.get (), regnum);
+  else
+    regnum = create_feature_loongarch_base32 (tdesc.get (), regnum);
+
+  return tdesc.release ();
+}
+
+target_desc *
+loongarch_get_base_target_description (int rlen)
+{
+  if (rlen == 64)
+    return loongarch_create_target_description (64);
+  else
+    return loongarch_create_target_description (32);
+}
diff --git a/gdb/arch/loongarch.h b/gdb/arch/loongarch.h
new file mode 100644
index 0000000..eddb685
--- /dev/null
+++ b/gdb/arch/loongarch.h
@@ -0,0 +1,30 @@
+/*
+   Copyright (C) 2021 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef ARCH_LOONGARCH_H
+#define ARCH_LOONGARCH_H
+
+#include <stdint.h>
+#include "elf/loongarch.h"
+#include "opcode/loongarch.h"
+
+struct target_desc;
+extern struct target_desc *loongarch_get_base_target_description (int rlen);
+extern struct target_desc *loongarch_create_target_description (int rlen);
+
+#endif
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d0c5bcf..12ee3ee 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -46172,6 +46172,7 @@ registers using the capitalization used in the description.
 * ARC Features::
 * ARM Features::
 * i386 Features::
+* LoongArch Features::
 * MicroBlaze Features::
 * MIPS Features::
 * M68K Features::
@@ -46400,6 +46401,15 @@ The @samp{org.gnu.gdb.i386.pkeys} feature is optional.  It should
 describe a single register, @samp{pkru}.  It is a 32-bit register
 valid for i386 and amd64.
 
+@node LoongArch Features
+@subsection LoongArch Features
+@cindex target descriptions, LoongArch Features
+
+The @samp{org.gnu.gdb.loongarch.base} feature is required for LoongArch
+targets.  It should contain the registers @samp{r0} through @samp{r31},
+@samp{pc}, and @samp{badvaddr}.  Either the architectural names (@samp{r0},
+@samp{r1}, etc) can be used, or the ABI names (@samp{zero}, @samp{ra}, etc).
+
 @node MicroBlaze Features
 @subsection MicroBlaze Features
 @cindex target descriptions, MicroBlaze features
diff --git a/gdb/features/loongarch/base32.c b/gdb/features/loongarch/base32.c
new file mode 100644
index 0000000..b6f2d06
--- /dev/null
+++ b/gdb/features/loongarch/base32.c
@@ -0,0 +1,47 @@
+/* THIS FILE IS GENERATED.  -*- buffer-read-only: t -*- vi:set ro:
+  Original: base32.xml */
+
+#include "gdbsupport/tdesc.h"
+
+static int
+create_feature_loongarch_base32 (struct target_desc *result, long regnum)
+{
+  struct tdesc_feature *feature;
+
+  feature = tdesc_create_feature (result, "org.gnu.gdb.loongarch.base");
+  tdesc_create_reg (feature, "r0", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r1", regnum++, 1, "general", 32, "code_ptr");
+  tdesc_create_reg (feature, "r2", regnum++, 1, "general", 32, "data_ptr");
+  tdesc_create_reg (feature, "r3", regnum++, 1, "general", 32, "data_ptr");
+  tdesc_create_reg (feature, "r4", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r5", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r6", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r7", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r8", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r9", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r10", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r11", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r12", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r13", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r14", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r15", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r16", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r17", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r18", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r19", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r20", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r21", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r22", regnum++, 1, "general", 32, "data_ptr");
+  tdesc_create_reg (feature, "r23", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r24", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r25", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r26", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r27", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r28", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r29", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r30", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "r31", regnum++, 1, "general", 32, "uint32");
+  tdesc_create_reg (feature, "pc", regnum++, 1, "general", 32, "code_ptr");
+  tdesc_create_reg (feature, "badvaddr", regnum++, 1, "general", 32, "code_ptr");
+  return regnum;
+}
diff --git a/gdb/features/loongarch/base32.xml b/gdb/features/loongarch/base32.xml
new file mode 100644
index 0000000..0afe81b
--- /dev/null
+++ b/gdb/features/loongarch/base32.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0"?>
+<!-- Copyright (C) 2021 Free Software Foundation, Inc.
+     Contributed by Loongson Ltd.
+
+     Copying and distribution of this file, with or without modification,
+     are permitted in any medium without royalty provided the copyright
+     notice and this notice are preserved.  -->
+
+<!DOCTYPE feature SYSTEM "gdb-target.dtd">
+<feature name="org.gnu.gdb.loongarch.base">
+  <reg name="r0" bitsize="32" type="uint32" group="general"/>
+  <reg name="r1" bitsize="32" type="code_ptr" group="general"/>
+  <reg name="r2" bitsize="32" type="data_ptr" group="general"/>
+  <reg name="r3" bitsize="32" type="data_ptr" group="general"/>
+  <reg name="r4" bitsize="32" type="uint32" group="general"/>
+  <reg name="r5" bitsize="32" type="uint32" group="general"/>
+  <reg name="r6" bitsize="32" type="uint32" group="general"/>
+  <reg name="r7" bitsize="32" type="uint32" group="general"/>
+  <reg name="r8" bitsize="32" type="uint32" group="general"/>
+  <reg name="r9" bitsize="32" type="uint32" group="general"/>
+  <reg name="r10" bitsize="32" type="uint32" group="general"/>
+  <reg name="r11" bitsize="32" type="uint32" group="general"/>
+  <reg name="r12" bitsize="32" type="uint32" group="general"/>
+  <reg name="r13" bitsize="32" type="uint32" group="general"/>
+  <reg name="r14" bitsize="32" type="uint32" group="general"/>
+  <reg name="r15" bitsize="32" type="uint32" group="general"/>
+  <reg name="r16" bitsize="32" type="uint32" group="general"/>
+  <reg name="r17" bitsize="32" type="uint32" group="general"/>
+  <reg name="r18" bitsize="32" type="uint32" group="general"/>
+  <reg name="r19" bitsize="32" type="uint32" group="general"/>
+  <reg name="r20" bitsize="32" type="uint32" group="general"/>
+  <reg name="r21" bitsize="32" type="uint32" group="general"/>
+  <reg name="r22" bitsize="32" type="data_ptr" group="general"/>
+  <reg name="r23" bitsize="32" type="uint32" group="general"/>
+  <reg name="r24" bitsize="32" type="uint32" group="general"/>
+  <reg name="r25" bitsize="32" type="uint32" group="general"/>
+  <reg name="r26" bitsize="32" type="uint32" group="general"/>
+  <reg name="r27" bitsize="32" type="uint32" group="general"/>
+  <reg name="r28" bitsize="32" type="uint32" group="general"/>
+  <reg name="r29" bitsize="32" type="uint32" group="general"/>
+  <reg name="r30" bitsize="32" type="uint32" group="general"/>
+  <reg name="r31" bitsize="32" type="uint32" group="general"/>
+  <reg name="pc" bitsize="32" type="code_ptr" group="general"/>
+  <reg name="badvaddr" bitsize="32" type="code_ptr" group="general"/>
+</feature>
diff --git a/gdb/features/loongarch/base64.c b/gdb/features/loongarch/base64.c
new file mode 100644
index 0000000..3ee2d9a
--- /dev/null
+++ b/gdb/features/loongarch/base64.c
@@ -0,0 +1,47 @@
+/* THIS FILE IS GENERATED.  -*- buffer-read-only: t -*- vi:set ro:
+  Original: base64.xml */
+
+#include "gdbsupport/tdesc.h"
+
+static int
+create_feature_loongarch_base64 (struct target_desc *result, long regnum)
+{
+  struct tdesc_feature *feature;
+
+  feature = tdesc_create_feature (result, "org.gnu.gdb.loongarch.base");
+  tdesc_create_reg (feature, "r0", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r1", regnum++, 1, "general", 64, "code_ptr");
+  tdesc_create_reg (feature, "r2", regnum++, 1, "general", 64, "data_ptr");
+  tdesc_create_reg (feature, "r3", regnum++, 1, "general", 64, "data_ptr");
+  tdesc_create_reg (feature, "r4", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r5", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r6", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r7", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r8", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r9", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r10", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r11", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r12", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r13", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r14", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r15", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r16", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r17", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r18", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r19", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r20", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r21", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r22", regnum++, 1, "general", 64, "data_ptr");
+  tdesc_create_reg (feature, "r23", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r24", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r25", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r26", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r27", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r28", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r29", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r30", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "r31", regnum++, 1, "general", 64, "uint64");
+  tdesc_create_reg (feature, "pc", regnum++, 1, "general", 64, "code_ptr");
+  tdesc_create_reg (feature, "badvaddr", regnum++, 1, "general", 64, "code_ptr");
+  return regnum;
+}
diff --git a/gdb/features/loongarch/base64.xml b/gdb/features/loongarch/base64.xml
new file mode 100644
index 0000000..b53479f
--- /dev/null
+++ b/gdb/features/loongarch/base64.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0"?>
+<!-- Copyright (C) 2021 Free Software Foundation, Inc.
+     Contributed by Loongson Ltd.
+
+     Copying and distribution of this file, with or without modification,
+     are permitted in any medium without royalty provided the copyright
+     notice and this notice are preserved.  -->
+
+<!DOCTYPE feature SYSTEM "gdb-target.dtd">
+<feature name="org.gnu.gdb.loongarch.base">
+  <reg name="r0" bitsize="64" type="uint64" group="general"/>
+  <reg name="r1" bitsize="64" type="code_ptr" group="general"/>
+  <reg name="r2" bitsize="64" type="data_ptr" group="general"/>
+  <reg name="r3" bitsize="64" type="data_ptr" group="general"/>
+  <reg name="r4" bitsize="64" type="uint64" group="general"/>
+  <reg name="r5" bitsize="64" type="uint64" group="general"/>
+  <reg name="r6" bitsize="64" type="uint64" group="general"/>
+  <reg name="r7" bitsize="64" type="uint64" group="general"/>
+  <reg name="r8" bitsize="64" type="uint64" group="general"/>
+  <reg name="r9" bitsize="64" type="uint64" group="general"/>
+  <reg name="r10" bitsize="64" type="uint64" group="general"/>
+  <reg name="r11" bitsize="64" type="uint64" group="general"/>
+  <reg name="r12" bitsize="64" type="uint64" group="general"/>
+  <reg name="r13" bitsize="64" type="uint64" group="general"/>
+  <reg name="r14" bitsize="64" type="uint64" group="general"/>
+  <reg name="r15" bitsize="64" type="uint64" group="general"/>
+  <reg name="r16" bitsize="64" type="uint64" group="general"/>
+  <reg name="r17" bitsize="64" type="uint64" group="general"/>
+  <reg name="r18" bitsize="64" type="uint64" group="general"/>
+  <reg name="r19" bitsize="64" type="uint64" group="general"/>
+  <reg name="r20" bitsize="64" type="uint64" group="general"/>
+  <reg name="r21" bitsize="64" type="uint64" group="general"/>
+  <reg name="r22" bitsize="64" type="data_ptr" group="general"/>
+  <reg name="r23" bitsize="64" type="uint64" group="general"/>
+  <reg name="r24" bitsize="64" type="uint64" group="general"/>
+  <reg name="r25" bitsize="64" type="uint64" group="general"/>
+  <reg name="r26" bitsize="64" type="uint64" group="general"/>
+  <reg name="r27" bitsize="64" type="uint64" group="general"/>
+  <reg name="r28" bitsize="64" type="uint64" group="general"/>
+  <reg name="r29" bitsize="64" type="uint64" group="general"/>
+  <reg name="r30" bitsize="64" type="uint64" group="general"/>
+  <reg name="r31" bitsize="64" type="uint64" group="general"/>
+  <reg name="pc" bitsize="64" type="code_ptr" group="general"/>
+  <reg name="badvaddr" bitsize="64" type="code_ptr" group="general"/>
+</feature>
-- 
2.1.0


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 2/6] gdb: LoongArch: Add initial baremetal support
  2021-12-09 11:35 [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
  2021-12-09 11:35 ` [PATCH 1/6] gdb: LoongArch: Add initial target description support Tiezhu Yang
@ 2021-12-09 11:35 ` Tiezhu Yang
  2022-01-06 21:27   ` Tom Tromey
  2022-01-10 14:28   ` Andrew Burgess
  2021-12-09 11:35 ` [PATCH 3/6] gdb: LoongArch: Add initial linux target support Tiezhu Yang
                   ` (5 subsequent siblings)
  7 siblings, 2 replies; 22+ messages in thread
From: Tiezhu Yang @ 2021-12-09 11:35 UTC (permalink / raw)
  To: gdb-patches
  Cc: Xuefeng Li, Chenghua Xu, Zhensong Liu, Qing zhang, Youling Tang,
	Tiezhu Yang

This commit adds initial baremetal support for LoongArch.

Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
Signed-off-by: Qing zhang <zhangqing@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 gdb/loongarch-tdep.c | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/loongarch-tdep.h |  62 ++++++++++++
 2 files changed, 332 insertions(+)
 create mode 100644 gdb/loongarch-tdep.c
 create mode 100644 gdb/loongarch-tdep.h

diff --git a/gdb/loongarch-tdep.c b/gdb/loongarch-tdep.c
new file mode 100644
index 0000000..3069752
--- /dev/null
+++ b/gdb/loongarch-tdep.c
@@ -0,0 +1,270 @@
+/* Target-dependent code for GNU/Linux LoongArch.
+
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   Contributed by Loongson Ltd.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   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 "defs.h"
+#include "arch-utils.h"
+#include "dwarf2/frame.h"
+#include "elf-bfd.h"
+#include "frame-unwind.h"
+#include "loongarch-tdep.h"
+#include "target-descriptions.h"
+#include "trad-frame.h"
+#include "user-regs.h"
+
+/* Implement the loongarch_skip_prologue gdbarch method.  */
+
+static CORE_ADDR
+loongarch_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
+{
+  CORE_ADDR func_addr;
+
+  /* See if we can determine the end of the prologue via the symbol table.
+     If so, then return either PC, or the PC after the prologue, whichever
+     is greater.  */
+  if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
+    {
+      CORE_ADDR post_prologue_pc
+	= skip_prologue_using_sal (gdbarch, func_addr);
+      if (post_prologue_pc != 0)
+	return std::max (pc, post_prologue_pc);
+    }
+
+  return 0;
+}
+
+/* Adjust the address downward (direction of stack growth) so that it
+   is correctly aligned for a new stack frame.  */
+static CORE_ADDR
+loongarch_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
+{
+  return align_down (addr, 16);
+}
+
+/* Generate, or return the cached frame cache for the loongarch frame unwinder.  */
+
+static struct trad_frame_cache *
+loongarch_frame_cache (struct frame_info *this_frame, void **this_cache)
+{
+  struct gdbarch *gdbarch = get_frame_arch (this_frame);
+  struct trad_frame_cache *cache;
+  CORE_ADDR pc;
+
+  if (*this_cache != NULL)
+    return (struct trad_frame_cache *) *this_cache;
+
+  cache = trad_frame_cache_zalloc (this_frame);
+  *this_cache = cache;
+
+  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
+  auto regs = tdep->regs;
+  trad_frame_set_reg_realreg (cache, regs.ra, -2 /* TF_REG_UNKNOWN */);
+  trad_frame_set_reg_realreg (cache, gdbarch_pc_regnum (gdbarch), regs.ra);
+
+  pc = get_frame_address_in_block (this_frame);
+  trad_frame_set_id (cache, frame_id_build_unavailable_stack (pc));
+
+  return cache;
+}
+
+/* Implement the this_id callback for loongarch frame unwinder.  */
+
+static void
+loongarch_frame_this_id (struct frame_info *this_frame, void **prologue_cache,
+			 struct frame_id *this_id)
+{
+  struct trad_frame_cache *info;
+
+  info = loongarch_frame_cache (this_frame, prologue_cache);
+  trad_frame_get_id (info, this_id);
+}
+
+/* Implement the prev_register callback for loongarch frame unwinder.  */
+
+static struct value *
+loongarch_frame_prev_register (struct frame_info *this_frame,
+			       void **prologue_cache, int regnum)
+{
+  struct trad_frame_cache *info;
+
+  info = loongarch_frame_cache (this_frame, prologue_cache);
+  return trad_frame_get_register (info, this_frame, regnum);
+}
+
+static const struct frame_unwind loongarch_frame_unwind = {
+  "loongarch prologue",
+  /*.type	   =*/NORMAL_FRAME,
+  /*.stop_reason   =*/default_frame_unwind_stop_reason,
+  /*.this_id	   =*/loongarch_frame_this_id,
+  /*.prev_register =*/loongarch_frame_prev_register,
+  /*.unwind_data   =*/NULL,
+  /*.sniffer	   =*/default_frame_sniffer,
+  /*.dealloc_cache =*/NULL,
+  /*.prev_arch	   =*/NULL,
+};
+
+static int
+loongarch_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
+{
+  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
+  auto regs = tdep->regs;
+
+  if (0 <= num && num < 32)
+    return regs.r + num;
+  else
+    return -1;
+}
+
+constexpr gdb_byte loongarch_default_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};
+typedef BP_MANIPULATION (loongarch_default_breakpoint) loongarch_breakpoint;
+
+static struct gdbarch *
+loongarch_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
+{
+  struct gdbarch *gdbarch;
+  struct loongarch_gdbarch_tdep tdep_instant, *tdep;
+  tdesc_arch_data_up tdesc_data;
+  const struct target_desc *tdesc = info.target_desc;
+  int i;
+  size_t regnum;
+
+  tdep = &tdep_instant;
+  memset (tdep, 0, sizeof (tdep));
+  memset (&tdep->regs, -1, sizeof (tdep->regs));
+
+  if (info.abfd != NULL
+      && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
+    {
+      int eflags = elf_elfheader (info.abfd)->e_flags;
+      unsigned char eclass = elf_elfheader (info.abfd)->e_ident[EI_CLASS];
+
+      if (eflags) /* Executable file  */
+        {
+          tdep->ef_abi = eflags & EF_LOONGARCH_ABI;
+        }
+      else /* Core file  */
+        {
+          if (eclass == ELFCLASS64)
+            tdep->ef_abi = EF_LOONGARCH_ABI_LP64;
+          else
+            tdep->ef_abi = EF_LOONGARCH_ABI_ILP32;
+        }
+    }
+  else
+    tdep->ef_abi = EF_LOONGARCH_ABI_LP64;
+
+  /* Check any target description for validity.  */
+  if (!tdesc_has_registers (tdesc))
+    tdesc = loongarch_get_base_target_description (
+      EF_LOONGARCH_IS_ILP32 (tdep->ef_abi) ? 32 : 64);
+
+  int valid_p = 1;
+  const struct tdesc_feature *feature;
+
+  feature = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.base");
+  if (feature == NULL)
+    return NULL;
+
+  regnum = 0;
+  tdesc_data = tdesc_data_alloc ();
+
+  tdep->regs.r = regnum;
+  /* Validate the description provides the mandatory base registers
+     and allocate their numbers.  */
+  for (i = 0; i < 32; i++)
+    valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), regnum++,
+                                        loongarch_r_normal_name[i] + 1);
+  valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
+                                      tdep->regs.pc = regnum++, "pc");
+  valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
+                                      tdep->regs.badvaddr = regnum++, "badvaddr");
+  if (!valid_p)
+    return NULL;
+
+  /* LoongArch code is always little-endian.  */
+  info.byte_order_for_code = BFD_ENDIAN_LITTLE;
+
+  /* Find a candidate among the list of pre-declared architectures.  */
+  for (arches = gdbarch_list_lookup_by_info (arches, &info); arches != NULL;
+       arches = gdbarch_list_lookup_by_info (arches->next, &info))
+    {
+      loongarch_gdbarch_tdep *candidate_tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
+
+      if (candidate_tdep->ef_abi != tdep->ef_abi)
+        continue;
+
+      return arches->gdbarch;
+    }
+
+  /* None found, so create a new architecture from the information provided.  */
+  tdep = (struct loongarch_gdbarch_tdep *) xmalloc (sizeof (tdep_instant));
+  memcpy (tdep, &tdep_instant, sizeof (tdep_instant));
+  gdbarch = gdbarch_alloc (&info, tdep);
+
+  /* Target data types.  */
+  set_gdbarch_short_bit (gdbarch, 16);
+  set_gdbarch_int_bit (gdbarch, 32);
+  set_gdbarch_long_bit (gdbarch, loongarch_rlen(gdbarch));
+  set_gdbarch_long_long_bit (gdbarch, 64);
+  set_gdbarch_ptr_bit (gdbarch, loongarch_rlen(gdbarch));
+  set_gdbarch_char_signed (gdbarch, 0);
+
+  info.target_desc = tdesc;
+  info.tdesc_data = tdesc_data.get ();
+
+  /* Information about registers.  */
+  tdep->regs.ra = tdep->regs.r + 1;
+  tdep->regs.sp = tdep->regs.r + 3;
+  set_gdbarch_num_regs (gdbarch, regnum);
+  set_gdbarch_sp_regnum (gdbarch, tdep->regs.sp);
+  set_gdbarch_pc_regnum (gdbarch, tdep->regs.pc);
+
+  /* Finalise the target description registers.  */
+  tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
+
+  /* Advance PC across function entry code.  */
+  set_gdbarch_skip_prologue (gdbarch, loongarch_skip_prologue);
+
+  /* Stack grows downward.  */
+  set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
+
+  /* Frame info.  */
+  set_gdbarch_frame_align (gdbarch, loongarch_frame_align);
+
+  /* Breakpoint manipulation.  */
+  set_gdbarch_breakpoint_kind_from_pc (gdbarch, loongarch_breakpoint::kind_from_pc);
+  set_gdbarch_sw_breakpoint_from_kind (gdbarch, loongarch_breakpoint::bp_from_kind);
+
+  /* Frame unwinders. Use DWARF debug info if available, otherwise use our own unwinder.  */
+  set_gdbarch_dwarf2_reg_to_regnum (gdbarch, loongarch_dwarf2_reg_to_regnum);
+  dwarf2_append_unwinders (gdbarch);
+  frame_unwind_append_unwinder (gdbarch, &loongarch_frame_unwind);
+
+  /* Hook in OS ABI-specific overrides, if they have been registered.  */
+  gdbarch_init_osabi (info, gdbarch);
+
+  return gdbarch;
+}
+
+void _initialize_loongarch_tdep ();
+void
+_initialize_loongarch_tdep ()
+{
+  gdbarch_register (bfd_arch_loongarch, loongarch_gdbarch_init, NULL);
+}
diff --git a/gdb/loongarch-tdep.h b/gdb/loongarch-tdep.h
new file mode 100644
index 0000000..50f2156
--- /dev/null
+++ b/gdb/loongarch-tdep.h
@@ -0,0 +1,62 @@
+/* Target-dependent code for GNU/Linux LoongArch.
+
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   Contributed by Loongson Ltd.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef LOONGARCH_TDEP_H
+#define LOONGARCH_TDEP_H
+
+#include "gdbarch.h"
+#include "arch/loongarch.h"
+
+#include <regset.h>
+#include <sys/procfs.h>
+
+extern const struct regset loongarch_elf_gregset;
+
+/* Target-dependent structure in gdbarch.  */
+struct loongarch_gdbarch_tdep : gdbarch_tdep
+{
+  int ef_abi; /* EF_LOONGARCH_ABI  */
+
+  struct
+  {
+    int r;
+    int ra;
+    int sp;
+    int pc;
+    int badvaddr;
+  } regs;
+};
+
+static inline int
+loongarch_rlen (struct gdbarch *gdbarch)
+{
+  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
+
+  if (EF_LOONGARCH_IS_LP64 (tdep->ef_abi))
+    return 64;
+  else if (EF_LOONGARCH_IS_ILP32 (tdep->ef_abi))
+    return 32;
+  else
+    gdb_assert_not_reached ("unknown ABI");
+
+  return 0;
+}
+
+#endif /* LOONGARCH_TDEP_H  */
-- 
2.1.0


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 3/6] gdb: LoongArch: Add initial linux target support
  2021-12-09 11:35 [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
  2021-12-09 11:35 ` [PATCH 1/6] gdb: LoongArch: Add initial target description support Tiezhu Yang
  2021-12-09 11:35 ` [PATCH 2/6] gdb: LoongArch: Add initial baremetal support Tiezhu Yang
@ 2021-12-09 11:35 ` Tiezhu Yang
  2022-01-06 21:31   ` Tom Tromey
  2022-01-10 14:34   ` Andrew Burgess
  2021-12-09 11:35 ` [PATCH 4/6] gdb: LoongArch: Add initial native linux support Tiezhu Yang
                   ` (4 subsequent siblings)
  7 siblings, 2 replies; 22+ messages in thread
From: Tiezhu Yang @ 2021-12-09 11:35 UTC (permalink / raw)
  To: gdb-patches
  Cc: Xuefeng Li, Chenghua Xu, Zhensong Liu, Qing zhang, Youling Tang,
	Tiezhu Yang

This commit adds initial linux target support for LoongArch.

Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
Signed-off-by: Qing zhang <zhangqing@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 gdb/loongarch-linux-tdep.c | 147 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 147 insertions(+)
 create mode 100644 gdb/loongarch-linux-tdep.c

diff --git a/gdb/loongarch-linux-tdep.c b/gdb/loongarch-linux-tdep.c
new file mode 100644
index 0000000..40b8008
--- /dev/null
+++ b/gdb/loongarch-linux-tdep.c
@@ -0,0 +1,147 @@
+/* Target-dependent code for GNU/Linux LoongArch.
+
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   Contributed by Loongson Ltd.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   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 "defs.h"
+#include "glibc-tdep.h"
+#include "inferior.h"
+#include "linux-tdep.h"
+#include "loongarch-tdep.h"
+#include "solib-svr4.h"
+#include "target-descriptions.h"
+
+/* Unpack an elf_gregset_t into GDB's register cache.  */
+
+static void
+loongarch_supply_elf_gregset (const struct regset *r,
+			      struct regcache *regcache, int regno,
+			      const void *gprs, size_t len)
+{
+  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
+  auto regs = tdep->regs;
+  gdb_assert (0 <= regs.r && sizeof (elf_gregset_t) <= len);
+
+  int regsize = register_size (regcache->arch (), regs.r);
+  const gdb_byte *buf = NULL;
+
+  if (regno == -1)
+    {
+      /* Set $r0 = 0.  */
+      regcache->raw_supply_zeroed (regs.r);
+
+      for (int i = 1; i < 32; i++)
+	{
+	  buf = (const gdb_byte*)gprs + regsize * i;
+	  regcache->raw_supply (regs.r + i, (const void *)buf);
+	}
+
+      /* Size base (pc) = regsize * regs.pc.  */
+      buf = (const gdb_byte*)gprs + regsize * regs.pc;
+      regcache->raw_supply (regs.pc, (const void *)buf);
+
+      /* Size base (badvaddr) = regsize * regs.badvaddr.  */
+      buf = (const gdb_byte*)gprs + regsize * regs.badvaddr;
+      regcache->raw_supply (regs.badvaddr, (const void *)buf);
+    }
+  else if (regs.r == regno)
+    regcache->raw_supply_zeroed (regno);
+  else if ((regs.r < regno && regno < regs.r + 32) ||
+	   (regs.pc == regno) || (regs.badvaddr == regno))
+    {
+      /* Offset offset (regno) = regsize * (regno - regs.r).  */
+      buf = (const gdb_byte*)gprs + regsize * (regno - regs.r);
+      regcache->raw_supply (regno, (const void *)buf);
+    }
+}
+
+/* Pack the GDB's register cache value into an elf_gregset_t.  */
+
+static void
+loongarch_fill_elf_gregset (const struct regset *r,
+			    const struct regcache *regcache, int regno,
+			    void *gprs, size_t len)
+{
+  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
+  auto regs = tdep->regs;
+  gdb_assert (0 <= regs.r && sizeof (elf_gregset_t) <= len);
+  int regsize = register_size (regcache->arch (), regs.r);
+  gdb_byte *buf = NULL;
+
+  if (regno == -1)
+    {
+      for (int i = 0; i < 32; i++)
+	{
+	  buf = (gdb_byte *)gprs + regsize * i;
+	  regcache->raw_collect (regs.r + i, (void *)buf);
+	}
+
+      /* Size base (pc) = regsize * regs.pc.  */
+      buf = (gdb_byte *)gprs + regsize * regs.pc;
+      regcache->raw_collect (regs.pc, (void *)buf);
+
+      /* Size base (badvaddr) = regsize * regs.badvaddr.  */
+      buf = (gdb_byte *)gprs + regsize * regs.badvaddr;
+      regcache->raw_collect (regs.badvaddr, (void *)buf);
+    }
+  else if ((regs.r <= regno && regno < regs.r + 32) ||
+	   (regs.pc == regno) || (regs.badvaddr == regno))
+    {
+      /* Offset offset (regno) = regsize * (regno - regs.r).  */
+      buf = (gdb_byte *)gprs + regsize * (regno - regs.r);
+      regcache->raw_collect (regno, (void *)buf);
+    }
+}
+
+const struct regset loongarch_elf_gregset =
+{
+  NULL,
+  loongarch_supply_elf_gregset,
+  loongarch_fill_elf_gregset,
+};
+
+static void
+loongarch_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
+{
+  linux_init_abi (info, gdbarch, 0);
+
+  set_solib_svr4_fetch_link_map_offsets (gdbarch, loongarch_rlen(gdbarch) == 32
+					 ? linux_ilp32_fetch_link_map_offsets
+					 : linux_lp64_fetch_link_map_offsets);
+
+  /* GNU/Linux uses SVR4-style shared libraries.  */
+  set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
+
+  /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
+  set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
+
+  /* Enable TLS support.  */
+  set_gdbarch_fetch_tls_load_module_address (gdbarch, svr4_fetch_objfile_link_map);
+}
+
+/* Initialize LoongArch Linux target support.  */
+
+void _initialize_loongarch_linux_tdep ();
+void
+_initialize_loongarch_linux_tdep ()
+{
+  gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch32,
+                          GDB_OSABI_LINUX, loongarch_linux_init_abi);
+  gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch64,
+                          GDB_OSABI_LINUX, loongarch_linux_init_abi);
+}
-- 
2.1.0


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 4/6] gdb: LoongArch: Add initial native linux support
  2021-12-09 11:35 [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
                   ` (2 preceding siblings ...)
  2021-12-09 11:35 ` [PATCH 3/6] gdb: LoongArch: Add initial linux target support Tiezhu Yang
@ 2021-12-09 11:35 ` Tiezhu Yang
  2022-01-06 21:36   ` Tom Tromey
  2022-01-10 14:38   ` Andrew Burgess
  2021-12-09 11:36 ` [PATCH 5/6] gdb: LoongArch: Add Makefile and configure Tiezhu Yang
                   ` (3 subsequent siblings)
  7 siblings, 2 replies; 22+ messages in thread
From: Tiezhu Yang @ 2021-12-09 11:35 UTC (permalink / raw)
  To: gdb-patches
  Cc: Xuefeng Li, Chenghua Xu, Zhensong Liu, Qing zhang, Youling Tang,
	Tiezhu Yang

This commit adds initial native linux support for LoongArch.

Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
Signed-off-by: Qing zhang <zhangqing@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 gdb/loongarch-linux-nat.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 175 insertions(+)
 create mode 100644 gdb/loongarch-linux-nat.c

diff --git a/gdb/loongarch-linux-nat.c b/gdb/loongarch-linux-nat.c
new file mode 100644
index 0000000..b35c3e2
--- /dev/null
+++ b/gdb/loongarch-linux-nat.c
@@ -0,0 +1,175 @@
+/* Target-dependent code for GNU/Linux LoongArch.
+
+   Copyright (C) 2021 Free Software Foundation, Inc.
+   Contributed by Loongson Ltd.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   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 "defs.h"
+#include "elf/common.h"
+#include "gregset.h"
+#include "inferior.h"
+#include "linux-nat-trad.h"
+#include "loongarch-tdep.h"
+#include "nat/gdb_ptrace.h"
+#include "target-descriptions.h"
+
+#include <asm/ptrace.h>
+
+/* LoongArch Linux native additions to the default linux support.  */
+
+class loongarch_linux_nat_target final : public linux_nat_trad_target
+{
+public:
+  /* Add our register access methods.  */
+  void fetch_registers (struct regcache *, int) override;
+  void store_registers (struct regcache *, int) override;
+
+protected:
+  /* Override linux_nat_trad_target methods.  */
+  CORE_ADDR register_u_offset (struct gdbarch *gdbarch, int regno,
+                               int store_p) override;
+};
+
+/* Fill GDB's register array with the general-purpose, pc and badvaddr
+   register values from the current thread.  */
+
+static void
+fetch_gregs_from_thread (struct regcache *regcache, int regno, pid_t tid)
+{
+  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
+  auto regs = tdep->regs;
+  elf_gregset_t regset;
+
+  if ((regno == -1) || (regs.r <= regno && regno < regs.r + 32) ||
+      (regs.pc == regno) || (regs.badvaddr == regno))
+  {
+    struct iovec iovec = { .iov_base = &regset, .iov_len = sizeof (regset) };
+
+    if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, (long) &iovec) < 0)
+      perror_with_name (_("Couldn't get NT_PRSTATUS registers"));
+    else
+      loongarch_elf_gregset.supply_regset (NULL, regcache, regno, &regset,
+					   sizeof (regset));
+  }
+}
+
+/* Store to the current thread the valid general-purpose, pc and badvaddr
+   register values in the GDB's register array.  */
+
+static void
+store_gregs_to_thread (struct regcache *regcache, int regno, pid_t tid)
+{
+  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());
+  auto regs = tdep->regs;
+  elf_gregset_t regset;
+
+  if ((regno == -1) || (regs.r <= regno && regno < regs.r + 32) ||
+      (regs.pc == regno) || (regs.badvaddr == regno))
+  {
+    struct iovec iovec = { .iov_base = &regset, .iov_len = sizeof (regset) };
+
+    if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, (long) &iovec) < 0)
+      perror_with_name (_("Couldn't get NT_PRSTATUS registers"));
+    else
+      {
+	loongarch_elf_gregset.collect_regset (NULL, regcache, regno, &regset,
+					      sizeof (regset));
+	if (ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, (long) &iovec) < 0)
+	  perror_with_name (_("Couldn't set NT_PRSTATUS registers"));
+      }
+  }
+}
+
+/* Implement the "fetch_registers" target_ops method.  */
+
+void
+loongarch_linux_nat_target::fetch_registers (struct regcache *regcache,
+					     int regno)
+{
+  pid_t tid = get_ptrace_pid (regcache->ptid ());
+
+  fetch_gregs_from_thread(regcache, regno, tid);
+}
+
+/* Implement the "store_registers" target_ops method.  */
+
+void
+loongarch_linux_nat_target::store_registers (struct regcache *regcache,
+					     int regno)
+{
+  pid_t tid = get_ptrace_pid (regcache->ptid ());
+
+  store_gregs_to_thread(regcache, regno, tid);
+}
+
+/* Return the address in the core dump or inferior of register REGNO.  */
+
+CORE_ADDR
+loongarch_linux_nat_target::register_u_offset (struct gdbarch *gdbarch,
+					       int regno, int store_p)
+{
+  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
+  auto regs = tdep->regs;
+
+  /* according to <asm/ptrace.h> */
+  if (0 <= regs.r && regs.r <= regno && regno < regs.r + GPR_NUM)
+    return GPR_BASE + regno - regs.r;
+  else if (regs.pc == regno)
+    return PC;
+  else
+    return -1;
+}
+
+static loongarch_linux_nat_target the_loongarch_linux_nat_target;
+
+/* Wrapper functions.  These are only used by libthread_db.  */
+
+void
+supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregset)
+{
+  loongarch_elf_gregset.supply_regset (NULL, regcache, -1, gregset,
+				       sizeof (gdb_gregset_t));
+}
+
+void
+fill_gregset (const struct regcache *regcache, gdb_gregset_t *gregset,
+	      int regno)
+{
+  loongarch_elf_gregset.collect_regset (NULL, regcache, regno, gregset,
+					sizeof (gdb_gregset_t));
+}
+
+void
+supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregset)
+{
+}
+
+void
+fill_fpregset (const struct regcache *regcache, gdb_fpregset_t *fpregset,
+	       int regno)
+{
+}
+
+/* Initialize LoongArch Linux native support.  */
+
+void _initialize_loongarch_linux_nat ();
+void
+_initialize_loongarch_linux_nat ()
+{
+  linux_target = &the_loongarch_linux_nat_target;
+  add_inf_child_target (&the_loongarch_linux_nat_target);
+}
-- 
2.1.0


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 5/6] gdb: LoongArch: Add Makefile and configure
  2021-12-09 11:35 [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
                   ` (3 preceding siblings ...)
  2021-12-09 11:35 ` [PATCH 4/6] gdb: LoongArch: Add initial native linux support Tiezhu Yang
@ 2021-12-09 11:36 ` Tiezhu Yang
  2022-01-06 21:39   ` Tom Tromey
  2022-01-10 12:06   ` Andrew Burgess
  2021-12-09 11:36 ` [PATCH 6/6] gdb: LoongArch: Update ChangeLog and NEWS Tiezhu Yang
                   ` (2 subsequent siblings)
  7 siblings, 2 replies; 22+ messages in thread
From: Tiezhu Yang @ 2021-12-09 11:36 UTC (permalink / raw)
  To: gdb-patches
  Cc: Xuefeng Li, Chenghua Xu, Zhensong Liu, Qing zhang, Youling Tang,
	Tiezhu Yang

This commit adds Makefile and configure for LoongArch.

Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
Signed-off-by: Qing zhang <zhangqing@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 gdb/Makefile.in       |  9 +++++++++
 gdb/configure.host    |  3 +++
 gdb/configure.nat     |  4 ++++
 gdb/configure.tgt     | 11 +++++++++++
 gdb/features/Makefile |  4 ++++
 5 files changed, 31 insertions(+)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index bff2757..d492882 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -733,6 +733,7 @@ ALL_TARGET_OBS = \
 	arch/arm-get-next-pcs.o \
 	arch/arm-linux.o \
 	arch/i386.o \
+	arch/loongarch.o \
 	arch/ppc-linux-common.o \
 	arch/riscv.o \
 	arm-bsd-tdep.o \
@@ -783,6 +784,9 @@ ALL_TARGET_OBS = \
 	linux-record.o \
 	linux-tdep.o \
 	lm32-tdep.o \
+	loongarch-tdep.o \
+	loongarch-linux-tdep.o \
+	loongarch-linux-nat.o \
 	m32c-tdep.o \
 	m32r-linux-tdep.o \
 	m32r-tdep.o \
@@ -1359,6 +1363,7 @@ HFILES_NO_SRCDIR = \
 	linux-record.h \
 	linux-tdep.h \
 	location.h \
+	loongarch-tdep.h \
 	m2-lang.h \
 	m32r-tdep.h \
 	m68k-tdep.h \
@@ -1493,6 +1498,7 @@ HFILES_NO_SRCDIR = \
 	arch/arc.h \
 	arch/arm.h \
 	arch/i386.h \
+	arch/loongarch.h \
 	arch/ppc-linux-common.h \
 	arch/ppc-linux-tdesc.h \
 	arch/riscv.h \
@@ -2234,6 +2240,9 @@ ALLDEPFILES = \
 	linux-record.c \
 	linux-tdep.c \
 	lm32-tdep.c \
+	loongarch-tdep.c \
+	loongarch-linux-tdep.c \
+	loongarch-linux-nat.c \
 	m32r-linux-nat.c \
 	m32r-linux-tdep.c \
 	m32r-tdep.c \
diff --git a/gdb/configure.host b/gdb/configure.host
index be40cc8..da71675 100644
--- a/gdb/configure.host
+++ b/gdb/configure.host
@@ -58,6 +58,7 @@ arc*)			gdb_host_cpu=arc ;;
 arm*)			gdb_host_cpu=arm ;;
 hppa*)			gdb_host_cpu=pa ;;
 i[34567]86*)		gdb_host_cpu=i386 ;;
+loongarch*)		gdb_host_cpu=loongarch ;;
 m68*)			gdb_host_cpu=m68k ;;
 mips*)			gdb_host_cpu=mips ;;
 powerpc* | rs6000)	gdb_host_cpu=powerpc ;;
@@ -117,6 +118,8 @@ i[34567]86-*-cygwin*)	gdb_host=cygwin ;;
 
 ia64-*-linux*)		gdb_host=linux ;;
 
+loongarch*-linux*)	gdb_host=linux ;;
+
 m68*-*-linux*)		gdb_host=linux ;;
 m68*-*-netbsd* | m68*-*-knetbsd*-gnu)
 			gdb_host=nbsdelf ;;
diff --git a/gdb/configure.nat b/gdb/configure.nat
index bdd0503..148c5ca 100644
--- a/gdb/configure.nat
+++ b/gdb/configure.nat
@@ -258,6 +258,10 @@ case ${gdb_host} in
 		# Host: Intel IA-64 running GNU/Linux
 		NATDEPFILES="${NATDEPFILES} ia64-linux-nat.o"
 		;;
+	    loongarch)
+		# Host: LoongArch, running GNU/Linux.
+		NATDEPFILES="${NATDEPFILES} loongarch-linux-nat.o linux-nat-trad.o"
+		;;
 	    m32r)
 		# Host: M32R based machine running GNU/Linux
 		NATDEPFILES="${NATDEPFILES} m32r-linux-nat.o"
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index 56cdfef..65f3bf6 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -91,6 +91,11 @@ ia64*-*-*)
 	cpu_obs="ia64-tdep.o"
 	;;
 
+loongarch*-*-*)
+	# Target: LoongArch baremetal
+	cpu_obs="loongarch-tdep.o arch/loongarch.o"
+	;;
+
 riscv*-*-*)
 	cpu_obs="riscv-tdep.o riscv-none-tdep.o arch/riscv.o \
 	         ravenscar-thread.o riscv-ravenscar-thread.o";;
@@ -341,6 +346,12 @@ lm32-*-*)
 	gdb_target_obs="lm32-tdep.o" 
 	;;
 
+loongarch*-*-linux*)
+	# Target: LoongArch running Linux
+	gdb_target_obs="loongarch-linux-tdep.o glibc-tdep.o \
+			linux-tdep.o solib-svr4.o"
+	;;
+
 m32c-*-*)
 	# Target: Renesas M32C family
 	gdb_target_obs="m32c-tdep.o"
diff --git a/gdb/features/Makefile b/gdb/features/Makefile
index 311176a..c5ad2cb 100644
--- a/gdb/features/Makefile
+++ b/gdb/features/Makefile
@@ -69,6 +69,7 @@ WHICH = mips-linux mips-dsp-linux \
 	tic6x-c64xp-linux tic6x-c64x-linux tic6x-c62x-linux
 
 # Record which registers should be sent to GDB by default after stop.
+loongarch-expedite = r3,pc
 mips-expedite = r29,pc
 mips-dsp-expedite = r29,pc
 mips64-expedite = r29,pc
@@ -173,6 +174,7 @@ GDB = false
 aarch64-feature = 1
 arm-feature = 1
 i386-feature = 1
+loongarch-feature = 1
 riscv-feature = 1
 tic6x-feature = 1
 
@@ -226,6 +228,8 @@ FEATURE_XMLFILES = aarch64-core.xml \
 	i386/64bit-pkeys.xml \
 	i386/64bit-sse.xml \
 	i386/x32-core.xml \
+	loongarch/base32.xml \
+	loongarch/base64.xml \
 	riscv/rv32e-xregs.xml \
 	riscv/32bit-cpu.xml \
 	riscv/32bit-fpu.xml \
-- 
2.1.0


^ permalink raw reply	[flat|nested] 22+ messages in thread

* [PATCH 6/6] gdb: LoongArch: Update ChangeLog and NEWS
  2021-12-09 11:35 [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
                   ` (4 preceding siblings ...)
  2021-12-09 11:36 ` [PATCH 5/6] gdb: LoongArch: Add Makefile and configure Tiezhu Yang
@ 2021-12-09 11:36 ` Tiezhu Yang
  2022-01-06 21:40   ` Tom Tromey
  2021-12-15  8:36 ` [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
  2022-01-06 21:41 ` Tom Tromey
  7 siblings, 1 reply; 22+ messages in thread
From: Tiezhu Yang @ 2021-12-09 11:36 UTC (permalink / raw)
  To: gdb-patches
  Cc: Xuefeng Li, Chenghua Xu, Zhensong Liu, Qing zhang, Youling Tang,
	Tiezhu Yang

This commit updates ChangeLog and NEWS for LoongArch.

Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
Signed-off-by: Qing zhang <zhangqing@loongson.cn>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 gdb/ChangeLog-2021 | 20 ++++++++++++++++++++
 gdb/NEWS           |  4 ++++
 2 files changed, 24 insertions(+)

diff --git a/gdb/ChangeLog-2021 b/gdb/ChangeLog-2021
index f1071a7..f3ac558 100644
--- a/gdb/ChangeLog-2021
+++ b/gdb/ChangeLog-2021
@@ -1,3 +1,23 @@
+2021-12-09  Tiezhu Yang  <yangtiezhu@loongson.cn>
+
+	* NEWS: Add basic support for LoongArch
+	* gdb/arch/loongarch.h: New file.
+	* gdb/arch/loongarch.c: New file.
+	* gdb/features/loongarch/base32.c: New file.
+	* gdb/features/loongarch/base32.xml: New file.
+	* gdb/features/loongarch/base64.c: New file.
+	* gdb/features/loongarch/base64.xml: New file.
+	* gdb/loongarch-linux-nat.c: New file.
+	* gdb/loongarch-linux-tdep.c: New file.
+	* gdb/loongarch-tdep.h: New file.
+	* gdb/loongarch-tdep.c: New file.
+	* gdb/Makefile.in: Add loongarch related.
+	* gdb/configure.host: Add loongarch related.
+	* gdb/configure.nat: Add loongarch related.
+	* gdb/configure.tgt: Add loongarch related.
+	* gdb/doc/gdb.texinfo: Add loongarch related.
+	* gdb/features/Makefile: Add loongarch related.
+
 2021-07-06  Tom de Vries  <tdevries@suse.de>
 
 	* dwarf2/read.c (scan_partial_symbols): Skip top-level imports of
diff --git a/gdb/NEWS b/gdb/NEWS
index 13b6628..719d813 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -53,6 +53,10 @@ maint packet
   as escaped hex, e.g. \x?? where '??' is replaces with the value of
   the non-printable character.
 
+* New targets
+
+GNU/Linux/LoongArch    loongarch*-*-linux*
+
 * Python API
 
   ** New function gdb.add_history(), which takes a gdb.Value object
-- 
2.1.0


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 0/6] gdb: Add basic support for LoongArch
  2021-12-09 11:35 [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
                   ` (5 preceding siblings ...)
  2021-12-09 11:36 ` [PATCH 6/6] gdb: LoongArch: Update ChangeLog and NEWS Tiezhu Yang
@ 2021-12-15  8:36 ` Tiezhu Yang
  2022-01-06 21:41 ` Tom Tromey
  7 siblings, 0 replies; 22+ messages in thread
From: Tiezhu Yang @ 2021-12-15  8:36 UTC (permalink / raw)
  To: gdb-patches, Pedro Alves, Joel Brobecker, Kevin Buettner,
	Andrew Burgess, Doug Evans, Simon Marchi, Yao Qi, Tom Tromey,
	Tom de Vries, Ulrich Weigand, Eli Zaretskii
  Cc: Xuefeng Li, Chenghua Xu, Zhensong Liu, Qing zhang, Youling Tang,
	Tiezhu Yang

On 12/09/2021 07:35 PM, Tiezhu Yang wrote:
> About one month ago, my workmate Zhensong Liu submitted a single gdb
> patch [1] to support LoongArch which is a new RISC architecture, thanks
> Zhensong for his great work.
>
> It seems that the patch is too big to review, a patch series would be
> easier to review after internal discussion.
>
> This patchset only adds the minimal changes as simple as possible, the
> basic command "run", "break", "continue", "next", "step" and "quit" can
> be used to debug.
>
> This is the first step, we will submit other more patches step by step
> in the future, these small patches make an easily understood change that
> can be verified by reviewers, any comments will be much appreciated.
>
> Here are some test results:
>
>     $ make check-gdb TESTS="gdb.base/a2-run.exp"
>     $ cat gdb/testsuite/gdb.sum
>     [...]
>     PASS: gdb.base/a2-run.exp: run "a2-run" with no args
>     PASS: gdb.base/a2-run.exp: no spurious messages at program exit
>     PASS: gdb.base/a2-run.exp: run "a2-run" with arg
>     PASS: gdb.base/a2-run.exp: run "a2-run" again with same args
>     PASS: gdb.base/a2-run.exp: set args
>     PASS: gdb.base/a2-run.exp: run after setting args to nil
>     PASS: gdb.base/a2-run.exp: set args 6
>     PASS: gdb.base/a2-run.exp: run "a2-run" again after setting args
>     PASS: gdb.base/a2-run.exp: run "a2-run" with shell
>
> 		    === gdb Summary ===
>
>     # of expected passes		9
>     [...]
>
>     $ make check-gdb TESTS="gdb.base/access-mem-running.exp"
>     $ cat gdb/testsuite/gdb.sum
>     [...]
>     PASS: gdb.base/access-mem-running.exp: all-stop: continuing
>     PASS: gdb.base/access-mem-running.exp: all-stop: get global_counter once
>     PASS: gdb.base/access-mem-running.exp: all-stop: get global_counter twice
>     PASS: gdb.base/access-mem-running.exp: all-stop: value changed
>     PASS: gdb.base/access-mem-running.exp: all-stop: print global_var before writing
>     PASS: gdb.base/access-mem-running.exp: all-stop: write to global_var
>     PASS: gdb.base/access-mem-running.exp: all-stop: print global_var after writing
>     PASS: gdb.base/access-mem-running.exp: all-stop: write to global_var again
>     PASS: gdb.base/access-mem-running.exp: all-stop: b maybe_stop_here
>     PASS: gdb.base/access-mem-running.exp: all-stop: breakpoint hits
>     PASS: gdb.base/access-mem-running.exp: non-stop: continuing
>     PASS: gdb.base/access-mem-running.exp: non-stop: get global_counter once
>     PASS: gdb.base/access-mem-running.exp: non-stop: get global_counter twice
>     PASS: gdb.base/access-mem-running.exp: non-stop: value changed
>     PASS: gdb.base/access-mem-running.exp: non-stop: print global_var before writing
>     PASS: gdb.base/access-mem-running.exp: non-stop: write to global_var
>     PASS: gdb.base/access-mem-running.exp: non-stop: print global_var after writing
>     PASS: gdb.base/access-mem-running.exp: non-stop: write to global_var again
>     PASS: gdb.base/access-mem-running.exp: non-stop: b maybe_stop_here
>     PASS: gdb.base/access-mem-running.exp: non-stop: breakpoint hits
>
> 		    === gdb Summary ===
>
>     # of expected passes		20
>     [...]
>
> The LoongArch documents:
> https://loongson.github.io/LoongArch-Documentation/README-EN.html
>
> The LoongArch ELF ABI Documents:
> https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html
>
> The LoongArch binutils has been merged into trunk:
> https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=e9a0721f8274
>
> [1] https://sourceware.org/pipermail/gdb-patches/2021-November/183353.html
>
> Tiezhu Yang (6):
>   gdb: LoongArch: Add initial target description support
>   gdb: LoongArch: Add initial baremetal support
>   gdb: LoongArch: Add initial linux target support
>   gdb: LoongArch: Add initial native linux support
>   gdb: LoongArch: Add Makefile and configure
>   gdb: LoongArch: Update ChangeLog and NEWS
>
>  gdb/ChangeLog-2021                |  20 +++
>  gdb/Makefile.in                   |   9 ++
>  gdb/NEWS                          |   4 +
>  gdb/arch/loongarch.c              |  47 +++++++
>  gdb/arch/loongarch.h              |  30 +++++
>  gdb/configure.host                |   3 +
>  gdb/configure.nat                 |   4 +
>  gdb/configure.tgt                 |  11 ++
>  gdb/doc/gdb.texinfo               |  10 ++
>  gdb/features/Makefile             |   4 +
>  gdb/features/loongarch/base32.c   |  47 +++++++
>  gdb/features/loongarch/base32.xml |  45 +++++++
>  gdb/features/loongarch/base64.c   |  47 +++++++
>  gdb/features/loongarch/base64.xml |  45 +++++++
>  gdb/loongarch-linux-nat.c         | 175 ++++++++++++++++++++++++
>  gdb/loongarch-linux-tdep.c        | 147 +++++++++++++++++++++
>  gdb/loongarch-tdep.c              | 270 ++++++++++++++++++++++++++++++++++++++
>  gdb/loongarch-tdep.h              |  62 +++++++++
>  18 files changed, 980 insertions(+)
>  create mode 100644 gdb/arch/loongarch.c
>  create mode 100644 gdb/arch/loongarch.h
>  create mode 100644 gdb/features/loongarch/base32.c
>  create mode 100644 gdb/features/loongarch/base32.xml
>  create mode 100644 gdb/features/loongarch/base64.c
>  create mode 100644 gdb/features/loongarch/base64.xml
>  create mode 100644 gdb/loongarch-linux-nat.c
>  create mode 100644 gdb/loongarch-linux-tdep.c
>  create mode 100644 gdb/loongarch-tdep.c
>  create mode 100644 gdb/loongarch-tdep.h
>

Hi global maintainers,

Is there any feedback for this series? Do you have any opinions?

If anybody has any suggestions on how could we move forward with
this series, that would be greatly appreciated.

Thanks,
Tiezhu


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 1/6] gdb: LoongArch: Add initial target description support
  2021-12-09 11:35 ` [PATCH 1/6] gdb: LoongArch: Add initial target description support Tiezhu Yang
@ 2022-01-06 21:19   ` Tom Tromey
  2022-01-10 14:04   ` Andrew Burgess
  1 sibling, 0 replies; 22+ messages in thread
From: Tom Tromey @ 2022-01-06 21:19 UTC (permalink / raw)
  To: Tiezhu Yang
  Cc: gdb-patches, Qing zhang, Xuefeng Li, Chenghua Xu, Zhensong Liu,
	Youling Tang

>>>>> ">" == Tiezhu Yang <yangtiezhu@loongson.cn> writes:

>> This commit adds initial target description support for LoongArch.

Hi.  Thank you for the patch.

>> +#include "defs.h"

Code in arch should use common-defs.h, not defs.h, because it may also
be compiled by gdbserver.  Even if your port doesn't handle gdbserver
now (I didn't look yet), it's still what I think we'd like.

>> +#include <stdint.h>

common-types.h includes inttypes.h so normally I think this isn't
needed.

>> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
>> index d0c5bcf..12ee3ee 100644
>> --- a/gdb/doc/gdb.texinfo
>> +++ b/gdb/doc/gdb.texinfo

Eli will have to review the doc changes.

Tom

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 2/6] gdb: LoongArch: Add initial baremetal support
  2021-12-09 11:35 ` [PATCH 2/6] gdb: LoongArch: Add initial baremetal support Tiezhu Yang
@ 2022-01-06 21:27   ` Tom Tromey
  2022-01-10 14:28   ` Andrew Burgess
  1 sibling, 0 replies; 22+ messages in thread
From: Tom Tromey @ 2022-01-06 21:27 UTC (permalink / raw)
  To: Tiezhu Yang
  Cc: gdb-patches, Qing zhang, Xuefeng Li, Chenghua Xu, Zhensong Liu,
	Youling Tang

>>>>> ">" == Tiezhu Yang <yangtiezhu@loongson.cn> writes:

>> This commit adds initial baremetal support for LoongArch.

Thanks.

>> +   Copyright (C) 2021 Free Software Foundation, Inc.

You'll want to update the years now.

>> +constexpr gdb_byte loongarch_default_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};

Probably should be static?
I didn't check to see if it's needed elsewhere.

>> diff --git a/gdb/loongarch-tdep.h b/gdb/loongarch-tdep.h
>> new file mode 100644
>> index 0000000..50f2156

>> +#include <regset.h>

More normal to use "" for gdb includes.

>> +#include <sys/procfs.h>

It seems wrong to put this here.  tdep files can be built for any host,
but sys/procfs.h probably doesn't exist universally.

Tom

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 3/6] gdb: LoongArch: Add initial linux target support
  2021-12-09 11:35 ` [PATCH 3/6] gdb: LoongArch: Add initial linux target support Tiezhu Yang
@ 2022-01-06 21:31   ` Tom Tromey
  2022-01-10 14:34   ` Andrew Burgess
  1 sibling, 0 replies; 22+ messages in thread
From: Tom Tromey @ 2022-01-06 21:31 UTC (permalink / raw)
  To: Tiezhu Yang
  Cc: gdb-patches, Qing zhang, Xuefeng Li, Chenghua Xu, Zhensong Liu,
	Youling Tang

>>>>> ">" == Tiezhu Yang <yangtiezhu@loongson.cn> writes:

>> This commit adds initial linux target support for LoongArch.

This one looks reasonable to me.  Thanks.

I didn't try to check any of the details of the port, btw.
I take your word for it on these.

It might be interesting to see the dejagnu test result summary for the
Linux port, or to compare them against x86-64 results.  Often this can
show whether there are big issues... though at the same time, I think
it's fine to accept a port even with issues.

thanks
Tom

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 4/6] gdb: LoongArch: Add initial native linux support
  2021-12-09 11:35 ` [PATCH 4/6] gdb: LoongArch: Add initial native linux support Tiezhu Yang
@ 2022-01-06 21:36   ` Tom Tromey
  2022-01-10 14:38   ` Andrew Burgess
  1 sibling, 0 replies; 22+ messages in thread
From: Tom Tromey @ 2022-01-06 21:36 UTC (permalink / raw)
  To: Tiezhu Yang
  Cc: gdb-patches, Qing zhang, Xuefeng Li, Chenghua Xu, Zhensong Liu,
	Youling Tang

>>>>> ">" == Tiezhu Yang <yangtiezhu@loongson.cn> writes:

>> This commit adds initial native linux support for LoongArch.

Thanks for the patch.

>> +    struct iovec iovec = { .iov_base = &regset, .iov_len = sizeof (regset) };

I'm surprised this compiles, because IIUC this one of the features that
is in C but not in C++.

It's fine to rewrite it as explicit assignments, e.g. from
arm-linux-nat.c:

      struct iovec iov;

      iov.iov_base = &regs;
      iov.iov_len = sizeof (regs);

There are a few spots that need this.

It possibly might compile as a GNU extension or something like that.
However we mostly avoid those unless they can be used conditionally
(this is why attributes can be used).

>> +  if ((regno == -1) || (regs.r <= regno && regno < regs.r + 32) ||
>> +      (regs.pc == regno) || (regs.badvaddr == regno))

FWIW the extra parens are redundant and gdb style is to avoid them.

>> +  store_gregs_to_thread(regcache, regno, tid);

Space before paren here.

Tom

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 5/6] gdb: LoongArch: Add Makefile and configure
  2021-12-09 11:36 ` [PATCH 5/6] gdb: LoongArch: Add Makefile and configure Tiezhu Yang
@ 2022-01-06 21:39   ` Tom Tromey
  2022-01-10 12:06   ` Andrew Burgess
  1 sibling, 0 replies; 22+ messages in thread
From: Tom Tromey @ 2022-01-06 21:39 UTC (permalink / raw)
  To: Tiezhu Yang
  Cc: gdb-patches, Qing zhang, Xuefeng Li, Chenghua Xu, Zhensong Liu,
	Youling Tang

>>>>> ">" == Tiezhu Yang <yangtiezhu@loongson.cn> writes:

>> This commit adds Makefile and configure for LoongArch.

Thank you.

>> @@ -783,6 +784,9 @@ ALL_TARGET_OBS = \
>>  	linux-record.o \
>>  	linux-tdep.o \
>>  	lm32-tdep.o \
>> +	loongarch-tdep.o \
>> +	loongarch-linux-tdep.o \
>> +	loongarch-linux-nat.o \

I think the -nat file should not be here.

Tom

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 6/6] gdb: LoongArch: Update ChangeLog and NEWS
  2021-12-09 11:36 ` [PATCH 6/6] gdb: LoongArch: Update ChangeLog and NEWS Tiezhu Yang
@ 2022-01-06 21:40   ` Tom Tromey
  0 siblings, 0 replies; 22+ messages in thread
From: Tom Tromey @ 2022-01-06 21:40 UTC (permalink / raw)
  To: Tiezhu Yang
  Cc: gdb-patches, Qing zhang, Xuefeng Li, Chenghua Xu, Zhensong Liu,
	Youling Tang

>>>>> ">" == Tiezhu Yang <yangtiezhu@loongson.cn> writes:

>> This commit updates ChangeLog and NEWS for LoongArch.

The NEWS part could be rolled into patch 5, I think, since that's the
patch that enables it.

We don't use ChangeLog any more so that can simply be dropped.

Tom

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 0/6] gdb: Add basic support for LoongArch
  2021-12-09 11:35 [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
                   ` (6 preceding siblings ...)
  2021-12-15  8:36 ` [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
@ 2022-01-06 21:41 ` Tom Tromey
  2022-01-07 12:41   ` Tiezhu Yang
  7 siblings, 1 reply; 22+ messages in thread
From: Tom Tromey @ 2022-01-06 21:41 UTC (permalink / raw)
  To: Tiezhu Yang
  Cc: gdb-patches, Qing zhang, Xuefeng Li, Chenghua Xu, Zhensong Liu,
	Youling Tang

>>>>> ">" == Tiezhu Yang <yangtiezhu@loongson.cn> writes:

>> This patchset only adds the minimal changes as simple as possible, the
>> basic command "run", "break", "continue", "next", "step" and "quit" can
>> be used to debug.

>> This is the first step, we will submit other more patches step by step
>> in the future, these small patches make an easily understood change that
>> can be verified by reviewers, any comments will be much appreciated.

Oops, sorry, I read the patches and neglected to read the cover letter
first.  I see now what you're up to, and this plan sounds fine to me.
Don't worry about the test results I asked about.

thanks,
Tom

^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 0/6] gdb: Add basic support for LoongArch
  2022-01-06 21:41 ` Tom Tromey
@ 2022-01-07 12:41   ` Tiezhu Yang
  0 siblings, 0 replies; 22+ messages in thread
From: Tiezhu Yang @ 2022-01-07 12:41 UTC (permalink / raw)
  To: Tom Tromey
  Cc: gdb-patches, Qing zhang, Xuefeng Li, Chenghua Xu, Zhensong Liu,
	Youling Tang



On 1/7/22 05:41, Tom Tromey wrote:
>>>>>> ">" == Tiezhu Yang <yangtiezhu@loongson.cn> writes:
> 
>>> This patchset only adds the minimal changes as simple as possible, the
>>> basic command "run", "break", "continue", "next", "step" and "quit" can
>>> be used to debug.
> 
>>> This is the first step, we will submit other more patches step by step
>>> in the future, these small patches make an easily understood change that
>>> can be verified by reviewers, any comments will be much appreciated.
> 
> Oops, sorry, I read the patches and neglected to read the cover letter
> first.  I see now what you're up to, and this plan sounds fine to me.
> Don't worry about the test results I asked about.

Hi Tom,

Thank you very much for your reply. I will send v2 in the next week.

Thanks,
Tiezhu

> 
> thanks,
> Tom


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 5/6] gdb: LoongArch: Add Makefile and configure
  2021-12-09 11:36 ` [PATCH 5/6] gdb: LoongArch: Add Makefile and configure Tiezhu Yang
  2022-01-06 21:39   ` Tom Tromey
@ 2022-01-10 12:06   ` Andrew Burgess
  1 sibling, 0 replies; 22+ messages in thread
From: Andrew Burgess @ 2022-01-10 12:06 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: gdb-patches

* Tiezhu Yang <yangtiezhu@loongson.cn> [2021-12-09 19:36:00 +0800]:

> This commit adds Makefile and configure for LoongArch.
> 
> Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
> Signed-off-by: Qing zhang <zhangqing@loongson.cn>
> Signed-off-by: Youling Tang <tangyouling@loongson.cn>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>  gdb/Makefile.in       |  9 +++++++++
>  gdb/configure.host    |  3 +++
>  gdb/configure.nat     |  4 ++++
>  gdb/configure.tgt     | 11 +++++++++++
>  gdb/features/Makefile |  4 ++++
>  5 files changed, 31 insertions(+)
> 
> diff --git a/gdb/Makefile.in b/gdb/Makefile.in
> index bff2757..d492882 100644
> --- a/gdb/Makefile.in
> +++ b/gdb/Makefile.in
> @@ -733,6 +733,7 @@ ALL_TARGET_OBS = \
>  	arch/arm-get-next-pcs.o \
>  	arch/arm-linux.o \
>  	arch/i386.o \
> +	arch/loongarch.o \
>  	arch/ppc-linux-common.o \
>  	arch/riscv.o \
>  	arm-bsd-tdep.o \
> @@ -783,6 +784,9 @@ ALL_TARGET_OBS = \
>  	linux-record.o \
>  	linux-tdep.o \
>  	lm32-tdep.o \
> +	loongarch-tdep.o \
> +	loongarch-linux-tdep.o \
> +	loongarch-linux-nat.o \
>  	m32c-tdep.o \
>  	m32r-linux-tdep.o \
>  	m32r-tdep.o \
> @@ -1359,6 +1363,7 @@ HFILES_NO_SRCDIR = \
>  	linux-record.h \
>  	linux-tdep.h \
>  	location.h \
> +	loongarch-tdep.h \
>  	m2-lang.h \
>  	m32r-tdep.h \
>  	m68k-tdep.h \
> @@ -1493,6 +1498,7 @@ HFILES_NO_SRCDIR = \
>  	arch/arc.h \
>  	arch/arm.h \
>  	arch/i386.h \
> +	arch/loongarch.h \
>  	arch/ppc-linux-common.h \
>  	arch/ppc-linux-tdesc.h \
>  	arch/riscv.h \
> @@ -2234,6 +2240,9 @@ ALLDEPFILES = \
>  	linux-record.c \
>  	linux-tdep.c \
>  	lm32-tdep.c \
> +	loongarch-tdep.c \
> +	loongarch-linux-tdep.c \
> +	loongarch-linux-nat.c \
>  	m32r-linux-nat.c \
>  	m32r-linux-tdep.c \
>  	m32r-tdep.c \
> diff --git a/gdb/configure.host b/gdb/configure.host
> index be40cc8..da71675 100644
> --- a/gdb/configure.host
> +++ b/gdb/configure.host
> @@ -58,6 +58,7 @@ arc*)			gdb_host_cpu=arc ;;
>  arm*)			gdb_host_cpu=arm ;;
>  hppa*)			gdb_host_cpu=pa ;;
>  i[34567]86*)		gdb_host_cpu=i386 ;;
> +loongarch*)		gdb_host_cpu=loongarch ;;
>  m68*)			gdb_host_cpu=m68k ;;
>  mips*)			gdb_host_cpu=mips ;;
>  powerpc* | rs6000)	gdb_host_cpu=powerpc ;;
> @@ -117,6 +118,8 @@ i[34567]86-*-cygwin*)	gdb_host=cygwin ;;
>  
>  ia64-*-linux*)		gdb_host=linux ;;
>  
> +loongarch*-linux*)	gdb_host=linux ;;
> +
>  m68*-*-linux*)		gdb_host=linux ;;
>  m68*-*-netbsd* | m68*-*-knetbsd*-gnu)
>  			gdb_host=nbsdelf ;;
> diff --git a/gdb/configure.nat b/gdb/configure.nat
> index bdd0503..148c5ca 100644
> --- a/gdb/configure.nat
> +++ b/gdb/configure.nat
> @@ -258,6 +258,10 @@ case ${gdb_host} in
>  		# Host: Intel IA-64 running GNU/Linux
>  		NATDEPFILES="${NATDEPFILES} ia64-linux-nat.o"
>  		;;
> +	    loongarch)
> +		# Host: LoongArch, running GNU/Linux.
> +		NATDEPFILES="${NATDEPFILES} loongarch-linux-nat.o linux-nat-trad.o"
> +		;;
>  	    m32r)
>  		# Host: M32R based machine running GNU/Linux
>  		NATDEPFILES="${NATDEPFILES} m32r-linux-nat.o"
> diff --git a/gdb/configure.tgt b/gdb/configure.tgt
> index 56cdfef..65f3bf6 100644
> --- a/gdb/configure.tgt
> +++ b/gdb/configure.tgt
> @@ -91,6 +91,11 @@ ia64*-*-*)
>  	cpu_obs="ia64-tdep.o"
>  	;;
>  
> +loongarch*-*-*)
> +	# Target: LoongArch baremetal
> +	cpu_obs="loongarch-tdep.o arch/loongarch.o"
> +	;;
> +
>  riscv*-*-*)
>  	cpu_obs="riscv-tdep.o riscv-none-tdep.o arch/riscv.o \
>  	         ravenscar-thread.o riscv-ravenscar-thread.o";;
> @@ -341,6 +346,12 @@ lm32-*-*)
>  	gdb_target_obs="lm32-tdep.o" 
>  	;;
>  
> +loongarch*-*-linux*)
> +	# Target: LoongArch running Linux
> +	gdb_target_obs="loongarch-linux-tdep.o glibc-tdep.o \
> +			linux-tdep.o solib-svr4.o"
> +	;;
> +
>  m32c-*-*)
>  	# Target: Renesas M32C family
>  	gdb_target_obs="m32c-tdep.o"
> diff --git a/gdb/features/Makefile b/gdb/features/Makefile
> index 311176a..c5ad2cb 100644
> --- a/gdb/features/Makefile
> +++ b/gdb/features/Makefile
> @@ -69,6 +69,7 @@ WHICH = mips-linux mips-dsp-linux \
>  	tic6x-c64xp-linux tic6x-c64x-linux tic6x-c62x-linux
>  
>  # Record which registers should be sent to GDB by default after stop.
> +loongarch-expedite = r3,pc

Is this actually used anywhere?  I'm not hugely familiar with how this
is used, but it looks like it might be used when generating the
regformat/*.dat files, which not all targets do.  Is it used somewhere
else?

If it's not used, or not used yet, I'd rather its introduction was put
off until we need it.

Thanks,
Andrew


>  mips-expedite = r29,pc
>  mips-dsp-expedite = r29,pc
>  mips64-expedite = r29,pc
> @@ -173,6 +174,7 @@ GDB = false
>  aarch64-feature = 1
>  arm-feature = 1
>  i386-feature = 1
> +loongarch-feature = 1
>  riscv-feature = 1
>  tic6x-feature = 1
>  
> @@ -226,6 +228,8 @@ FEATURE_XMLFILES = aarch64-core.xml \
>  	i386/64bit-pkeys.xml \
>  	i386/64bit-sse.xml \
>  	i386/x32-core.xml \
> +	loongarch/base32.xml \
> +	loongarch/base64.xml \
>  	riscv/rv32e-xregs.xml \
>  	riscv/32bit-cpu.xml \
>  	riscv/32bit-fpu.xml \
> -- 
> 2.1.0
> 
> 


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 1/6] gdb: LoongArch: Add initial target description support
  2021-12-09 11:35 ` [PATCH 1/6] gdb: LoongArch: Add initial target description support Tiezhu Yang
  2022-01-06 21:19   ` Tom Tromey
@ 2022-01-10 14:04   ` Andrew Burgess
  2022-01-11 23:14     ` Tiezhu Yang
  1 sibling, 1 reply; 22+ messages in thread
From: Andrew Burgess @ 2022-01-10 14:04 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: gdb-patches

* Tiezhu Yang <yangtiezhu@loongson.cn> [2021-12-09 19:35:56 +0800]:

> This commit adds initial target description support for LoongArch.
> 
> Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
> Signed-off-by: Qing zhang <zhangqing@loongson.cn>
> Signed-off-by: Youling Tang <tangyouling@loongson.cn>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>  gdb/arch/loongarch.c              | 47 +++++++++++++++++++++++++++++++++++++++
>  gdb/arch/loongarch.h              | 30 +++++++++++++++++++++++++
>  gdb/doc/gdb.texinfo               | 10 +++++++++
>  gdb/features/loongarch/base32.c   | 47 +++++++++++++++++++++++++++++++++++++++
>  gdb/features/loongarch/base32.xml | 45 +++++++++++++++++++++++++++++++++++++
>  gdb/features/loongarch/base64.c   | 47 +++++++++++++++++++++++++++++++++++++++
>  gdb/features/loongarch/base64.xml | 45 +++++++++++++++++++++++++++++++++++++
>  7 files changed, 271 insertions(+)
>  create mode 100644 gdb/arch/loongarch.c
>  create mode 100644 gdb/arch/loongarch.h
>  create mode 100644 gdb/features/loongarch/base32.c
>  create mode 100644 gdb/features/loongarch/base32.xml
>  create mode 100644 gdb/features/loongarch/base64.c
>  create mode 100644 gdb/features/loongarch/base64.xml
> 
> diff --git a/gdb/arch/loongarch.c b/gdb/arch/loongarch.c
> new file mode 100644
> index 0000000..bddabc7
> --- /dev/null
> +++ b/gdb/arch/loongarch.c
> @@ -0,0 +1,47 @@
> +/* Copyright (C) 2021 Free Software Foundation, Inc.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   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 "defs.h"
> +#include "arch/loongarch.h"
> +
> +#include <../features/loongarch/base32.c>
> +#include <../features/loongarch/base64.c>
> +
> +target_desc *
> +loongarch_create_target_description (int rlen)
> +{
> +  int regnum = 0;
> +  target_desc_up tdesc = allocate_target_description ();
> +
> +  set_tdesc_architecture (tdesc.get (), rlen == 64 ? "loongarch64" : "loongarch32");
> +
> +  if (rlen == 64)
> +    regnum = create_feature_loongarch_base64 (tdesc.get (), regnum);
> +  else
> +    regnum = create_feature_loongarch_base32 (tdesc.get (), regnum);
> +
> +  return tdesc.release ();
> +}
> +
> +target_desc *
> +loongarch_get_base_target_description (int rlen)
> +{
> +  if (rlen == 64)
> +    return loongarch_create_target_description (64);
> +  else
> +    return loongarch_create_target_description (32);
> +}

I'm pretty sure that this code is not correct.  GDB relies on the same
target description (as in the xml string) corresponding to the same
target_desc object.

You can see why this is needed in gdbarch_list_lookup_by_info, where
we compare the target_desc pointer when looking up a previous
gdbarch.  If the target_desc is different then each time you start a
new inferior you'll create a new gdbarch object.

If you look in tdesc_parse_xml you'll see how GDB uses a cache to
ensure target descriptions fetched from the remote are singletons, and
if you look in riscv_lookup_target_description (arch/risc.c) you'll
see how we do a similar thing for native risc-v inferiors.

I would recommend adopting the risc-v code, this was already reused by
ARC recently, so it should probably do for you too (see arch/arc.c).

Thanks,
Andrew

> diff --git a/gdb/arch/loongarch.h b/gdb/arch/loongarch.h
> new file mode 100644
> index 0000000..eddb685
> --- /dev/null
> +++ b/gdb/arch/loongarch.h
> @@ -0,0 +1,30 @@
> +/*
> +   Copyright (C) 2021 Free Software Foundation, Inc.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
> +
> +#ifndef ARCH_LOONGARCH_H
> +#define ARCH_LOONGARCH_H
> +
> +#include <stdint.h>
> +#include "elf/loongarch.h"
> +#include "opcode/loongarch.h"
> +
> +struct target_desc;
> +extern struct target_desc *loongarch_get_base_target_description (int rlen);
> +extern struct target_desc *loongarch_create_target_description (int rlen);
> +
> +#endif
> diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
> index d0c5bcf..12ee3ee 100644
> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -46172,6 +46172,7 @@ registers using the capitalization used in the description.
>  * ARC Features::
>  * ARM Features::
>  * i386 Features::
> +* LoongArch Features::
>  * MicroBlaze Features::
>  * MIPS Features::
>  * M68K Features::
> @@ -46400,6 +46401,15 @@ The @samp{org.gnu.gdb.i386.pkeys} feature is optional.  It should
>  describe a single register, @samp{pkru}.  It is a 32-bit register
>  valid for i386 and amd64.
>  
> +@node LoongArch Features
> +@subsection LoongArch Features
> +@cindex target descriptions, LoongArch Features
> +
> +The @samp{org.gnu.gdb.loongarch.base} feature is required for LoongArch
> +targets.  It should contain the registers @samp{r0} through @samp{r31},
> +@samp{pc}, and @samp{badvaddr}.  Either the architectural names (@samp{r0},
> +@samp{r1}, etc) can be used, or the ABI names (@samp{zero}, @samp{ra}, etc).
> +
>  @node MicroBlaze Features
>  @subsection MicroBlaze Features
>  @cindex target descriptions, MicroBlaze features
> diff --git a/gdb/features/loongarch/base32.c b/gdb/features/loongarch/base32.c
> new file mode 100644
> index 0000000..b6f2d06
> --- /dev/null
> +++ b/gdb/features/loongarch/base32.c
> @@ -0,0 +1,47 @@
> +/* THIS FILE IS GENERATED.  -*- buffer-read-only: t -*- vi:set ro:
> +  Original: base32.xml */
> +
> +#include "gdbsupport/tdesc.h"
> +
> +static int
> +create_feature_loongarch_base32 (struct target_desc *result, long regnum)
> +{
> +  struct tdesc_feature *feature;
> +
> +  feature = tdesc_create_feature (result, "org.gnu.gdb.loongarch.base");
> +  tdesc_create_reg (feature, "r0", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r1", regnum++, 1, "general", 32, "code_ptr");
> +  tdesc_create_reg (feature, "r2", regnum++, 1, "general", 32, "data_ptr");
> +  tdesc_create_reg (feature, "r3", regnum++, 1, "general", 32, "data_ptr");
> +  tdesc_create_reg (feature, "r4", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r5", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r6", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r7", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r8", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r9", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r10", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r11", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r12", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r13", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r14", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r15", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r16", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r17", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r18", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r19", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r20", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r21", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r22", regnum++, 1, "general", 32, "data_ptr");
> +  tdesc_create_reg (feature, "r23", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r24", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r25", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r26", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r27", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r28", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r29", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r30", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "r31", regnum++, 1, "general", 32, "uint32");
> +  tdesc_create_reg (feature, "pc", regnum++, 1, "general", 32, "code_ptr");
> +  tdesc_create_reg (feature, "badvaddr", regnum++, 1, "general", 32, "code_ptr");
> +  return regnum;
> +}
> diff --git a/gdb/features/loongarch/base32.xml b/gdb/features/loongarch/base32.xml
> new file mode 100644
> index 0000000..0afe81b
> --- /dev/null
> +++ b/gdb/features/loongarch/base32.xml
> @@ -0,0 +1,45 @@
> +<?xml version="1.0"?>
> +<!-- Copyright (C) 2021 Free Software Foundation, Inc.
> +     Contributed by Loongson Ltd.
> +
> +     Copying and distribution of this file, with or without modification,
> +     are permitted in any medium without royalty provided the copyright
> +     notice and this notice are preserved.  -->
> +
> +<!DOCTYPE feature SYSTEM "gdb-target.dtd">
> +<feature name="org.gnu.gdb.loongarch.base">
> +  <reg name="r0" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r1" bitsize="32" type="code_ptr" group="general"/>
> +  <reg name="r2" bitsize="32" type="data_ptr" group="general"/>
> +  <reg name="r3" bitsize="32" type="data_ptr" group="general"/>
> +  <reg name="r4" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r5" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r6" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r7" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r8" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r9" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r10" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r11" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r12" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r13" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r14" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r15" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r16" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r17" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r18" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r19" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r20" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r21" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r22" bitsize="32" type="data_ptr" group="general"/>
> +  <reg name="r23" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r24" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r25" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r26" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r27" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r28" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r29" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r30" bitsize="32" type="uint32" group="general"/>
> +  <reg name="r31" bitsize="32" type="uint32" group="general"/>
> +  <reg name="pc" bitsize="32" type="code_ptr" group="general"/>
> +  <reg name="badvaddr" bitsize="32" type="code_ptr" group="general"/>
> +</feature>
> diff --git a/gdb/features/loongarch/base64.c b/gdb/features/loongarch/base64.c
> new file mode 100644
> index 0000000..3ee2d9a
> --- /dev/null
> +++ b/gdb/features/loongarch/base64.c
> @@ -0,0 +1,47 @@
> +/* THIS FILE IS GENERATED.  -*- buffer-read-only: t -*- vi:set ro:
> +  Original: base64.xml */
> +
> +#include "gdbsupport/tdesc.h"
> +
> +static int
> +create_feature_loongarch_base64 (struct target_desc *result, long regnum)
> +{
> +  struct tdesc_feature *feature;
> +
> +  feature = tdesc_create_feature (result, "org.gnu.gdb.loongarch.base");
> +  tdesc_create_reg (feature, "r0", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r1", regnum++, 1, "general", 64, "code_ptr");
> +  tdesc_create_reg (feature, "r2", regnum++, 1, "general", 64, "data_ptr");
> +  tdesc_create_reg (feature, "r3", regnum++, 1, "general", 64, "data_ptr");
> +  tdesc_create_reg (feature, "r4", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r5", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r6", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r7", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r8", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r9", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r10", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r11", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r12", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r13", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r14", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r15", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r16", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r17", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r18", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r19", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r20", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r21", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r22", regnum++, 1, "general", 64, "data_ptr");
> +  tdesc_create_reg (feature, "r23", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r24", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r25", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r26", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r27", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r28", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r29", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r30", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "r31", regnum++, 1, "general", 64, "uint64");
> +  tdesc_create_reg (feature, "pc", regnum++, 1, "general", 64, "code_ptr");
> +  tdesc_create_reg (feature, "badvaddr", regnum++, 1, "general", 64, "code_ptr");
> +  return regnum;
> +}
> diff --git a/gdb/features/loongarch/base64.xml b/gdb/features/loongarch/base64.xml
> new file mode 100644
> index 0000000..b53479f
> --- /dev/null
> +++ b/gdb/features/loongarch/base64.xml
> @@ -0,0 +1,45 @@
> +<?xml version="1.0"?>
> +<!-- Copyright (C) 2021 Free Software Foundation, Inc.
> +     Contributed by Loongson Ltd.
> +
> +     Copying and distribution of this file, with or without modification,
> +     are permitted in any medium without royalty provided the copyright
> +     notice and this notice are preserved.  -->
> +
> +<!DOCTYPE feature SYSTEM "gdb-target.dtd">
> +<feature name="org.gnu.gdb.loongarch.base">
> +  <reg name="r0" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r1" bitsize="64" type="code_ptr" group="general"/>
> +  <reg name="r2" bitsize="64" type="data_ptr" group="general"/>
> +  <reg name="r3" bitsize="64" type="data_ptr" group="general"/>
> +  <reg name="r4" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r5" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r6" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r7" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r8" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r9" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r10" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r11" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r12" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r13" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r14" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r15" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r16" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r17" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r18" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r19" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r20" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r21" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r22" bitsize="64" type="data_ptr" group="general"/>
> +  <reg name="r23" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r24" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r25" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r26" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r27" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r28" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r29" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r30" bitsize="64" type="uint64" group="general"/>
> +  <reg name="r31" bitsize="64" type="uint64" group="general"/>
> +  <reg name="pc" bitsize="64" type="code_ptr" group="general"/>
> +  <reg name="badvaddr" bitsize="64" type="code_ptr" group="general"/>
> +</feature>
> -- 
> 2.1.0
> 
> 


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 2/6] gdb: LoongArch: Add initial baremetal support
  2021-12-09 11:35 ` [PATCH 2/6] gdb: LoongArch: Add initial baremetal support Tiezhu Yang
  2022-01-06 21:27   ` Tom Tromey
@ 2022-01-10 14:28   ` Andrew Burgess
  1 sibling, 0 replies; 22+ messages in thread
From: Andrew Burgess @ 2022-01-10 14:28 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: gdb-patches

* Tiezhu Yang <yangtiezhu@loongson.cn> [2021-12-09 19:35:57 +0800]:

> This commit adds initial baremetal support for LoongArch.
> 
> Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
> Signed-off-by: Qing zhang <zhangqing@loongson.cn>
> Signed-off-by: Youling Tang <tangyouling@loongson.cn>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>

Thanks for this patch.  All references to NULL should be replaced with
nullptr, I'll not point out each one below.  I have some other notes
which you'll find inline.

> ---
>  gdb/loongarch-tdep.c | 270 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  gdb/loongarch-tdep.h |  62 ++++++++++++
>  2 files changed, 332 insertions(+)
>  create mode 100644 gdb/loongarch-tdep.c
>  create mode 100644 gdb/loongarch-tdep.h
> 
> diff --git a/gdb/loongarch-tdep.c b/gdb/loongarch-tdep.c
> new file mode 100644
> index 0000000..3069752
> --- /dev/null
> +++ b/gdb/loongarch-tdep.c
> @@ -0,0 +1,270 @@
> +/* Target-dependent code for GNU/Linux LoongArch.
> +
> +   Copyright (C) 2021 Free Software Foundation, Inc.
> +   Contributed by Loongson Ltd.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   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 "defs.h"
> +#include "arch-utils.h"
> +#include "dwarf2/frame.h"
> +#include "elf-bfd.h"
> +#include "frame-unwind.h"
> +#include "loongarch-tdep.h"
> +#include "target-descriptions.h"
> +#include "trad-frame.h"
> +#include "user-regs.h"
> +
> +/* Implement the loongarch_skip_prologue gdbarch method.  */
> +
> +static CORE_ADDR
> +loongarch_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
> +{
> +  CORE_ADDR func_addr;
> +
> +  /* See if we can determine the end of the prologue via the symbol table.
> +     If so, then return either PC, or the PC after the prologue, whichever
> +     is greater.  */
> +  if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
> +    {
> +      CORE_ADDR post_prologue_pc
> +	= skip_prologue_using_sal (gdbarch, func_addr);
> +      if (post_prologue_pc != 0)
> +	return std::max (pc, post_prologue_pc);
> +    }
> +
> +  return 0;
> +}
> +
> +/* Adjust the address downward (direction of stack growth) so that it
> +   is correctly aligned for a new stack frame.  */
> +static CORE_ADDR
> +loongarch_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)

Missing blank line after the header comment.

> +{
> +  return align_down (addr, 16);
> +}
> +
> +/* Generate, or return the cached frame cache for the loongarch frame unwinder.  */
> +
> +static struct trad_frame_cache *
> +loongarch_frame_cache (struct frame_info *this_frame, void **this_cache)
> +{
> +  struct gdbarch *gdbarch = get_frame_arch (this_frame);
> +  struct trad_frame_cache *cache;
> +  CORE_ADDR pc;
> +
> +  if (*this_cache != NULL)
> +    return (struct trad_frame_cache *) *this_cache;
> +
> +  cache = trad_frame_cache_zalloc (this_frame);
> +  *this_cache = cache;
> +
> +  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
> +  auto regs = tdep->regs;
> +  trad_frame_set_reg_realreg (cache, regs.ra, -2 /* TF_REG_UNKNOWN */);

I'm struggling with this line.  TF_REG_UNKNOWN was removed in commit
098caef485a4, so I suspect it might be the case that this code needs
to be updated.  I haven't figured out if there's an appropriate
function to call, but I suspect we somehow need to end up calling
trad_frame_saved_reg::set_unknown(), but maybe you need to add a new
wrapper function to that operates on the cache?

> +  trad_frame_set_reg_realreg (cache, gdbarch_pc_regnum (gdbarch), regs.ra);
> +
> +  pc = get_frame_address_in_block (this_frame);
> +  trad_frame_set_id (cache, frame_id_build_unavailable_stack (pc));
> +
> +  return cache;
> +}
> +
> +/* Implement the this_id callback for loongarch frame unwinder.  */
> +
> +static void
> +loongarch_frame_this_id (struct frame_info *this_frame, void **prologue_cache,
> +			 struct frame_id *this_id)
> +{
> +  struct trad_frame_cache *info;
> +
> +  info = loongarch_frame_cache (this_frame, prologue_cache);
> +  trad_frame_get_id (info, this_id);
> +}
> +
> +/* Implement the prev_register callback for loongarch frame unwinder.  */
> +
> +static struct value *
> +loongarch_frame_prev_register (struct frame_info *this_frame,
> +			       void **prologue_cache, int regnum)
> +{
> +  struct trad_frame_cache *info;
> +
> +  info = loongarch_frame_cache (this_frame, prologue_cache);
> +  return trad_frame_get_register (info, this_frame, regnum);
> +}
> +
> +static const struct frame_unwind loongarch_frame_unwind = {
> +  "loongarch prologue",
> +  /*.type	   =*/NORMAL_FRAME,
> +  /*.stop_reason   =*/default_frame_unwind_stop_reason,
> +  /*.this_id	   =*/loongarch_frame_this_id,
> +  /*.prev_register =*/loongarch_frame_prev_register,
> +  /*.unwind_data   =*/NULL,
> +  /*.sniffer	   =*/default_frame_sniffer,
> +  /*.dealloc_cache =*/NULL,
> +  /*.prev_arch	   =*/NULL,
> +};
> +
> +static int
> +loongarch_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)

Missing a header comment.

> +{
> +  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
> +  auto regs = tdep->regs;
> +
> +  if (0 <= num && num < 32)
> +    return regs.r + num;
> +  else
> +    return -1;
> +}
> +
> +constexpr gdb_byte loongarch_default_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};
> +typedef BP_MANIPULATION (loongarch_default_breakpoint) loongarch_breakpoint;
> +
> +static struct gdbarch *
> +loongarch_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
> +{
> +  struct gdbarch *gdbarch;
> +  struct loongarch_gdbarch_tdep tdep_instant, *tdep;
> +  tdesc_arch_data_up tdesc_data;
> +  const struct target_desc *tdesc = info.target_desc;
> +  int i;
> +  size_t regnum;

The GDB style is no longer to declare all variables at the start of
the block.  You should move as many of these as possible to the point
of first use.  I think gdbarch, tdesc_data, i, regnum can definitively
be deferred.

> +
> +  tdep = &tdep_instant;
> +  memset (tdep, 0, sizeof (tdep));
> +  memset (&tdep->regs, -1, sizeof (tdep->regs));
> +
> +  if (info.abfd != NULL
> +      && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
> +    {
> +      int eflags = elf_elfheader (info.abfd)->e_flags;
> +      unsigned char eclass = elf_elfheader (info.abfd)->e_ident[EI_CLASS];
> +
> +      if (eflags) /* Executable file  */

These comments should be moved into the following blocks.

> +        {
> +          tdep->ef_abi = eflags & EF_LOONGARCH_ABI;
> +        }
> +      else /* Core file  */
> +        {
> +          if (eclass == ELFCLASS64)
> +            tdep->ef_abi = EF_LOONGARCH_ABI_LP64;
> +          else
> +            tdep->ef_abi = EF_LOONGARCH_ABI_ILP32;
> +        }
> +    }
> +  else
> +    tdep->ef_abi = EF_LOONGARCH_ABI_LP64;
> +
> +  /* Check any target description for validity.  */
> +  if (!tdesc_has_registers (tdesc))
> +    tdesc = loongarch_get_base_target_description (
> +      EF_LOONGARCH_IS_ILP32 (tdep->ef_abi) ? 32 : 64);

GDB style is to place the '(' on the next line, so

	tdesc = loongarch_get_base_target_description
	  (EF_LOONGARCH_IS_ILP32 (tdep->ef_abi) ? 32 : 64);

Though if it were me, to avoid the long line, I'd do:

  if (!tdesc_has_registers (tdesc))
    {
      int register_length = EF_LOONGARCH_IS_ILP32 (tdep->ef_abi) ? 32 : 64;
      tdesc = loongarch_get_base_target_description (register_length);
    }

But that's just a suggestion.

> +
> +  int valid_p = 1;

This should be a bool.

> +  const struct tdesc_feature *feature;
> +
> +  feature = tdesc_find_feature (tdesc, "org.gnu.gdb.loongarch.base");
> +  if (feature == NULL)
> +    return NULL;
> +
> +  regnum = 0;
> +  tdesc_data = tdesc_data_alloc ();
> +
> +  tdep->regs.r = regnum;
> +  /* Validate the description provides the mandatory base registers
> +     and allocate their numbers.  */
> +  for (i = 0; i < 32; i++)

You can move the declaration of i into this for loop.

> +    valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), regnum++,
> +                                        loongarch_r_normal_name[i] + 1);
> +  valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
> +                                      tdep->regs.pc = regnum++, "pc");
> +  valid_p &= tdesc_numbered_register (feature, tdesc_data.get (),
> +                                      tdep->regs.badvaddr = regnum++, "badvaddr");
> +  if (!valid_p)
> +    return NULL;
> +
> +  /* LoongArch code is always little-endian.  */
> +  info.byte_order_for_code = BFD_ENDIAN_LITTLE;
> +
> +  /* Find a candidate among the list of pre-declared architectures.  */
> +  for (arches = gdbarch_list_lookup_by_info (arches, &info); arches != NULL;
> +       arches = gdbarch_list_lookup_by_info (arches->next, &info))
> +    {
> +      loongarch_gdbarch_tdep *candidate_tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);

This line is too long, you should split it like:

  loongarch_gdbarch_tdep *candidate_tdep
    = (loongarch_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);

> +
> +      if (candidate_tdep->ef_abi != tdep->ef_abi)
> +        continue;
> +
> +      return arches->gdbarch;
> +    }
> +
> +  /* None found, so create a new architecture from the information provided.  */
> +  tdep = (struct loongarch_gdbarch_tdep *) xmalloc (sizeof (tdep_instant));
> +  memcpy (tdep, &tdep_instant, sizeof (tdep_instant));
> +  gdbarch = gdbarch_alloc (&info, tdep);
> +
> +  /* Target data types.  */
> +  set_gdbarch_short_bit (gdbarch, 16);
> +  set_gdbarch_int_bit (gdbarch, 32);
> +  set_gdbarch_long_bit (gdbarch, loongarch_rlen(gdbarch));
> +  set_gdbarch_long_long_bit (gdbarch, 64);
> +  set_gdbarch_ptr_bit (gdbarch, loongarch_rlen(gdbarch));
> +  set_gdbarch_char_signed (gdbarch, 0);
> +
> +  info.target_desc = tdesc;
> +  info.tdesc_data = tdesc_data.get ();
> +
> +  /* Information about registers.  */
> +  tdep->regs.ra = tdep->regs.r + 1;
> +  tdep->regs.sp = tdep->regs.r + 3;
> +  set_gdbarch_num_regs (gdbarch, regnum);
> +  set_gdbarch_sp_regnum (gdbarch, tdep->regs.sp);
> +  set_gdbarch_pc_regnum (gdbarch, tdep->regs.pc);
> +
> +  /* Finalise the target description registers.  */
> +  tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
> +
> +  /* Advance PC across function entry code.  */
> +  set_gdbarch_skip_prologue (gdbarch, loongarch_skip_prologue);
> +
> +  /* Stack grows downward.  */
> +  set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
> +
> +  /* Frame info.  */
> +  set_gdbarch_frame_align (gdbarch, loongarch_frame_align);
> +
> +  /* Breakpoint manipulation.  */
> +  set_gdbarch_breakpoint_kind_from_pc (gdbarch, loongarch_breakpoint::kind_from_pc);
> +  set_gdbarch_sw_breakpoint_from_kind (gdbarch, loongarch_breakpoint::bp_from_kind);
> +
> +  /* Frame unwinders. Use DWARF debug info if available, otherwise use our own unwinder.  */
> +  set_gdbarch_dwarf2_reg_to_regnum (gdbarch, loongarch_dwarf2_reg_to_regnum);
> +  dwarf2_append_unwinders (gdbarch);
> +  frame_unwind_append_unwinder (gdbarch, &loongarch_frame_unwind);
> +
> +  /* Hook in OS ABI-specific overrides, if they have been registered.  */
> +  gdbarch_init_osabi (info, gdbarch);
> +
> +  return gdbarch;
> +}
> +
> +void _initialize_loongarch_tdep ();
> +void
> +_initialize_loongarch_tdep ()
> +{
> +  gdbarch_register (bfd_arch_loongarch, loongarch_gdbarch_init, NULL);
> +}
> diff --git a/gdb/loongarch-tdep.h b/gdb/loongarch-tdep.h
> new file mode 100644
> index 0000000..50f2156
> --- /dev/null
> +++ b/gdb/loongarch-tdep.h
> @@ -0,0 +1,62 @@
> +/* Target-dependent code for GNU/Linux LoongArch.
> +
> +   Copyright (C) 2021 Free Software Foundation, Inc.
> +   Contributed by Loongson Ltd.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
> +
> +#ifndef LOONGARCH_TDEP_H
> +#define LOONGARCH_TDEP_H
> +
> +#include "gdbarch.h"
> +#include "arch/loongarch.h"
> +
> +#include <regset.h>
> +#include <sys/procfs.h>
> +
> +extern const struct regset loongarch_elf_gregset;

Missing a comment.

> +
> +/* Target-dependent structure in gdbarch.  */
> +struct loongarch_gdbarch_tdep : gdbarch_tdep
> +{
> +  int ef_abi; /* EF_LOONGARCH_ABI  */
> +
> +  struct
> +  {
> +    int r;
> +    int ra;
> +    int sp;
> +    int pc;
> +    int badvaddr;
> +  } regs;
> +};

I think every field in this structure needs a comment.  It's possible
that a single comment for 'regs' might be enough if its descriptive
enough.

> +
> +static inline int
> +loongarch_rlen (struct gdbarch *gdbarch)

Missing a header comment.

> +{
> +  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);
> +
> +  if (EF_LOONGARCH_IS_LP64 (tdep->ef_abi))
> +    return 64;
> +  else if (EF_LOONGARCH_IS_ILP32 (tdep->ef_abi))
> +    return 32;
> +  else
> +    gdb_assert_not_reached ("unknown ABI");
> +
> +  return 0;
> +}
> +
> +#endif /* LOONGARCH_TDEP_H  */
> -- 
> 2.1.0
> 
> 

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 3/6] gdb: LoongArch: Add initial linux target support
  2021-12-09 11:35 ` [PATCH 3/6] gdb: LoongArch: Add initial linux target support Tiezhu Yang
  2022-01-06 21:31   ` Tom Tromey
@ 2022-01-10 14:34   ` Andrew Burgess
  1 sibling, 0 replies; 22+ messages in thread
From: Andrew Burgess @ 2022-01-10 14:34 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: gdb-patches

* Tiezhu Yang <yangtiezhu@loongson.cn> [2021-12-09 19:35:58 +0800]:

> This commit adds initial linux target support for LoongArch.
> 
> Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
> Signed-off-by: Qing zhang <zhangqing@loongson.cn>
> Signed-off-by: Youling Tang <tangyouling@loongson.cn>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>

Like Tom, I can't check the details of the actual implementation, but
so long as the code is up to standard, I don't think it really
matters, I'm sure you'll fix any bugs that come to light.

I do have some coding standard issues that could do with being fixed
though, notes inline below.

> ---
>  gdb/loongarch-linux-tdep.c | 147 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 147 insertions(+)
>  create mode 100644 gdb/loongarch-linux-tdep.c
> 
> diff --git a/gdb/loongarch-linux-tdep.c b/gdb/loongarch-linux-tdep.c
> new file mode 100644
> index 0000000..40b8008
> --- /dev/null
> +++ b/gdb/loongarch-linux-tdep.c
> @@ -0,0 +1,147 @@
> +/* Target-dependent code for GNU/Linux LoongArch.
> +
> +   Copyright (C) 2021 Free Software Foundation, Inc.
> +   Contributed by Loongson Ltd.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   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 "defs.h"
> +#include "glibc-tdep.h"
> +#include "inferior.h"
> +#include "linux-tdep.h"
> +#include "loongarch-tdep.h"
> +#include "solib-svr4.h"
> +#include "target-descriptions.h"
> +
> +/* Unpack an elf_gregset_t into GDB's register cache.  */
> +
> +static void
> +loongarch_supply_elf_gregset (const struct regset *r,
> +			      struct regcache *regcache, int regno,
> +			      const void *gprs, size_t len)
> +{
> +  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());

Long line, should be split at the '='.

> +  auto regs = tdep->regs;
> +  gdb_assert (0 <= regs.r && sizeof (elf_gregset_t) <= len);
> +
> +  int regsize = register_size (regcache->arch (), regs.r);
> +  const gdb_byte *buf = NULL;

Again s/NULL/nullptr/ throughout.

> +
> +  if (regno == -1)
> +    {
> +      /* Set $r0 = 0.  */
> +      regcache->raw_supply_zeroed (regs.r);
> +
> +      for (int i = 1; i < 32; i++)
> +	{
> +	  buf = (const gdb_byte*)gprs + regsize * i;

Should be a space after the cast, before gprs.  In fact, I notice
there's a lot of this formatting issue below, I'll not point them all
out, but they all need fixing.

> +	  regcache->raw_supply (regs.r + i, (const void *)buf);
> +	}
> +
> +      /* Size base (pc) = regsize * regs.pc.  */
> +      buf = (const gdb_byte*)gprs + regsize * regs.pc;
> +      regcache->raw_supply (regs.pc, (const void *)buf);
> +
> +      /* Size base (badvaddr) = regsize * regs.badvaddr.  */
> +      buf = (const gdb_byte*)gprs + regsize * regs.badvaddr;
> +      regcache->raw_supply (regs.badvaddr, (const void *)buf);
> +    }
> +  else if (regs.r == regno)
> +    regcache->raw_supply_zeroed (regno);
> +  else if ((regs.r < regno && regno < regs.r + 32) ||
> +	   (regs.pc == regno) || (regs.badvaddr == regno))

The '||' needs to be at the start of the following line, not the end
of the previous line.

> +    {
> +      /* Offset offset (regno) = regsize * (regno - regs.r).  */
> +      buf = (const gdb_byte*)gprs + regsize * (regno - regs.r);
> +      regcache->raw_supply (regno, (const void *)buf);
> +    }
> +}
> +
> +/* Pack the GDB's register cache value into an elf_gregset_t.  */
> +
> +static void
> +loongarch_fill_elf_gregset (const struct regset *r,
> +			    const struct regcache *regcache, int regno,
> +			    void *gprs, size_t len)
> +{
> +  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());

Line length.

> +  auto regs = tdep->regs;
> +  gdb_assert (0 <= regs.r && sizeof (elf_gregset_t) <= len);
> +  int regsize = register_size (regcache->arch (), regs.r);
> +  gdb_byte *buf = NULL;
> +
> +  if (regno == -1)
> +    {
> +      for (int i = 0; i < 32; i++)
> +	{
> +	  buf = (gdb_byte *)gprs + regsize * i;
> +	  regcache->raw_collect (regs.r + i, (void *)buf);
> +	}
> +
> +      /* Size base (pc) = regsize * regs.pc.  */
> +      buf = (gdb_byte *)gprs + regsize * regs.pc;
> +      regcache->raw_collect (regs.pc, (void *)buf);
> +
> +      /* Size base (badvaddr) = regsize * regs.badvaddr.  */
> +      buf = (gdb_byte *)gprs + regsize * regs.badvaddr;
> +      regcache->raw_collect (regs.badvaddr, (void *)buf);
> +    }
> +  else if ((regs.r <= regno && regno < regs.r + 32) ||
> +	   (regs.pc == regno) || (regs.badvaddr == regno))
> +    {
> +      /* Offset offset (regno) = regsize * (regno - regs.r).  */
> +      buf = (gdb_byte *)gprs + regsize * (regno - regs.r);
> +      regcache->raw_collect (regno, (void *)buf);
> +    }
> +}
> +
> +const struct regset loongarch_elf_gregset =
> +{
> +  NULL,
> +  loongarch_supply_elf_gregset,
> +  loongarch_fill_elf_gregset,
> +};

Should have a header comment, even a simple one.

> +
> +static void
> +loongarch_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)

Again, should have a header comment.

> +{
> +  linux_init_abi (info, gdbarch, 0);
> +
> +  set_solib_svr4_fetch_link_map_offsets (gdbarch, loongarch_rlen(gdbarch) == 32
> +					 ? linux_ilp32_fetch_link_map_offsets
> +					 : linux_lp64_fetch_link_map_offsets);
> +
> +  /* GNU/Linux uses SVR4-style shared libraries.  */
> +  set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
> +
> +  /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
> +  set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
> +
> +  /* Enable TLS support.  */
> +  set_gdbarch_fetch_tls_load_module_address (gdbarch, svr4_fetch_objfile_link_map);
> +}
> +
> +/* Initialize LoongArch Linux target support.  */
> +
> +void _initialize_loongarch_linux_tdep ();
> +void
> +_initialize_loongarch_linux_tdep ()
> +{
> +  gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch32,
> +                          GDB_OSABI_LINUX, loongarch_linux_init_abi);
> +  gdbarch_register_osabi (bfd_arch_loongarch, bfd_mach_loongarch64,
> +                          GDB_OSABI_LINUX, loongarch_linux_init_abi);
> +}
> -- 
> 2.1.0
> 
> 

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 4/6] gdb: LoongArch: Add initial native linux support
  2021-12-09 11:35 ` [PATCH 4/6] gdb: LoongArch: Add initial native linux support Tiezhu Yang
  2022-01-06 21:36   ` Tom Tromey
@ 2022-01-10 14:38   ` Andrew Burgess
  1 sibling, 0 replies; 22+ messages in thread
From: Andrew Burgess @ 2022-01-10 14:38 UTC (permalink / raw)
  To: Tiezhu Yang; +Cc: gdb-patches

* Tiezhu Yang <yangtiezhu@loongson.cn> [2021-12-09 19:35:59 +0800]:

> This commit adds initial native linux support for LoongArch.
> 
> Signed-off-by: Zhensong Liu <liuzhensong@loongson.cn>
> Signed-off-by: Qing zhang <zhangqing@loongson.cn>
> Signed-off-by: Youling Tang <tangyouling@loongson.cn>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>

Again s/NULL/nullptr/ throughout.

> ---
>  gdb/loongarch-linux-nat.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 175 insertions(+)
>  create mode 100644 gdb/loongarch-linux-nat.c
> 
> diff --git a/gdb/loongarch-linux-nat.c b/gdb/loongarch-linux-nat.c
> new file mode 100644
> index 0000000..b35c3e2
> --- /dev/null
> +++ b/gdb/loongarch-linux-nat.c
> @@ -0,0 +1,175 @@
> +/* Target-dependent code for GNU/Linux LoongArch.
> +
> +   Copyright (C) 2021 Free Software Foundation, Inc.
> +   Contributed by Loongson Ltd.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   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 "defs.h"
> +#include "elf/common.h"
> +#include "gregset.h"
> +#include "inferior.h"
> +#include "linux-nat-trad.h"
> +#include "loongarch-tdep.h"
> +#include "nat/gdb_ptrace.h"
> +#include "target-descriptions.h"
> +
> +#include <asm/ptrace.h>
> +
> +/* LoongArch Linux native additions to the default linux support.  */
> +
> +class loongarch_linux_nat_target final : public linux_nat_trad_target
> +{
> +public:
> +  /* Add our register access methods.  */
> +  void fetch_registers (struct regcache *, int) override;
> +  void store_registers (struct regcache *, int) override;
> +
> +protected:
> +  /* Override linux_nat_trad_target methods.  */
> +  CORE_ADDR register_u_offset (struct gdbarch *gdbarch, int regno,
> +                               int store_p) override;
> +};
> +
> +/* Fill GDB's register array with the general-purpose, pc and badvaddr
> +   register values from the current thread.  */
> +
> +static void
> +fetch_gregs_from_thread (struct regcache *regcache, int regno, pid_t tid)
> +{
> +  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());

Line length, split at '='.

> +  auto regs = tdep->regs;
> +  elf_gregset_t regset;
> +
> +  if ((regno == -1) || (regs.r <= regno && regno < regs.r + 32) ||
> +      (regs.pc == regno) || (regs.badvaddr == regno))

Move '||' to the start of the following line.

> +  {
> +    struct iovec iovec = { .iov_base = &regset, .iov_len = sizeof (regset) };
> +
> +    if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, (long) &iovec) < 0)
> +      perror_with_name (_("Couldn't get NT_PRSTATUS registers"));
> +    else
> +      loongarch_elf_gregset.supply_regset (NULL, regcache, regno, &regset,
> +					   sizeof (regset));
> +  }
> +}
> +
> +/* Store to the current thread the valid general-purpose, pc and badvaddr
> +   register values in the GDB's register array.  */
> +
> +static void
> +store_gregs_to_thread (struct regcache *regcache, int regno, pid_t tid)
> +{
> +  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (regcache->arch ());

Long line.

> +  auto regs = tdep->regs;
> +  elf_gregset_t regset;
> +
> +  if ((regno == -1) || (regs.r <= regno && regno < regs.r + 32) ||
> +      (regs.pc == regno) || (regs.badvaddr == regno))

Move '||'.

> +  {
> +    struct iovec iovec = { .iov_base = &regset, .iov_len = sizeof (regset) };
> +
> +    if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, (long) &iovec) < 0)
> +      perror_with_name (_("Couldn't get NT_PRSTATUS registers"));
> +    else
> +      {
> +	loongarch_elf_gregset.collect_regset (NULL, regcache, regno, &regset,
> +					      sizeof (regset));
> +	if (ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS, (long) &iovec) < 0)
> +	  perror_with_name (_("Couldn't set NT_PRSTATUS registers"));
> +      }
> +  }
> +}
> +
> +/* Implement the "fetch_registers" target_ops method.  */
> +
> +void
> +loongarch_linux_nat_target::fetch_registers (struct regcache *regcache,
> +					     int regno)
> +{
> +  pid_t tid = get_ptrace_pid (regcache->ptid ());
> +
> +  fetch_gregs_from_thread(regcache, regno, tid);
> +}
> +
> +/* Implement the "store_registers" target_ops method.  */
> +
> +void
> +loongarch_linux_nat_target::store_registers (struct regcache *regcache,
> +					     int regno)
> +{
> +  pid_t tid = get_ptrace_pid (regcache->ptid ());
> +
> +  store_gregs_to_thread(regcache, regno, tid);
> +}
> +
> +/* Return the address in the core dump or inferior of register REGNO.  */
> +
> +CORE_ADDR
> +loongarch_linux_nat_target::register_u_offset (struct gdbarch *gdbarch,
> +					       int regno, int store_p)
> +{
> +  loongarch_gdbarch_tdep *tdep = (loongarch_gdbarch_tdep *) gdbarch_tdep (gdbarch);

Long line.

> +  auto regs = tdep->regs;
> +
> +  /* according to <asm/ptrace.h> */

Capital letter at the start of the comment please.

> +  if (0 <= regs.r && regs.r <= regno && regno < regs.r + GPR_NUM)
> +    return GPR_BASE + regno - regs.r;
> +  else if (regs.pc == regno)
> +    return PC;
> +  else
> +    return -1;
> +}
> +
> +static loongarch_linux_nat_target the_loongarch_linux_nat_target;
> +
> +/* Wrapper functions.  These are only used by libthread_db.  */
> +
> +void
> +supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregset)
> +{
> +  loongarch_elf_gregset.supply_regset (NULL, regcache, -1, gregset,
> +				       sizeof (gdb_gregset_t));
> +}
> +
> +void
> +fill_gregset (const struct regcache *regcache, gdb_gregset_t *gregset,
> +	      int regno)
> +{
> +  loongarch_elf_gregset.collect_regset (NULL, regcache, regno, gregset,
> +					sizeof (gdb_gregset_t));
> +}
> +
> +void
> +supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregset)
> +{
> +}
> +
> +void
> +fill_fpregset (const struct regcache *regcache, gdb_fpregset_t *fpregset,
> +	       int regno)
> +{
> +}
> +
> +/* Initialize LoongArch Linux native support.  */
> +
> +void _initialize_loongarch_linux_nat ();
> +void
> +_initialize_loongarch_linux_nat ()
> +{
> +  linux_target = &the_loongarch_linux_nat_target;
> +  add_inf_child_target (&the_loongarch_linux_nat_target);
> +}
> -- 
> 2.1.0
> 
> 

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 22+ messages in thread

* Re: [PATCH 1/6] gdb: LoongArch: Add initial target description support
  2022-01-10 14:04   ` Andrew Burgess
@ 2022-01-11 23:14     ` Tiezhu Yang
  0 siblings, 0 replies; 22+ messages in thread
From: Tiezhu Yang @ 2022-01-11 23:14 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches



On 1/10/22 22:04, Andrew Burgess wrote:
> * Tiezhu Yang <yangtiezhu@loongson.cn> [2021-12-09 19:35:56 +0800]:
> 
>> This commit adds initial target description support for LoongArch.
[snip]
> 
> I'm pretty sure that this code is not correct.  GDB relies on the same
> target description (as in the xml string) corresponding to the same
> target_desc object.
> 
> You can see why this is needed in gdbarch_list_lookup_by_info, where
> we compare the target_desc pointer when looking up a previous
> gdbarch.  If the target_desc is different then each time you start a
> new inferior you'll create a new gdbarch object.
> 
> If you look in tdesc_parse_xml you'll see how GDB uses a cache to
> ensure target descriptions fetched from the remote are singletons, and
> if you look in riscv_lookup_target_description (arch/risc.c) you'll
> see how we do a similar thing for native risc-v inferiors.
> 
> I would recommend adopting the risc-v code, this was already reused by
> ARC recently, so it should probably do for you too (see arch/arc.c).

Hi Andrew,

Thanks very much for your suggestions, I will look into it.

And also I will modify the related code as you suggested in the other 
patches.

Thanks,
Tiezhu


^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2022-01-11 23:14 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-09 11:35 [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
2021-12-09 11:35 ` [PATCH 1/6] gdb: LoongArch: Add initial target description support Tiezhu Yang
2022-01-06 21:19   ` Tom Tromey
2022-01-10 14:04   ` Andrew Burgess
2022-01-11 23:14     ` Tiezhu Yang
2021-12-09 11:35 ` [PATCH 2/6] gdb: LoongArch: Add initial baremetal support Tiezhu Yang
2022-01-06 21:27   ` Tom Tromey
2022-01-10 14:28   ` Andrew Burgess
2021-12-09 11:35 ` [PATCH 3/6] gdb: LoongArch: Add initial linux target support Tiezhu Yang
2022-01-06 21:31   ` Tom Tromey
2022-01-10 14:34   ` Andrew Burgess
2021-12-09 11:35 ` [PATCH 4/6] gdb: LoongArch: Add initial native linux support Tiezhu Yang
2022-01-06 21:36   ` Tom Tromey
2022-01-10 14:38   ` Andrew Burgess
2021-12-09 11:36 ` [PATCH 5/6] gdb: LoongArch: Add Makefile and configure Tiezhu Yang
2022-01-06 21:39   ` Tom Tromey
2022-01-10 12:06   ` Andrew Burgess
2021-12-09 11:36 ` [PATCH 6/6] gdb: LoongArch: Update ChangeLog and NEWS Tiezhu Yang
2022-01-06 21:40   ` Tom Tromey
2021-12-15  8:36 ` [PATCH 0/6] gdb: Add basic support for LoongArch Tiezhu Yang
2022-01-06 21:41 ` Tom Tromey
2022-01-07 12:41   ` Tiezhu Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).