Kee*_*oot 0 c debugging terminal lldb
我正在编写一个程序,它读取一个文件,一点一点地反转它,并将结果存储在一个新文件中,而不分配大于 1kb 的块。当我在终端中运行它时,它创建文件但不写入文件,而是崩溃并给出分段错误 11。当我尝试使用 lldb 调试它时,整个代码运行没有任何问题。我的终端遵循与 lldb 不同的分配规则吗?我该如何解决这个问题?
我已经用大文件和小文件运行了代码,但即使使用几乎空的 txt 文件,它也会崩溃。我正在使用 lldb-340.4.119 运行 osx 10.10.5
int const CHUNK_SIZE = 1024;
int chunk_index = 0;
int character;
char new_filename[] = "output";
struct Chunk {
struct Chunk *previous;
int data[(CHUNK_SIZE-sizeof(struct Chunk*))/sizeof(int)];
};
struct Chunk* memory = (struct Chunk *)malloc(sizeof(struct Chunk));
struct Chunk* temp;
FILE *fp;
fp = fopen(argv[1], "r");
// read file into memory
character = fgetc(fp);
do {
memory->data[chunk_index] = character;
chunk_index++;
if ( chunk_index*sizeof(int) > CHUNK_SIZE-sizeof(struct Chunk*)){
chunk_index = 0;
temp = (struct Chunk *)malloc(sizeof(struct Chunk));
temp->previous = memory;
memory = temp;
}
character = fgetc(fp);
}
while (character !=EOF);
chunk_index--;
fclose(fp);
// write to new file
fp = fopen(new_filename, "wb");
do {
while (chunk_index >=0) {
printf("%c", memory->data[chunk_index]);
fprintf(fp, "%c", memory->data[chunk_index]);
chunk_index--;
}
chunk_index = (CHUNK_SIZE-sizeof(struct Chunk*))/sizeof(int);
temp = memory;
memory = memory->previous;
free(temp);
} while(memory!=NULL);
Run Code Online (Sandbox Code Playgroud)
在调试器中运行与没有调试器运行之间的差异可能是由于调试器禁用了 ASLR。当您尝试调试它时,这可能会隐藏问题。
尝试撤消此操作。看来在LLDB中应该用命令来完成
settings set target.disable-aslr false
Run Code Online (Sandbox Code Playgroud)
这应该撤消 ASLR 的禁用。在 GDB 中将会是
set disable-randomization off
Run Code Online (Sandbox Code Playgroud)
不要忘记在调试器下重新启动程序(无需重新启动调试器!)以使此设置生效。在 GDB 中是run命令,LLDB 中应该有类似的命令。