Objective c 将IOKit IOReturn错误代码映射到字符串
当我得到错误0x10时,我希望能够理解该错误代码。查找IOReturn.h和mach/error.h不是特别方便。当我得到0x22错误代码时,我迷路了。这真是一个愚蠢的问题,但是有没有像error2String这样的函数可以将IOReturn错误代码映射到描述错误的字符串?内核中运行的代码可以使用Objective c 将IOKit IOReturn错误代码映射到字符串,objective-c,iokit,Objective C,Iokit,当我得到错误0x10时,我希望能够理解该错误代码。查找IOReturn.h和mach/error.h不是特别方便。当我得到0x22错误代码时,我迷路了。这真是一个愚蠢的问题,但是有没有像error2String这样的函数可以将IOReturn错误代码映射到描述错误的字符串?内核中运行的代码可以使用IOService::stringFromReturn(…)我从IOService.h移植了它,这应该可以完成这项工作 -(NSString*)stringFromError:(unsigned int
IOService::stringFromReturn(…)
我从IOService.h移植了它,这应该可以完成这项工作
-(NSString*)stringFromError:(unsigned int)errorVal
{
NSDictionary *ioReturnMap =
@{@kIOReturnSuccess: @"success",
@kIOReturnError: @"general error",
@kIOReturnNoMemory: @"memory allocation error",
@kIOReturnNoResources: @"resource shortage",
@kIOReturnIPCError: @"Mach IPC failure",
@kIOReturnNoDevice: @"no such device",
@kIOReturnNotPrivileged: @"privilege violation",
@kIOReturnBadArgument: @"invalid argument",
@kIOReturnLockedRead: @"device is read locked",
@kIOReturnLockedWrite: @"device is write locked",
@kIOReturnExclusiveAccess: @"device is exclusive access",
@kIOReturnBadMessageID: @"bad IPC message ID",
@kIOReturnUnsupported: @"unsupported function",
@kIOReturnVMError: @"virtual memory error",
@kIOReturnInternalError: @"internal driver error",
@kIOReturnIOError: @"I/O error",
@kIOReturnCannotLock: @"cannot acquire lock",
@kIOReturnNotOpen: @"device is not open",
@kIOReturnNotReadable: @"device is not readable",
@kIOReturnNotWritable: @"device is not writeable",
@kIOReturnNotAligned: @"alignment error",
@kIOReturnBadMedia: @"media error",
@kIOReturnStillOpen: @"device is still open",
@kIOReturnRLDError: @"rld failure",
@kIOReturnDMAError: @"DMA failure",
@kIOReturnBusy: @"device is busy",
@kIOReturnTimeout: @"I/O timeout",
@kIOReturnOffline: @"device is offline",
@kIOReturnNotReady: @"device is not ready",
@kIOReturnNotAttached: @"device/channel is not attached",
@kIOReturnNoChannels: @"no DMA channels available",
@kIOReturnNoSpace: @"no space for data",
@kIOReturnPortExists: @"device port already exists",
@kIOReturnCannotWire: @"cannot wire physical memory",
@kIOReturnNoInterrupt: @"no interrupt attached",
@kIOReturnNoFrames: @"no DMA frames enqueued",
@kIOReturnMessageTooLarge: @"message is too large",
@kIOReturnNotPermitted: @"operation is not permitted",
@kIOReturnNoPower: @"device is without power",
@kIOReturnNoMedia: @"media is not present",
@kIOReturnUnformattedMedia: @"media is not formatted",
@kIOReturnUnsupportedMode: @"unsupported mode",
@kIOReturnUnderrun: @"data underrun",
@kIOReturnOverrun: @"data overrun",
@kIOReturnDeviceError: @"device error",
@kIOReturnNoCompletion: @"no completion routine",
@kIOReturnAborted: @"operation was aborted",
@kIOReturnNoBandwidth: @"bus bandwidth would be exceeded",
@kIOReturnNotResponding: @"device is not responding",
@kIOReturnInvalid: @"unanticipated driver error",
};
return [ioReturnMap objectForKey:[NSNumber numberWithInt:err_get_code(errorVal)]];
}
以下是@chuckus对Swift的回答:
func convertIOReturnToString(uu状态:IOReturn)->字符串?{
设映射=[
成功:“成功”,
kIOReturnError:“一般错误”,
KioreTurnomery:“内存分配错误”,
kIOReturnNoResources:“资源短缺”,
Kiore错误:“马赫IPC故障”,
KioreTurnNodeDevice:“没有这样的设备”,
kIOReturnNotPrivileged:“特权违反”,
kIOReturnBadArgument:“无效参数”,
kIOReturnLockedRead:“设备已读锁定”,
kIOReturnLockedWrite:“设备已写入锁定”,
kIOReturnExclusiveAccess:“设备是独占访问”,
kIOReturnBadMessageID:“错误的IPC消息ID”,
kIOReturnUnsupported:“不支持的函数”,
kIOReturnVMError:“虚拟内存错误”,
kIOReturnInternalError:“内部驱动程序错误”,
kIOReturnIOError:“I/O错误”,
kIOReturnCannotLock:“无法获取锁”,
KioreTurnoOpen:“设备未打开”,
kIOReturnNotReadable:“设备不可读”,
kIOReturnNotWritable:“设备不可写”,
kIOReturnNotAligned:“对齐错误”,
kIOReturnBadMedia:“媒体错误”,
kIOReturnStillOpen:“设备仍处于打开状态”,
kIOReturnRLDError:“rld故障”,
kIOReturnDMAError:“DMA故障”,
kIOReturnBusy:“设备正忙”,
kIOReturnTimeout:“I/O超时”,
kIOReturnOffline:“设备处于脱机状态”,
kIOReturnNotReady:“设备未就绪”,
kIOReturnNotAttached:“未连接设备/通道”,
KioreTurnoChannel:“没有可用的DMA信道”,
kIOReturnNoSpace:“没有数据空间”,
kIOReturnPortExists:“设备端口已存在”,
kIOReturnCannotWire:“无法连接物理内存”,
kIOReturnNoInterrupt:“未附加中断”,
KioreTurnoFrames:“没有DMA帧排队”,
kIOReturnMessageTooLarge:“消息太大”,
KioreTurnNotAllowed:“不允许操作”,
kIOReturnNoPower:“设备没有电源”,
KioreTurnomedia:“媒体不存在”,
kIOReturnUnformattedMedia:“媒体未格式化”,
kIOReturnUnsupportedMode:“不支持的模式”,
kIOReturnUnderrun:“数据underrun”,
KioreTurnOverflow:“数据溢出”,
kIOReturnDeviceError:“设备错误”,
KioreTurnoCompletion:“无完成程序”,
kIOReturnAborted:“操作已中止”,
kIOReturnNoBandwidth:“将超过总线带宽”,
kIOReturnNotResponding:“设备没有响应”,
kIOReturnInvalid:“意外的驱动程序错误”
]
返回地图[状态]
}
< /代码> 您可以使用MajyError字符串标准基础函数来执行此操作。
例如,在Swift中:
func krToString (_ kr: kern_return_t) -> String {
if let cStr = mach_error_string(kr) {
return String (cString: cStr)
} else {
return "Unknown kernel error \(kr)"
}
}
文档明确指出这是一条路……但我完全不知道如何从代码中调用它。我得到的只是“未找到符号”错误。我需要链接什么?进口/包括?非常感谢。您的代码是内核扩展还是用户模式应用程序/库?不幸的是,IOService::stringFromReturn(…)仅对内核中运行的代码可用。那么,错误代码0x10是什么?(我现在正在体验)谢谢!基于,您可能应该首先使用err\u get\u code()
宏提取错误代码。Colin Wilson的答案要好得多!使用mach/mach\u error.h
中的mach\u error\u string()
。我不知道为什么投票率不高。比庞大的查找表干净得多我同意@genghiskhan,这是最好的解决方案!该函数在mach/mach\u error.h
中声明。