首页 > 解决方案 > C程序中的分段错误

问题描述

您好,我需要一些帮助来解决分段错误错误。我目前正在使用内核 4.19.94-ti-r42 运行 debian 10.3 buster。我有一个 ac 程序,它在我的 beaglebone black 上调用 PRU(可编程实时单元)程序。c 程序是用以下方式编译的:

gcc ledbutton.c -g ‐lpthread ‐lprussdrv

c程序:

#include <stdio.h>
#include <stdlib.h>
#include <prussdrv.h>
#include <pruss_intc_mapping.h>
#include <unistd.h>

#define PRU_NUM 0   // using PRU0 for these examples

int main (void)
{
   if(getuid()!=0){
      printf("You must run this program as root. Exiting.\n");
      exit(EXIT_FAILURE);
   }
   // Initialize structure used by prussdrv_pruintc_intc
   // PRUSS_INTC_INITDATA is found in pruss_intc_mapping.h
   tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;

   // Allocate and initialize memory
   prussdrv_init ();
   prussdrv_open (PRU_EVTOUT_0);

   // Map PRU's interrupts
   prussdrv_pruintc_init(&pruss_intc_initdata);

   // Load and execute the PRU program on the PRU
   prussdrv_exec_program (PRU_NUM, "./ledButton.bin");

   // Wait for event completion from PRU, returns the PRU_EVTOUT_0 number
   int n = prussdrv_pru_wait_event (PRU_EVTOUT_0);
   printf("EBB PRU program completed, event number %d.\n", n);

   // Disable PRU and close memory mappings
   prussdrv_pru_disable(PRU_NUM);
   prussdrv_exit ();
   return EXIT_SUCCESS;
}

编译后我收到一个segmentation fault错误。所以我安装了一个 gdb 编译器来调试它并确定可能的错误来源。使用gdb ./a.out提供:

(gdb) run
Starting program: /home/debian/EBB_CH13/ledbutton/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/arm-linux-gnueabihf/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0xb6f9f470 in prussdrv_pruintc_init () from /usr/lib/libprussdrv.so
(gdb) backtrace
#0  0xb6f9f470 in prussdrv_pruintc_init () from /usr/lib/libprussdrv.so
#1  0x00400880 in main () at ledbutton.c:30

libprussdrv.so 的内容已加密,因此我无法查看。pru 程序 (ledbutton.bin) 位于同一目录中,是编译闪烁 LED 的汇编程序 (ledbutton.p) 的结果。ledbutton.p 代码是用以下代码编译的:

pasm ‐b ledButton.p

汇编代码:

.origin 0                        // start of program in PRU memory
.entrypoint START                // program entry point (for a debugger)

#define INS_PER_US   200         // 5ns per instruction
#define INS_PER_DELAY_LOOP 2     // two instructions per delay loop
                                 // set up a 50ms delay
#define DELAY  50 * 1000 * (INS_PER_US / INS_PER_DELAY_LOOP)

#define PRU0_R31_VEC_VALID 32    // allows notification of program completion
#define PRU_EVTOUT_0    3        // the event number that is sent back

START:
        SET     r30.t5           // turn on the output pin (LED on)
        MOV     r0, DELAY        // store the length of the delay in REG0
DELAYON:
        SUB     r0, r0, 1        // Decrement REG0 by 1
        QBNE    DELAYON, r0, 0   // Loop to DELAYON, unless REG0=0
LEDOFF:
        CLR     r30.t5           // clear the output bin (LED off)
        MOV     r0, DELAY        // Reset REG0 to the length of the delay
DELAYOFF:
        SUB     r0, r0, 1        // decrement REG0 by 1
        QBNE    DELAYOFF, r0, 0  // Loop to DELAYOFF, unless REG0=0

        QBBC    START, r31.t3    // is the button pressed? If not, loop

END:                             // notify the calling app that finished
        MOV     R31.b0, PRU0_R31_VEC_VALID | PRU_EVTOUT_0
        HALT                     // halt the pru program

任何帮助表示赞赏。如果您需要更多信息,请告诉我。谢谢你。

标签: cassemblysegmentation-fault

解决方案


推荐阅读