我应该使用哪些gdb命令来缩小在“ main”标签中出现分段错误的位置? [英] What gdb commands should I use to narrow down where in label 'main' did I get the segmentation fault?

查看:131
本文介绍了我应该使用哪些gdb命令来缩小在“ main”标签中出现分段错误的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's my assembly code and my main subroutine. Here are my macros and constants:

             .text
fmt:         .string "x \t\t ln(x)\n"
sfmt:        .string "%.10lf \t %.10lf\n"
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。如何缩小该信号在主要位置的位置?
PS我只知道GDB的基础知识。

I already used gdb however, it only points out that the SIGSEGV signal came from 0x0000000000420358 in main(). How can I narrow down where in 'main' this signal comes from? P.S I only know the basics of GDB.

GDB内容:(更新)

GDB Stuff:(update)

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

我不知道这是否有帮助,但这是C版本。我将其转换为程序集,因为那是我需要提交的内容。此外,由于多数民众赞成在作弊,因此我们无法使用任何类型的转换器。

I don't know if this helps but this is the C version THAT WORKS. I am converting it to assembly because thats what I need to hand in. Also we cannot use any types of converter since thats considered cheating.

#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 \t\t 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 %.10lf\n",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 ,因此只能包含全零字节。 (而且它将无法执行。)

Turns out your main is in the .bss section, not .text where it belongs, so it can only contain all-zero bytes. (And it won't be executable).

GDB通常只想反汇编 .text 部分中的代码。

GDB normally only wants to disassemble code in the .text section so that also explains GDB being weird.

这就是为什么您应该将代码简化为MCVE( Minimal / complete / verifiable example)的原因使它尽可能小,同时仍能解决问题。

This is why you should reduce your code to a MCVE (Minimal / complete / verifiable example) to make it as small as possible while still containing the problem.

这篇关于我应该使用哪些gdb命令来缩小在“ main”标签中出现分段错误的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆