You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/08/19 08:33:26 UTC

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a change in pull request #4367: riscv/backtrace: add up_backtrace support

xiaoxiang781216 commented on a change in pull request #4367:
URL: https://github.com/apache/incubator-nuttx/pull/4367#discussion_r691902891



##########
File path: arch/risc-v/src/common/riscv_backtrace.c
##########
@@ -0,0 +1,165 @@
+/****************************************************************************
+ * arch/risc-v/src/common/riscv_backtrace.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 "sched/sched.h"
+
+#include "riscv_internal.h"
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: getfp
+ *
+ * Description:
+ *  getfp() returns current frame pointer
+ *
+ ****************************************************************************/
+
+static inline uintptr_t getfp(void)
+{
+  register uint64_t fp;
+
+  __asm__
+  (
+    "\tadd  %0, x0, fp\n"
+    : "=r"(fp)
+  );
+
+  return (uintptr_t)fp;
+}
+
+/****************************************************************************
+ * Name: in_code_region
+ *
+ * Description:
+ *  in_code_region() verify the program counter is within code section
+ *
+ ****************************************************************************/
+
+static bool in_code_region(FAR void *pc)
+{
+  if ((uintptr_t)pc >= (uintptr_t)_START_TEXT &&
+      (uintptr_t)pc <  (uintptr_t)_END_TEXT)
+    {
+      return true;
+    }
+
+  return false;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: up_backtrace
+ *
+ * Description:
+ *  up_backtrace()  returns  a backtrace for the TCB, in the array
+ *  pointed to by buffer.  A backtrace is the series of currently active
+ *  function calls for the program.  Each item in the array pointed to by
+ *  buffer is of type void *, and is the return address from the
+ *  corresponding stack frame.  The size argument specifies the maximum
+ *  number of addresses that can be stored in buffer.   If  the backtrace is
+ *  larger than size, then the addresses corresponding to the size most
+ *  recent function calls are returned; to obtain the complete backtrace,
+ *  make sure that buffer and size are large enough.
+ *
+ * Input Parameters:
+ *   tcb    - Address of the task's TCB
+ *   buffer - Return address from the corresponding stack frame
+ *   size   - Maximum number of addresses that can be stored in buffer
+ *
+ * Returned Value:
+ *   up_backtrace() returns the number of addresses returned in buffer
+ *
+ ****************************************************************************/
+
+int up_backtrace(FAR struct tcb_s *tcb, FAR void **buffer, int size)
+{
+  FAR struct tcb_s *rtcb;
+  FAR uintptr_t *fp;
+  FAR uintptr_t *ra;
+  irqstate_t flags;
+  int i = 0;
+
+  if (!tcb || size <= 0 || !buffer)
+    {
+      return 0;
+    }
+
+  flags = enter_critical_section();

Review comment:
       let's hold the critical section only when dump the different thread.




-- 
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-unsubscribe@nuttx.apache.org

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