如何在C中的程序中使用gdb

时间:2016-10-08 09:57:38

标签: c gdb

在第31行的C中的以下程序中,它完成了内存功能。我想在那行中保留1073741824的内存。我必须填写文件偏移量,旧值和新值的表。该程序有两个参数,一个是输入文件,另一个是输出文件。这两个文件都是.wav文件。 我认为我可以用gdb来做,但我不知道这样做。

该计划是:

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

#typedef int16_t int16;

#include "mulaw.h"

 /* This function reads 8 bit samples from fIn, decodes them
 * from G.711Mu into pcm_s16be, and writes them into fOut.
*/
void decodeFile(FILE * fIn, FILE * fOut, uint32_t samples) {

uint32_t i;

line 31 -> 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) {
/* Data structures needed later */
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;

/* Banner */
puts("       ___       _______  __    __       _______               ");
puts("      /   |     / _____/ /  |  /  |     / _____/               ");
puts("     / /| |    / /      /   | /   |    / /____                 ");
puts("    / /_| |   / /      / /| |/ /| |   / _____/  |   | |      /| |  /|  / ");
puts("   / ___  |  / /____  / / |___/ | |  / /____    |   | |     /_| | / | /  ");
puts("  /_/   |_| /______/ /_/        |_| /______/    |___| |___ /  | |/  |/ ");
puts("                                                |              ");
puts("                                                |              ");
puts("                                                               ");
puts("                                  [ACME G.711Mu (mu-law) decoder]     \n");

/* 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 */
        decodeFile(fIn, fOut, SubchunkSize);
    } else {
        /* unknown chunk, skipping */
        fseek(fIn, SubchunkSize, SEEK_CUR);
    }
}

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

exit(EXIT_SUCCESS);
}

0 个答案:

没有答案