2012-10-12 104 views
1

下面的第三行返回-1。我應該怎樣做調試?失敗的「寫入」unix系統調用

void *zeroed_block = calloc(512, 1); 
lseek(3, sectorNum * 512, SEEK_SET); 
return write(fd, zeroed_block, 512); /* This line returns -1 */ 

當我打印造成的寫入(使用PERROR)錯誤的原因是「錯誤的文件描述符」

此處,我打開文件描述符代碼:

char *diskpath = argv[optind]; – Rose Perrone 1 min ago edit 
int fd = diskimg_open(diskpath, (removeFlag == NULL)); 

int 
diskimg_open (char *pathname, int readOnly) 
{ 
    return open(pathname, readOnly ? O_RDONLY : O_RDWR); 
} 

開幕在這三行代碼之外,文件描述符在我使用它的每隔一段時間都可靠地工作。

我在Mac OS,所以這裏的dtruss輸出,如要求:

SYSCALL(args)  = return 
open(".\0", 0x0, 0x1)  = 3 0 
fstat64(0x3, 0x7FFF63557420, 0x0)  = 0 0 
fcntl(0x3, 0x32, 0x7FFF635576A0)   = 0 0 
close(0x3)  = 0 0 
stat64("/Users/rdp/Documents/School/110/readfiles/code\0", 0x7FFF63557390, 0x0) = 0 0 
issetugid(0x7FFF63593530, 0x7FFF63557BFA, 0x7FFF63593530)  = 0 0 
csops(0x0, 0x0, 0x7FFF6355767C)  = 0 0 
shared_region_check_np(0x7FFF635555C8, 0x2, 0x55)  = 0 0 
stat64("/usr/lib/dtrace/libdtrace_dyld.dylib\0", 0x7FFF63556790, 0x7FFF63557680 = 0 0 
__sysctl(0x7FFF6355702C, 0x2, 0x7FFF63557018)  = 0 0 
bsdthread_register(0x7FFF911A6B68, 0x7FFF911A6B78, 0x2000)  = 0 0 
thread_selfid(0x7FFF911A6B68, 0x7FFF911A6B78, 0x0)  = 444904 0 
mmap(0x0, 0x2000, 0x3, 0x1002, 0x1000000, 0x4)  = 0x3961000 0 
mprotect(0x103961000, 0x88, 0x1)   = 0 0 
mmap(0x0, 0x17000, 0x3, 0x1002, 0x1000000, 0x6)  = 0x3969000 0 
mprotect(0x103969000, 0x1000, 0x0)  = 0 0 
mprotect(0x10397F000, 0x1000, 0x0)  = 0 0 
mmap(0x0, 0x17000, 0x3, 0x1002, 0x1000000, 0x6)  = 0x3980000 0 
mprotect(0x103980000, 0x1000, 0x0)  = 0 0 
mprotect(0x103996000, 0x1000, 0x0)  = 0 0 
mmap(0x0, 0x1000, 0x3, 0x1002, 0x1000000, 0x6)  = 0x3963000 0 
mprotect(0x103963000, 0x1000, 0x1)  = 0 0 
mprotect(0x103961000, 0x88, 0x3)   = 0 0 
mmap(0x7FA408400000, 0x200000, 0x3, 0x1002, 0x7000000, 0x6)  = 0x3997000 0 
munmap(0x103997000, 0x69000)   = 0 0 
munmap(0x103B00000, 0x97000)   = 0 0 
mprotect(0x103961000, 0x88, 0x1)   = 0 0 
getpid(0x7FFF63556E64, 0x3, 0x1)   = 58942 0 
__mac_syscall(0x7FFF8F05E7FE, 0x2, 0x7FFF63556EB0)  = 0 0 
stat64("/AppleInternal\0", 0x7FFF63556F18, 0x0)  = -1 Err#2 
audit_session_self(0x7FFF63556C68, 0xFFFFFFFFFFFFFFFC, 0x4)  = 5123 0 
geteuid(0x7FFF63556C68, 0xFFFFFFFFFFFFFFFC, 0x0)   = 0 0 
getegid(0x7FFF63556C68, 0xFFFFFFFFFFFFFFFC, 0x0)   = 0 0 
getaudit_addr(0x7FFF63556EB0, 0x30, 0x0)   = 0 0 
csops(0xE63E, 0x7, 0x7FFF63556AE0)  = 0 0 
mmap(0x0, 0x2000, 0x3, 0x1002, 0x1000000, 0x14)  = 0x3997000 0 
mprotect(0x103997000, 0x88, 0x1)   = 0 0 
mmap(0x0, 0x17000, 0x3, 0x1002, 0x1000000, 0x6)  = 0x3999000 0 
mprotect(0x103999000, 0x1000, 0x0)  = 0 0 
mprotect(0x1039AF000, 0x1000, 0x0)  = 0 0 
mmap(0x0, 0x17000, 0x3, 0x1002, 0x1000000, 0x6)  = 0x39B0000 0 
mprotect(0x1039B0000, 0x1000, 0x0)  = 0 0 
mprotect(0x1039C6000, 0x1000, 0x0)  = 0 0 
mprotect(0x103963000, 0x1000, 0x3)  = 0 0 
mprotect(0x103963000, 0x1000, 0x1)  = 0 0 
mprotect(0x103997000, 0x88, 0x3)   = 0 0 
mmap(0x7FA3F8400000, 0x200000, 0x3, 0x1002, 0x7000000, 0x7)  = 0xF8400000 0 
munmap(0x7FA3F8500000, 0x100000)   = 0 0 
mprotect(0x103997000, 0x88, 0x1)   = 0 0 
open("../3testdisks/basicDiskImage\0", 0x0, 0x1)   = 3 0 
lseek(0x3, 0x0, 0x0)   = 0 0 
read(0x3, "\a\001\0", 0x200)   = 512 0 
lseek(0x3, 0x200, 0x0)  = 512 0 
read(0x3, "\020\0", 0x200)  = 512 0 
lseek(0x3, 0x0, 0x2)   = 10240000 0 
getrlimit(0x1008, 0x7FFF63556F10, 0x10395B430)  = 0 0 
fstat64(0x1, 0x7FFF63556E78, 0x7FFF63556F3C)   = 0 0 
ioctl(0x1, 0x4004667A, 0x7FFF63556F14)  = 0 0 
mmap(0x7FA3F8400000, 0x1000000, 0x3, 0x1002, 0x2000000, 0x6)   = 0xF8500000 0 
munmap(0x7FA3F8500000, 0x300000)   = 0 0 
munmap(0x7FA3F9000000, 0x500000)   = 0 0 
write_nocancel(0x1, "Disk -t is 10240000 bytes (10000 KB)\n\0", 0x25)  = 37 0 
write_nocancel(0x1, "Superblock s_isize 16\n\0", 0x16)  = 22 0 
write_nocancel(0x1, "Superblock s_fsize 20000\n\0", 0x19)  = 25 0 
write_nocancel(0x1, "Superblock s_nfree 89\n\0", 0x16)  = 22 0 
write_nocancel(0x1, "Superblock s_ninode 0\n\0", 0x16)  = 22 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7ACC00, 0x0)  = 8047616 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7ACC00, 0x0)  = 8047616 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AC600, 0x0)  = 8046080 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AC600, 0x0)  = 8046080 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7ACC00, 0x0)  = 8047616 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7ACC00, 0x0)  = 8047616 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AC600, 0x0)  = 8046080 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AC600, 0x0)  = 8046080 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AA800, 0x0)  = 8038400 0 
read(0x3, "_<`<c<f<i<l<o<r<u<a<d<g<j<m<p<s<v<b<e<h<k<n<q<t<w<x<{<~<\201<\204<\207<\212<\215<y<|<\177<\202<\205<\210<\213<\216<z<}<\200<\203<\206<\211<\214<\217<\220<\223<\226<\231<\234<\237<\242<\245<\221<\224<\227<\232<\235<\240<\243<\246<\222<\225<\230<\233<\236<\241<\244<\247<\250<\253<\256<\261<\264<\267<\272<\275<\251<\254<\257<\262<\265<\270<\273<\276<\252<\255<\260<\263<\266<\271<\274<\277<\300<\303<\306<\311<\314<\317<\322<\325<\301<\304<\307<\312<\315<\320<\323<\326<\302<\305<\310<\313<\316<\321<\324<\327<\330<\333<\336<\341<\344<\347<\352<", 0x200)   = 512 0 
lseek(0x3, 0x78BE00, 0x0)  = 7912960 0 
dtrace: error on enabled probe ID 1789 (ID 118: syscall::write:return): out of scratch space in action #12 at DIF offset 44 
writev_nocancel(0x2, 0x7FFF635570E8, 0x4)  = 31 0 
write_nocancel(0x2, "Uh oh. inode_zero_contents returns -1.\0", 0x26)  = 38 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7ACC00, 0x0)  = 8047616 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7ACC00, 0x0)  = 8047616 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AC600, 0x0)  = 8046080 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AC600, 0x0)  = 8046080 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AC600, 0x0)  = 8046080 0 
dtrace: error on enabled probe ID 1789 (ID 118: syscall::write:return): out of scratch space in action #12 at DIF offset 44 
writev_nocancel(0x2, 0x7FFF63556E48, 0x4)  = 31 0 
write_nocancel(0x2, "Uh oh. inode_zero_contents returns -1.\0", 0x26)  = 38 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7ACC00, 0x0)  = 8047616 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7ACC00, 0x0)  = 8047616 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AC600, 0x0)  = 8046080 0 
read(0x3, "\001\0", 0x200)  = 512 0 
lseek(0x3, 0x400, 0x0)  = 1024 0 
read(0x3, "\377\301\004\0", 0x200)  = 512 0 
lseek(0x3, 0x7AC600, 0x0)  = 8046080 0 
read(0x3, "\001\0", 0x200)  = 512 0 
write_nocancel(0x2, "The file was not removed. We can still find it . That's a problem. The file was located at /foo/XXX\n\0", 0x64)   = 100 0 
close(0x3)  = 0 0 
open("/dev/dtracehelper\0", 0x2, 0xFFFFFFFFFFFFFFE8)   = 3 0 
ioctl(0x3, 0x80086804, 0x7FFF63557518)  = 0 0 
close(0x3)  = 0 0 
stat64("/usr/lib/libstdc++.6.dylib\0", 0x7FFF635565C0, 0x7FFF63557440)  = 0 0 
stat64("/usr/lib/libc++abi.dylib\0", 0x7FFF635564D0, 0x7FFF63557350)   = 0 0 
open("/usr/lib/dtrace/libdtrace_dyld.dylib\0", 0x0, 0x0)   = 3 0 
pread(0x3, "\312\376\272\276\0", 0x1000, 0x0)  = 4096 0 
pread(0x3, "\317\372\355\376\a\0", 0x1000, 0x1000)  = 4096 0 
mmap(0x103964000, 0x2000, 0x5, 0x12, 0x3, 0x100001F)   = 0x3964000 0 
mmap(0x103966000, 0x1000, 0x3, 0x12, 0x3, 0x100001F)   = 0x3966000 0 
mmap(0x103967000, 0x1F40, 0x1, 0x12, 0x3, 0x100001F)   = 0x3967000 0 
close(0x3)  = 0 0 
stat64("/usr/lib/libssl.0.9.8.dylib\0", 0x7FFF635565A0, 0x7FFF63557420)  = 0 0 
stat64("/usr/lib/libcrypto.0.9.8.dylib\0", 0x7FFF635565A0, 0x7FFF63557420) = 0 0 
stat64("/usr/lib/libSystem.B.dylib\0", 0x7FFF635565A0, 0x7FFF63557420)  = 0 0 
stat64("/System/Library/PrivateFrameworks/TrustEvaluationAgent.framework/Versions/A/TrustEvaluationAgent\0", 0x7FFF635563A0, 0x7FFF63557220)   = 0 0 
stat64("/usr/lib/libz.1.dylib\0", 0x7FFF635563A0, 0x7FFF63557220)  = 0 0 
stat64("/usr/lib/system/libcache.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libcommonCrypto.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libcompiler_rt.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40 = 0 0 
stat64("/usr/lib/system/libcopyfile.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libdispatch.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libdnsinfo.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libdyld.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libkeymgr.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/liblaunch.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libmacho.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libmathCommon.A.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libquarantine.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libremovefile.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libsystem_blocks.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libsystem_c.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libsystem_dnssd.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libsystem_info.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40 = 0 0 
stat64("/usr/lib/system/libsystem_kernel.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libsystem_network.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libsystem_notify.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libsystem_sandbox.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libunc.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
stat64("/usr/lib/system/libunwind.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40) = 0 0 
stat64("/usr/lib/system/libxpc.dylib\0", 0x7FFF63555FC0, 0x7FFF63556E40)  = 0 0 
getpid(0x7FFF63557498, 0x103960004, 0xEA60)  = 58942 0 
+0

r您確定fd!= NULL ??? – Jeyaram

+0

你確定'fd'是'3'嗎? –

+0

是的,我在gdb中打印fd –

回答

7

既然你問我應該怎麼做調試?

if(write(fd, zeroed_block,512) < 0){ 
    perror("Reason : "); 
} 

,以獲得更好的錯誤消息

+0

(gdb)原因::錯誤的文件描述符(gdb)p fd $ 1 = 3 –

+0

我將fd更改爲3,並且得到了相同的原因。 –

+0

char * diskpath = argv [optind]; –

1

我應該怎麼做調試?

您可以使用strace來獲取由系統調用引起的錯誤消息。由於錯誤是「錯誤的文件描述符」,因此您可以在創建strace日誌時查看它,以及在嘗試寫入之前發生了什麼。

+0

我實際上在Mac OS上,所以我使用了'dtruss',並在我的問題中發佈了輸出。 –

+1

@Rose Perrone:你可能打開'basicDiskImage'只讀。不幸的是,'dtruss'不能以可讀的形式顯示'open'標誌的值。 – ks1322