簡體   English   中英

過程中出現“分段錯誤(核心已轉儲)”

[英]"Segmentation fault (core dumped)" in procedure

我和 gdb-peda 一起去了,它說錯誤是在 inner_loop label 中產生的。程序應該做兩個方陣的乘法

`

matrix_mult:
pushl %ebp        # save the value of ebp on the stack
movl %esp, %ebp   # set up the stack frame 
    
xorl %eax, %eax   # initialize the loop counter
movl 8(%ebp), %ebx # get the pointer to matrix A
movl 12(%ebp), %ecx # get the pointer to matrix B
movl 16(%ebp), %edx # get the pointer to matrix C
movl 20(%ebp), %esi # get the value of N

matrix_loop:
pushl %esi        # save the value of esi on the stack
movl $0, %esi     # initialize the inner loop counter
movl $0, %ebp     # initialize the result for this element

inner_loop:
movl (%ebx,%esi,4), %eax # get the element from matrix A
mull (%ecx,%esi,4) # multiply it by the element from matrix B
addl %eax, %ebp   # add the result to ebp
addl $1, %esi       # increment the loop counter
73: movl 20(%ebp),%edi 
74: cmpl %edi, %esi # compare to N
75: jl inner_loop     # if the loop counter is less than N, jump back to the beginning of the loop

#At this point, ebp contains the result for this element of the matrix
movl %ebp, (%edx) # store the result in the output matrix
addl $4, %edx     # move to the next element in the output matrix
popl %esi         # restore the value of esi
incl %eax           # increment the loop counter
movl 20(%ebp),%edi         
cmpl %edi, %eax # compare to N
jl matrix_loop    # if the loop counter is less than N, jump back to the beginning of the outer loop


popl %ebp
ret 

`

使用 gdb-peda 時,它表示它在第 73-75 行之間中斷,顯示如下內容:

=> 0x565561e4 <inner_loop+11>:  mov    edi,DWORD PTR [ebp+0x14]
   0x565561e7 <inner_loop+14>:  cmp    esi,edi
   0x565561e9 <inner_loop+16>:  jle    0x565561d9 <inner_loop>`

你問的是如何調試這個。

一種做法如下:

  1. Segmentation fault 是memory的讀寫沒有分配給進程導致的

  2. 找出故障的有效地址(不是指令,而是 memory 參考地址)。 當然,這將是非法的。

  3. 找出誰計算了該地址,並解決問題。

一方面,您要在代碼中尋找錯誤(或丟失)的指令,這可能是一個迭代過程:找到處理錯誤數據的好代碼。 因此,您必須找到生成錯誤數據的代碼(另一次迭代)並重復直到找到出錯的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM