GDB在C语言编程中的应用

GDB在C语言编程中的应用,gdb,Gdb,我有一个C语言程序的问题。我需要将局部变量的绝对内存地址发送到main函数。我正在用gdb调试 如何知道变量中是否存在溢出 多谢各位 #include <stdlib.h> #include <stdio.h> #include <stdint.h> #include <string.h> #include <ctype.h> typedef int16_t int16; #include "mulaw.h" void deco

我有一个C语言程序的问题。我需要将局部变量的绝对内存地址发送到main函数。我正在用gdb调试

如何知道变量中是否存在溢出

多谢各位

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>

typedef int16_t int16;

#include "mulaw.h"


void decodeFile(FILE * fIn, FILE * fOut, uint32_t samples) {

uint32_t i;

uint8_t * inSamples = malloc(samples * sizeof(uint8_t));
int16_t * outSamples = malloc(samples * sizeof(int16_t));

fread(inSamples, sizeof(uint8_t), samples, fIn);

for (i = 0; i < samples; i++) {
    outSamples[i] = 4 * muLaw[inSamples[i]];
}

fwrite(outSamples, sizeof(int16_t), samples, fOut);

free(inSamples);
free(outSamples);
 }

#define MAX_FILE_SIZE 256

int main(int argc, char **argv) 
{

char inputFile[MAX_FILE_SIZE];
char outputFile[MAX_FILE_SIZE];

FILE * fIn = NULL, * fOut = NULL;

struct header_t {
    char ChunkID[4];
    int32_t ChunkSize;
    char    Format[4];
} header;

char SubchunkID[4];
uint32_t SubchunkSize;

struct subheader_t {
    int16_t AudioFormat;
    int16_t NumChannels;
    int32_t SampleRate;
    int32_t ByteRate;
    int16_t BlockAlign;
    int16_t BitsPerSample;
    int16_t ExtraParamSize;
    int16_t Padding;
} subheader;



/* Usage */
if (argc != 3) {
    puts("Usage is: mulaw INFILE OUTFILE\n");
    exit(EXIT_FAILURE);
}

/* Careful here!!!!! */
strncpy(inputFile, argv[1], MAX_FILE_SIZE);
strncpy(outputFile, argv[2], MAX_FILE_SIZE);

/* Open input file */
fIn = fopen (inputFile, "rb");

/* Read main header */
fread(&header, sizeof(struct header_t), 1, fIn);

if (memcmp(header.ChunkID, "RIFF", 4) != 0
    || memcmp(header.Format, "WAVE", 4) != 0) {

    fprintf(stderr, "Unknown input format\n");
    exit(EXIT_FAILURE);
}

/* Read sub header */

while (fread(SubchunkID, sizeof(SubchunkID), 1, fIn)) {
    fread(&SubchunkSize, sizeof(SubchunkSize), 1, fIn);

    printf("Reading chunk of type %c%c%c%c (%d bytes)\n", 
        isprint(SubchunkID[0]) ? SubchunkID[0] : '?',
        isprint(SubchunkID[1]) ? SubchunkID[1] : '?',
        isprint(SubchunkID[2]) ? SubchunkID[2] : '?',
        isprint(SubchunkID[3]) ? SubchunkID[3] : '?',
        (int) SubchunkSize);

    if (memcmp(SubchunkID, "fmt ", 4) == 0) {
        /* read a fmt_ header */
        fread(&subheader, SubchunkSize, 1, fIn);

        /* we are going to adjust this header now to change the audio format */
        if (subheader.AudioFormat != 7) {
            fprintf(stderr, "Only mu-law audio input is supported\n");
            exit(EXIT_FAILURE);
        }

        /* adjust audio format and bit depth */
        subheader.AudioFormat = 1;
        subheader.BitsPerSample = 16;

        /* fix derivative fields */
        subheader.ByteRate = subheader.SampleRate * subheader.NumChannels * subheader.BitsPerSample / 8;
        subheader.BlockAlign = subheader.NumChannels * subheader.BitsPerSample / 8;

        /* we don't write ExtraParamSize, because for AudioFormat == 1 it is not needed */
        SubchunkSize -= 2;

        /* Open file and write the header for our updated fmt_ chunk */
        fOut = fopen (outputFile, "wb");
        fwrite(&header, sizeof(struct header_t), 1, fOut);  /* Main header */

        fwrite(SubchunkID, sizeof(SubchunkID), 1, fOut);    /* Subheader */
        fwrite(&SubchunkSize, sizeof(SubchunkSize), 1, fOut);
        fwrite(&subheader, SubchunkSize, 1, fOut);

    } else if (memcmp(SubchunkID, "data", 4) == 0) {
        /* here is our mu-law data */

        /* write the header for our new chunk (it is twice as large) */
        int32_t tSubchunkSize = SubchunkSize * 2;
        fwrite(SubchunkID, sizeof(SubchunkID), 1, fOut);
        fwrite(&tSubchunkSize, sizeof(SubchunkSize), 1, fOut);

        /* process the data */

        (fIn, fOut, SubchunkSize);
    } else {
        /* unknown chunk, skipping */
        fseek(fIn, SubchunkSize, SEEK_CUR);
    }
}

/* Cleanup and exit */
fclose(fIn);
fclose(fOut);

exit(EXIT_SUCCESS);

只需运行
gdb yourexe

然后在gdb中:

break main
run
p &inputFile
这将为您提供
输入文件的地址:ex
$2=(char(*)[256])0x62fd60

要获取变量的大小,您可以使用
p sizeof(variable)
p sizeof(键入变量的类型)

也就是说,不确定它是否有助于您追踪您的问题。

请尝试澄清您的问题。描述您所做的事情和正在发生的事情,告诉我们您认为代码存在问题的原因。我们不知道你在你的计算机上做什么,也不知道你在看什么,也不知道我是如何看到变量的大小的。例如,inputFile直接显示,但例如p&fIn显示为:$3=(文件**)0x7fffffffe240.try
p(sizeof(fIn))
非常感谢Jean François。我还有一个问题。我想用gdb修改uint8_t*inSamples=malloc(samples*sizeof(uint8_t))以反转程序第31行中1073741824字节的内存。我必须用数据文件偏移量、旧值和新值填充表格。最好在gdb之外完成!你必须回答你的问题,因为它是从gdb上的问题开始的,现在是另外一个问题。这里不是这样的对不起Jean François。
break main
run
p &inputFile