【问题标题】:Mapping IOKit IOReturn error code to String将 IOKit IOReturn 错误代码映射到 String
【发布时间】:2010-10-08 02:32:40
【问题描述】:

当我收到错误 0x10 时,我希望能够理解该错误代码。查找 IOReturn.h 和 mach/error.h 并不是特别方便。当我得到 0x22 错误代码时,我迷路了。这真是个愚蠢的问题,但是有没有像 error2String 这样的函数可以将 IOReturn 错误代码映射到描述错误的字符串?

【问题讨论】:

  • 那么,错误代码 0x10 是什么? (我现在正在经历。)谢谢!

标签: objective-c iokit


【解决方案1】:

您可以使用 mach_error_string 标准基础函数来执行此操作。

例如。在斯威夫特:

func krToString (_ kr: kern_return_t) -> String {
    if let cStr = mach_error_string(kr) {
        return String (cString: cStr)
    } else {
        return "Unknown kernel error \(kr)"
    }
}

【讨论】:

  • 我不知道为什么这没有得到更高的投票。比庞大的查找表干净得多
  • 我同意@genghiskhan,这是最好的解决方案!该函数在mach/mach_error.h中声明。
【解决方案2】:

在内核中运行的代码可以使用IOService::stringFromReturn(...)

【讨论】:

  • 文档明确表明这是要走的路……但我完全不了解如何从我的代码中调用它。我得到的只是“找不到符号”错误。我需要链接什么?导入/包含?非常感谢。
  • 您的代码是内核扩展,还是用户模式应用程序/库? IOService::stringFromReturn(...) (不幸的是)仅可用于在内核中运行的代码。
【解决方案3】:

我将它从 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)]];
}

【讨论】:

  • 基于this page,你应该先使用err_get_code()宏来提取错误代码。
  • Colin Wilson 的回答要好得多!使用来自mach/mach_error.hmach_error_string()
猜你喜欢
  • 2019-05-23
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 2012-08-16
  • 2011-01-23
相关资源
最近更新 更多