c语言 - 我应该使用哪些 gdb 命令来缩小标签'main'中出现分段错误的位置?



这是我的汇编代码和我的主要子例程。 以下是我的宏和常量:

.text
fmt:         .string "x tt ln(x)n"
sfmt:        .string "%.10lf t %.10lfn"
error:       .string "Error"
filename:    .string "input.bin"
.data
LIM:         .double 0r1.0E-13
zero:        .double 0r0.0
one:         .double 0r1.0
half:        .double 0r0.5
define(i_r,w19)
define(j_r,w20)
define(n_r,w21)
define(fd_r,w22)
define(ln_x,d8)
define(cur_term,d24)
define(n_read,x25)
define(x_j,d26)
BUF_SIZE = 98*8
AT_FDCWD = -100
O_RDONLY = 0
buf_s = 16
.bss
x_arr:    .skip   BUF_SIZE
fp        .req    x29
lr        .req    x30
.balign 4
.global main

这是我的主要子例程:

main:
stp    fp,lr,[sp,-16]!
mov    fp,sp
ldp   fp,lr,[sp],16
ret

我已经使用了 gdb,但是,它只指出 SIGSEGV 信号来自 main(( 中的0x0000000000420358。如何缩小此信号在"主要"中的位置? 附言我只知道GDB的基础知识。

GDB 东西:(更新(

(gdb) x/i $pc
=> 0x420358:    .inst   0x00000000 ; undefined

我不知道这是否有帮助,但这是有效的 C 版本。我正在将其转换为组装,因为这是我需要交出的。此外,我们不能使用任何类型的转换器,因为这被认为是作弊。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>                                                                 //Used for the keyword for flags and other predefined values for the argument on openat,etc.
#define LIM         1.0e-13                                                     
#define DOUBSIZE    100                                                         //There are 97 double values in the binary file
#define buf_size    98*8                                                        
double x[DOUBSIZE];
int main() {
register int i = 1,j = 0,fd = openat(AT_FDCWD,"input.bin",O_RDONLY);    //int fd = openat(int dirfd,const char *pathname (basically a string),int flags,mode_t mode);
register double ln_x = 0.0,cur_term;
//double *x;        //(only local variable)                                         //(a local variable so it must be in the stack)only assuming there are 32 double precision values in the binary file
register long n_read = read(fd,&x,buf_size);                            //reads in 8 bytes(lost the double x[...] in this line since x is now pointing at the buffer
if(fd == -1) {
printf("Error!");
return 0;
}
if(n_read == -1) {                                                          //Error checker
printf("Error!");                           
return 0;                                   
}                                           
printf("x tt ln(x)n");                                                   //The header of the thing to be printed
while(j < (buf_size/8)) {                                                   //note that it is implied that EOF = -1 in C
if(x[j] <= 0.5) {                                                       //if x is less than or equal to 1/2,go to the next double value(assuming I don't know values in the bin file)
j++;
i = 1;
continue;
}
cur_term = (1.0/i) * (pow((double)((x[j]-1.0)/(x[j])),i));
ln_x += cur_term;
while(cur_term >= LIM) {                                                //continue to accumulate terms until the absolute value of the term is less than 1.0E-13
i++;                                                                //follows the pattern of the series.
cur_term = (1.0/i)*(pow((double)((x[j]-1.0)/(x[j])),i));            //since it should start with x[1]
ln_x += cur_term;                                                   //adds the new term to previous ln(x) value
}
printf("%.10lf t %.10lfn",x[j],ln_x);                                 //prints the current value of ln(x) and x
j++;                                                                    //manages which x double value will be used next
i = 1;
ln_x = 0.0;
}
close(fd);
return 0;
}

事实证明,您的main位于.bss部分,而不是.text它所属的位置,因此它只能包含全零字节。 (它不会是可执行的(。

GDB 通常只想在.text部分中反汇编代码,这也解释了 GDB 的奇怪之处。

这就是为什么您应该将代码简化为 MCVE(最小/完整/可验证示例(以使其尽可能小,同时仍然包含问题。

最新更新