xiaoxiang781216 commented on code in PR #7260:
URL: https://github.com/apache/incubator-nuttx/pull/7260#discussion_r990768956


##########
arch/arm/src/common/arm_backtrace_unwind.c:
##########
@@ -0,0 +1,680 @@
+/****************************************************************************
+ * arch/arm/src/common/arm_backtrace_unwind.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/arch.h>
+
+#include <arch/elf.h>
+
+#include "sched/sched.h"
+#include "arm_internal.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+enum regs
+{
+#ifdef CONFIG_ARM_THUMB
+  FP = 7,
+#else
+  FP = 11,
+#endif /* CONFIG_ARM_THUMB */
+  SP = 13,
+  LR = 14,
+  PC = 15
+};
+
+/****************************************************************************
+ * Private Data Types
+ ****************************************************************************/
+
+struct unwind_frame_s
+{
+  unsigned long fp;
+  unsigned long sp;
+  unsigned long lr;
+  unsigned long pc;
+
+  /* address of the LR value on the stack */
+
+  unsigned long *lr_addr;
+
+  /* highest value of sp allowed */
+
+  unsigned long stack_top;
+};
+
+struct unwind_ctrl_s
+{
+  unsigned long        vrs[16];   /* virtual register set */
+  const unsigned long *insn;      /* pointer to the current instructions word 
*/
+  unsigned long        stack_top; /* highest value of sp allowed */
+  unsigned long       *lr_addr;   /* address of LR value on the stack */
+  int                  entries;   /* number of entries left to interpret */
+  int                  byte;      /* current byte number in the instructions 
word */
+
+  /* 1 : check for stack overflow for each register pop.
+   * 0 : save overhead if there is plenty of stack remaining.
+   */
+
+  int check_each_pop;
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Convert a prel31 symbol to an absolute address */
+
+#define prel31_to_addr(ptr)  \
+  ({  \
+  /* sign-extend to 32 bits */  \
+  long offset = (((long)*(ptr)) << 1) >> 1;  \
+  (unsigned long)(ptr) + offset;  \
+  })
+
+/****************************************************************************
+ * Name: search_index
+ *
+ * Description:
+ *  Binary search in the unwind index. The entries are
+ *  guaranteed to be sorted in ascending order by the linker.
+ *
+ *  start    = first entry
+ *  origin   = first entry with positive offset
+ *             (or stop if there is no such entry)
+ *  stop - 1 = last entry
+ *
+ ****************************************************************************/
+
+nosanitize_address
+static const struct __EIT_entry *
+search_index(unsigned long addr, const struct __EIT_entry *start,
+             const struct __EIT_entry *origin,
+             const struct __EIT_entry *stop)
+{
+  unsigned long addr_prel31;
+
+  /* only search in the section with the matching sign. This way the
+   * prel31 numbers can be compared as unsigned longs.
+   */
+
+  if (addr < (unsigned long)start)
+    {
+      /* negative offsets: [start; origin) */
+
+      stop = origin;
+    }
+  else
+    {
+      /* positive offsets: [origin; stop) */
+
+      start = origin;
+    }
+
+  /* prel31 for address relavive to start */
+
+  addr_prel31 = (addr - (unsigned long)start) & 0x7fffffff;
+
+  while (start < stop - 1)
+    {
+      const struct __EIT_entry *mid = start + ((stop - start) >> 1);
+
+      /* As addr_prel31 is relative to start an offset is needed to
+       * make it relative to mid.
+       */
+
+      if (addr_prel31 -
+          ((unsigned long)mid - (unsigned long)start) < mid->fnoffset)
+        {
+          stop = mid;
+        }
+      else
+        {
+          /* keep addr_prel31 relative to start */
+
+          addr_prel31 -= ((unsigned long)mid - (unsigned long)start);
+          start = mid;
+        }
+    }
+
+  return (start->fnoffset <= addr_prel31) ? start : NULL;
+}
+
+nosanitize_address
+static const struct __EIT_entry *
+unwind_find_origin(const struct __EIT_entry *start,
+                   const struct __EIT_entry *stop)
+{
+  const struct __EIT_entry *mid;
+
+  while (start < stop)
+    {
+      mid = start + ((stop - start) >> 1);
+
+      if (mid->fnoffset >= 0x40000000)
+        {
+          /* negative offset */
+
+          start = mid + 1;
+        }
+      else
+        {
+          /* positive offset */
+
+          stop = mid;
+        }
+    }
+
+  return stop;
+}
+
+nosanitize_address
+static const struct __EIT_entry *unwind_find_entry(unsigned long addr)
+{
+  /* main unwind table */
+
+  return search_index(addr, __exidx_start,
+                      unwind_find_origin(__exidx_start, __exidx_end),
+                      __exidx_end);
+}
+
+nosanitize_address
+static unsigned long unwind_get_byte(struct unwind_ctrl_s *ctrl)
+{
+  unsigned long ret;
+
+  if (ctrl->entries <= 0)
+    {
+      return 0;
+    }
+
+  ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff;
+
+  if (ctrl->byte == 0)
+    {
+      ctrl->insn++;
+      ctrl->entries--;
+      ctrl->byte = 3;
+    }
+  else
+    {
+      ctrl->byte--;
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: unwind_pop_register
+ *
+ * Description:
+ *  Before poping a register check whether it is feasible or not
+ *
+ ****************************************************************************/
+
+nosanitize_address
+static int unwind_pop_register(struct unwind_ctrl_s *ctrl,
+                               unsigned long **vsp, unsigned int reg)
+{
+  if (ctrl->check_each_pop)
+    {
+      if (*vsp >= (unsigned long *)ctrl->stack_top)
+        {
+          return -1;
+        }
+    }
+
+  /* Use READ_ONCE_NOCHECK here to avoid this memory access

Review Comment:
   remove



##########
arch/arm/src/common/arm_backtrace_unwind.c:
##########
@@ -0,0 +1,680 @@
+/****************************************************************************
+ * arch/arm/src/common/arm_backtrace_unwind.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/arch.h>
+
+#include <arch/elf.h>
+
+#include "sched/sched.h"
+#include "arm_internal.h"
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+enum regs
+{
+#ifdef CONFIG_ARM_THUMB
+  FP = 7,
+#else
+  FP = 11,
+#endif /* CONFIG_ARM_THUMB */
+  SP = 13,
+  LR = 14,
+  PC = 15
+};
+
+/****************************************************************************
+ * Private Data Types
+ ****************************************************************************/
+
+struct unwind_frame_s
+{
+  unsigned long fp;
+  unsigned long sp;
+  unsigned long lr;
+  unsigned long pc;
+
+  /* address of the LR value on the stack */
+
+  unsigned long *lr_addr;
+
+  /* highest value of sp allowed */
+
+  unsigned long stack_top;
+};
+
+struct unwind_ctrl_s
+{
+  unsigned long        vrs[16];   /* virtual register set */
+  const unsigned long *insn;      /* pointer to the current instructions word 
*/
+  unsigned long        stack_top; /* highest value of sp allowed */
+  unsigned long       *lr_addr;   /* address of LR value on the stack */
+  int                  entries;   /* number of entries left to interpret */
+  int                  byte;      /* current byte number in the instructions 
word */
+
+  /* 1 : check for stack overflow for each register pop.
+   * 0 : save overhead if there is plenty of stack remaining.
+   */
+
+  int check_each_pop;
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Convert a prel31 symbol to an absolute address */
+
+#define prel31_to_addr(ptr)  \
+  ({  \
+  /* sign-extend to 32 bits */  \
+  long offset = (((long)*(ptr)) << 1) >> 1;  \
+  (unsigned long)(ptr) + offset;  \
+  })
+
+/****************************************************************************
+ * Name: search_index
+ *
+ * Description:
+ *  Binary search in the unwind index. The entries are
+ *  guaranteed to be sorted in ascending order by the linker.
+ *
+ *  start    = first entry
+ *  origin   = first entry with positive offset
+ *             (or stop if there is no such entry)
+ *  stop - 1 = last entry
+ *
+ ****************************************************************************/
+
+nosanitize_address
+static const struct __EIT_entry *
+search_index(unsigned long addr, const struct __EIT_entry *start,
+             const struct __EIT_entry *origin,
+             const struct __EIT_entry *stop)
+{
+  unsigned long addr_prel31;
+
+  /* only search in the section with the matching sign. This way the

Review Comment:
   Change all first char in comment to upper case



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to