Android strerror

http://androidxref.com/8.0.0_r4/xref/bionic/libc/bionic/strerror.cpp#35

根据 error code number 查找 error string.

http://androidxref.com/8.0.0_r4/xref/bionic/libc/bionic/strerror_r.cpp#_sys_error_strings

http://androidxref.com/8.0.0_r4/xref/bionic/libc/private/bionic_errdefs.h

#ifndef __BIONIC_ERRDEF
#error "__BIONIC_ERRDEF must be defined before including this file"
#endif
__BIONIC_ERRDEF( 0              ,   0, "Success" )
__BIONIC_ERRDEF( EPERM          ,   1, "Operation not permitted" )
__BIONIC_ERRDEF( ENOENT         ,   2, "No such file or directory" )
__BIONIC_ERRDEF( ESRCH          ,   3, "No such process" )
__BIONIC_ERRDEF( EINTR          ,   4, "Interrupted system call" )
__BIONIC_ERRDEF( EIO            ,   5, "I/O error" )
__BIONIC_ERRDEF( ENXIO          ,   6, "No such device or address" )
__BIONIC_ERRDEF( E2BIG          ,   7, "Argument list too long" )
__BIONIC_ERRDEF( ENOEXEC        ,   8, "Exec format error" )
__BIONIC_ERRDEF( EBADF          ,   9, "Bad file descriptor" )
__BIONIC_ERRDEF( ECHILD         ,  10, "No child processes" )
__BIONIC_ERRDEF( EAGAIN         ,  11, "Try again" )
__BIONIC_ERRDEF( ENOMEM         ,  12, "Out of memory" )
__BIONIC_ERRDEF( EACCES         ,  13, "Permission denied" )
__BIONIC_ERRDEF( EFAULT         ,  14, "Bad address" )
__BIONIC_ERRDEF( ENOTBLK        ,  15, "Block device required" )
__BIONIC_ERRDEF( EBUSY          ,  16, "Device or resource busy" )
__BIONIC_ERRDEF( EEXIST         ,  17, "File exists" )
__BIONIC_ERRDEF( EXDEV          ,  18, "Cross-device link" )
__BIONIC_ERRDEF( ENODEV         ,  19, "No such device" )
__BIONIC_ERRDEF( ENOTDIR        ,  20, "Not a directory" )
__BIONIC_ERRDEF( EISDIR         ,  21, "Is a directory" )
__BIONIC_ERRDEF( EINVAL         ,  22, "Invalid argument" )
__BIONIC_ERRDEF( ENFILE         ,  23, "File table overflow" )
__BIONIC_ERRDEF( EMFILE         ,  24, "Too many open files" )
__BIONIC_ERRDEF( ENOTTY         ,  25, "Not a typewriter" )
__BIONIC_ERRDEF( ETXTBSY        ,  26, "Text file busy" )
__BIONIC_ERRDEF( EFBIG          ,  27, "File too large" )
__BIONIC_ERRDEF( ENOSPC         ,  28, "No space left on device" )
__BIONIC_ERRDEF( ESPIPE         ,  29, "Illegal seek" )
__BIONIC_ERRDEF( EROFS          ,  30, "Read-only file system" )
__BIONIC_ERRDEF( EMLINK         ,  31, "Too many links" )
__BIONIC_ERRDEF( EPIPE          ,  32, "Broken pipe" )
__BIONIC_ERRDEF( EDOM           ,  33, "Math argument out of domain of func" )
__BIONIC_ERRDEF( ERANGE         ,  34, "Math result not representable" )
__BIONIC_ERRDEF( EDEADLK        ,  35, "Resource deadlock would occur" )
__BIONIC_ERRDEF( ENAMETOOLONG   ,  36, "File name too long" )
__BIONIC_ERRDEF( ENOLCK         ,  37, "No record locks available" )
__BIONIC_ERRDEF( ENOSYS         ,  38, "Function not implemented" )
__BIONIC_ERRDEF( ENOTEMPTY      ,  39, "Directory not empty" )
__BIONIC_ERRDEF( ELOOP          ,  40, "Too many symbolic links encountered" )
__BIONIC_ERRDEF( ENOMSG         ,  42, "No message of desired type" )
__BIONIC_ERRDEF( EIDRM          ,  43, "Identifier removed" )
__BIONIC_ERRDEF( ECHRNG         ,  44, "Channel number out of range" )
__BIONIC_ERRDEF( EL2NSYNC       ,  45, "Level 2 not synchronized" )
__BIONIC_ERRDEF( EL3HLT         ,  46, "Level 3 halted" )
__BIONIC_ERRDEF( EL3RST         ,  47, "Level 3 reset" )
__BIONIC_ERRDEF( ELNRNG         ,  48, "Link number out of range" )
__BIONIC_ERRDEF( EUNATCH        ,  49, "Protocol driver not attached" )
__BIONIC_ERRDEF( ENOCSI         ,  50, "No CSI structure available" )
__BIONIC_ERRDEF( EL2HLT         ,  51, "Level 2 halted" )
__BIONIC_ERRDEF( EBADE          ,  52, "Invalid exchange" )
__BIONIC_ERRDEF( EBADR          ,  53, "Invalid request descriptor" )
__BIONIC_ERRDEF( EXFULL         ,  54, "Exchange full" )
__BIONIC_ERRDEF( ENOANO         ,  55, "No anode" )
__BIONIC_ERRDEF( EBADRQC        ,  56, "Invalid request code" )
__BIONIC_ERRDEF( EBADSLT        ,  57, "Invalid slot" )
__BIONIC_ERRDEF( EBFONT         ,  59, "Bad font file format" )
__BIONIC_ERRDEF( ENOSTR         ,  60, "Device not a stream" )
__BIONIC_ERRDEF( ENODATA        ,  61, "No data available" )
__BIONIC_ERRDEF( ETIME          ,  62, "Timer expired" )
__BIONIC_ERRDEF( ENOSR          ,  63, "Out of streams resources" )
__BIONIC_ERRDEF( ENONET         ,  64, "Machine is not on the network" )
__BIONIC_ERRDEF( ENOPKG         ,  65, "Package not installed" )
__BIONIC_ERRDEF( EREMOTE        ,  66, "Object is remote" )
__BIONIC_ERRDEF( ENOLINK        ,  67, "Link has been severed" )
__BIONIC_ERRDEF( EADV           ,  68, "Advertise error" )
__BIONIC_ERRDEF( ESRMNT         ,  69, "Srmount error" )
__BIONIC_ERRDEF( ECOMM          ,  70, "Communication error on send" )
__BIONIC_ERRDEF( EPROTO         ,  71, "Protocol error" )
__BIONIC_ERRDEF( EMULTIHOP      ,  72, "Multihop attempted" )
__BIONIC_ERRDEF( EDOTDOT        ,  73, "RFS specific error" )
__BIONIC_ERRDEF( EBADMSG        ,  74, "Not a data message" )
__BIONIC_ERRDEF( EOVERFLOW      ,  75, "Value too large for defined data type" )
__BIONIC_ERRDEF( ENOTUNIQ       ,  76, "Name not unique on network" )
__BIONIC_ERRDEF( EBADFD         ,  77, "File descriptor in bad state" )
__BIONIC_ERRDEF( EREMCHG        ,  78, "Remote address changed" )
__BIONIC_ERRDEF( ELIBACC        ,  79, "Can not access a needed shared library" )
__BIONIC_ERRDEF( ELIBBAD        ,  80, "Accessing a corrupted shared library" )
__BIONIC_ERRDEF( ELIBSCN        ,  81, ".lib section in a.out corrupted" )
__BIONIC_ERRDEF( ELIBMAX        ,  82, "Attempting to link in too many shared libraries" )
__BIONIC_ERRDEF( ELIBEXEC       ,  83, "Cannot exec a shared library directly" )
__BIONIC_ERRDEF( EILSEQ         ,  84, "Illegal byte sequence" )
__BIONIC_ERRDEF( ERESTART       ,  85, "Interrupted system call should be restarted" )
__BIONIC_ERRDEF( ESTRPIPE       ,  86, "Streams pipe error" )
__BIONIC_ERRDEF( EUSERS         ,  87, "Too many users" )
__BIONIC_ERRDEF( ENOTSOCK       ,  88, "Socket operation on non-socket" )
__BIONIC_ERRDEF( EDESTADDRREQ   ,  89, "Destination address required" )
__BIONIC_ERRDEF( EMSGSIZE       ,  90, "Message too long" )
__BIONIC_ERRDEF( EPROTOTYPE     ,  91, "Protocol wrong type for socket" )
__BIONIC_ERRDEF( ENOPROTOOPT    ,  92, "Protocol not available" )
__BIONIC_ERRDEF( EPROTONOSUPPORT,  93, "Protocol not supported" )
__BIONIC_ERRDEF( ESOCKTNOSUPPORT,  94, "Socket type not supported" )
__BIONIC_ERRDEF( EOPNOTSUPP     ,  95, "Operation not supported on transport endpoint" )
__BIONIC_ERRDEF( EPFNOSUPPORT   ,  96, "Protocol family not supported" )
__BIONIC_ERRDEF( EAFNOSUPPORT   ,  97, "Address family not supported by protocol" )
__BIONIC_ERRDEF( EADDRINUSE     ,  98, "Address already in use" )
__BIONIC_ERRDEF( EADDRNOTAVAIL  ,  99, "Cannot assign requested address" )
__BIONIC_ERRDEF( ENETDOWN       , 100, "Network is down" )
__BIONIC_ERRDEF( ENETUNREACH    , 101, "Network is unreachable" )
__BIONIC_ERRDEF( ENETRESET      , 102, "Network dropped connection because of reset" )
__BIONIC_ERRDEF( ECONNABORTED   , 103, "Software caused connection abort" )
__BIONIC_ERRDEF( ECONNRESET     , 104, "Connection reset by peer" )
__BIONIC_ERRDEF( ENOBUFS        , 105, "No buffer space available" )
__BIONIC_ERRDEF( EISCONN        , 106, "Transport endpoint is already connected" )
__BIONIC_ERRDEF( ENOTCONN       , 107, "Transport endpoint is not connected" )
__BIONIC_ERRDEF( ESHUTDOWN      , 108, "Cannot send after transport endpoint shutdown" )
__BIONIC_ERRDEF( ETOOMANYREFS   , 109, "Too many references: cannot splice" )
__BIONIC_ERRDEF( ETIMEDOUT      , 110, "Connection timed out" )
__BIONIC_ERRDEF( ECONNREFUSED   , 111, "Connection refused" )
__BIONIC_ERRDEF( EHOSTDOWN      , 112, "Host is down" )
__BIONIC_ERRDEF( EHOSTUNREACH   , 113, "No route to host" )
__BIONIC_ERRDEF( EALREADY       , 114, "Operation already in progress" )
__BIONIC_ERRDEF( EINPROGRESS    , 115, "Operation now in progress" )
__BIONIC_ERRDEF( ESTALE         , 116, "Stale NFS file handle" )
__BIONIC_ERRDEF( EUCLEAN        , 117, "Structure needs cleaning" )
__BIONIC_ERRDEF( ENOTNAM        , 118, "Not a XENIX named type file" )
__BIONIC_ERRDEF( ENAVAIL        , 119, "No XENIX semaphores available" )
__BIONIC_ERRDEF( EISNAM         , 120, "Is a named type file" )
__BIONIC_ERRDEF( EREMOTEIO      , 121, "Remote I/O error" )
__BIONIC_ERRDEF( EDQUOT         , 122, "Quota exceeded" )
__BIONIC_ERRDEF( ENOMEDIUM      , 123, "No medium found" )
__BIONIC_ERRDEF( EMEDIUMTYPE    , 124, "Wrong medium type" )
__BIONIC_ERRDEF( ECANCELED      , 125, "Operation Canceled" )
__BIONIC_ERRDEF( ENOKEY         , 126, "Required key not available" )
__BIONIC_ERRDEF( EKEYEXPIRED    , 127, "Key has expired" )
__BIONIC_ERRDEF( EKEYREVOKED    , 128, "Key has been revoked" )
__BIONIC_ERRDEF( EKEYREJECTED   , 129, "Key was rejected by service" )
__BIONIC_ERRDEF( EOWNERDEAD     , 130, "Owner died" )
__BIONIC_ERRDEF( ENOTRECOVERABLE, 131, "State not recoverable" )

#undef __BIONIC_ERRDEF
http://androidxref.com/8.0.0_r4/xref/bionic/libc/bionic/strerror_r.cpp#52

如果 error code number 在定义的字符串对里面找不到,就直接输出 Unknown error %d 加上 error number.

int strerror_r(int error_number, char* buf, size_t buf_len) {
  ErrnoRestorer errno_restorer;
  size_t length;

  const char* error_name = __strerror_lookup(error_number);
  if (error_name != NULL) {
    length = strlcpy(buf, error_name, buf_len);
  } else {
    length = __libc_format_buffer(buf, buf_len, "Unknown error %d", error_number);
  }

一般直接在系统调用之后用 strerror(errno) , 这个 errno 又是什么呢?

http://androidxref.com/8.0.0_r4/xref/bionic/libc/include/errno.h#47

/* a macro expanding to the errno l-value */
#define  errno   (*__errno())
volatile int*  __errno() {
  return reinterpret_cast<int*>(&(__get_tls()[TLS_SLOT_ERRNO]));
}
# define __get_tls() ({ void** __val; __asm__("mrc p15, 0, %0, c13, c0, 3" : "=r"(__val)); __val; })

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0360f/CHDGIJFB.html

mrc p15, 0, %0, c13, c0, 3

这是 ARM 协处理器指令,在ARM 文档中可以看到是用于 Read Thread ID Privileged Read Write only Register


对应的写命令就是 

mcr p15, 0, %0, c13, c0, 3

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0360f/CACEAIHG.html


在代码中找到写寄存器的地方:

http://androidxref.com/kernel_3.18/xref/arch/arm/include/asm/tls.h#81

static inline void set_tls(unsigned long val)
{
	struct thread_info *thread;

	thread = current_thread_info();

	thread->tp_value[0] = val;

	/*
	 * This code runs with preemption enabled and therefore must
	 * be reentrant with respect to switch_tls.
	 *
	 * We need to ensure ordering between the shadow state and the
	 * hardware state, so that we don't corrupt the hardware state
	 * with a stale shadow state during context switch.
	 *
	 * If we're preempted here, switch_tls will load TPIDRURO from
	 * thread_info upon resuming execution and the following mcr
	 * is merely redundant.
	 */
	barrier();

	if (!tls_emu) {
		if (has_tls_reg) {
			asm("mcr p15, 0, %0, c13, c0, 3"
			    : : "r" (val));

http://androidxref.com/kernel_3.18/xref/arch/arm/kernel/traps.c#626

asmlinkage int arm_syscall(int no, struct pt_regs *regs)
{

	case NR(set_tls):
		set_tls(regs->ARM_r0);
		return 0;


可以看到其实 是将 ARM R0 寄存器(ARM架构下R0 存储的是函数调用的返回值),

因此,系统调用的返回值就是在 errno 中能获取到。


Android 的系统调用都在:

http://androidxref.com/8.0.0_r4/xref/bionic/libc/arch-arm/syscalls/

http://androidxref.com/8.0.0_r4/xref/bionic/libc/arch-arm/syscalls/write.S

/* Generated by gensyscalls.py. Do not edit. */
#include <private/bionic_asm.h>
ENTRY(write)
    mov     ip, r7
    .cfi_register r7, ip
    ldr     r7, =__NR_write
    swi     #0
    mov     r7, ip
    .cfi_restore r7
    cmn     r0, #(MAX_ERRNO + 1)
    bxls    lr
    neg     r0, r0
    b       __set_errno_internal
END(write)
http://androidxref.com/8.0.0_r4/xref/bionic/libc/arch-arm/syscalls/__set_tls.S
ENTRY(__set_tls)
    mov     ip, r7
    .cfi_register r7, ip
    ldr     r7, =__ARM_NR_set_tls
    swi     #0
    mov     r7, ip
    .cfi_restore r7
    cmn     r0, #(MAX_ERRNO + 1)
    bxls    lr
    neg     r0, r0
    b       __set_errno_internal
END(__set_tls)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值