1

The third line below returns -1. What should I do to debug?

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

When I print the error caused by write (using perror) The reason is "Bad file descriptor"

Here's the code where I open the file descriptor:

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);
}

The opening the file descriptor works relialbly every other time I use it, outside these three lines of code.

I'm on Mac OS, so here's the output of dtruss, as requested:

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
4

2 回答 2

7

既然你问我应该怎么做才能调试?

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

获得更好的错误信息

于 2012-10-12T06:35:06.120 回答
1

我应该怎么做才能调试?

您可以使用strace获取由系统调用引起的错误消息。由于错误是“错误的文件描述符”,您可以在 strace 日志中查看它的创建时间以及在尝试写入之前发生的情况。

于 2012-10-12T07:06:41.323 回答