I wrote a program to copy the memory content of FFFF0000-FFFFFFFF to a file, but it says "Segmentation fault", (i use AMD64 lenny, so the address is long), how could i fix it? thanks!
#include <string.h> #include <stdio.h>
void CopyMemoryToFile(char* FilePath, long StartAddress, long OffSet) { FILE* Memory; Memory=fopen (FilePath, "w"); if(Memory!=NULL) { void* Start; Start=StartAddress; fwrite(Start, 1, OffSet, Memory); } fclose(Memory);
} int main (void) { CopyMemoryToFile("~/cgi-bin_d/Memory",0xFFFF0000,0xFFFF); return 0; }
Quoting Star Liu minxinjianxin@gmail.com:
I wrote a program to copy the memory content of FFFF0000-FFFFFFFF to a file, but it says "Segmentation fault", (i use AMD64 lenny, so the address is long), how could i fix it? thanks!
you should only fclose() if Memory != NULL, so your function would be better off like this:
void CopyMemoryToFile(char* FilePath, long StartAddress, long OffSet) { FILE* Memory; Memory=fopen (FilePath, "w"); if(Memory!=NULL) { void* Start; Start=StartAddress; fwrite(Start, 1, OffSet, Memory); fclose(Memory); } }
Please do stop cross-posting.
On Mon, Jul 14, 2008 at 05:31:18PM +0800, Star Liu wrote:
I wrote a program to copy the memory content of FFFF0000-FFFFFFFF to a file, but it says "Segmentation fault", (i use AMD64 lenny, so the address is long), how could i fix it?
In Linux your program is running in protected mode, so all memory addresses are virtual and not physical. The CPU has a paging table which is used for translating between virtual and physical. In Linux, mmap() is used to request access to a physical address space. Your application does not have anything set up at virtual address 0xffff0000 so the kernel rewards it with SIGSEGV as usual. SIGSEGV always means that an application has tried to access virtual memory that it did not have access to.
flashrom uses mmap() correctly already. Let's continue that thread.
//Peter