关键词不能为空

当前您在: 主页 > 英语 >

android中的Signal

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-02-11 01:20
tags:

-

2021年2月11日发(作者:finishes)



Android


中的


Signal


信号





Contents


Signal


信号


......................... .................................................. .................................................. ....................... 1


[1] signal


中文描述列表



.


.......................................... .................................................. .............................. 2


[2] Android


信号处理


................ .................................................. .................................................. ........... 5


[3] What is si_codes .... .................................................. .................................................. .......................... 8


Example: SEGV_ACCERR ...................................... .................................................. ............................. 12


Example: BUS_ADRALN


.


.............................................. .................................................. ...................... 12


Example: SIGPIPE .......................................... .................................................. .................................... 14


[4] signal and Traces/tombstone ....... .................................................. .................................................. . 14


[5] Example: Add some logs to debug signal: .................................... .................................................. ... 16


1 Who kill system_server ....... .................................................. .................................................. ......... 16


2 see kernel why unkown reason exit


.


............... .................................................. ............................... 16


[6] Reference


.


................. .................................................. .................................................. ..................... 16






What is signal


How many kind signal,General signal


How to send signal


How to handle signal


How to generally use signal


Signal and system call


signal handle Reentrant Functions


signal set


signal queue


sigprocmask Function





Signals are a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-


compliant operating systems. A signal is an asynchronous notification sent to a process or to a specific


thread within the same process in order to notify it of an event that occurred. Signals originated in


1970s Bell Labs Unix and have been more recently specified in the POSIX standard.


When a signal is sent, the operating system interrupts the target process' normal flow of execution to


deliver the signal. Execution can be interrupted during any non-atomic instruction. If the process has


previously registered a signal handler, that routine is executed. Otherwise, the default signal handler is


executed.


Embedded programs may find signals useful for interprocess communications, as the computational and


memory footprint for signals is small.




[1] signal


中文描述列表




Signal Description


SIGABRT


由调用


abort< /p>


函数产生,进程非正常退出



SIGALRM



alarm


函数设置的


timer


超时或


setitimer


函数设置的


interval timer


超时



SIGBUS


某种特定的硬件异常,通常由内存访问引起



SIGCANCEL



Solaris Thread Library


内部使用,通常不会使用



SIGCHLD


进程


Termina te



Stop


的时候,


SIGCHLD


会发送给它的父进程。缺省情况下该


Signal


会被


忽略



SIGCONT


当被


stop


的进程恢复运行的时候,自动发送



SIGEMT


和实现相关的硬件异常



SIGFPE


数学相关的异常,如被


0


除,浮点溢出,等等



SIGFREEZE Solaris


专用,

< br>Hiberate


或者


Suspended


时候发送



SIGHUP


发送给具有


Terminal



Con trolling Process


,当


terminal



disconnect


时候发送



SIGILL


非法指令异常



SIGINFO BSD signal


。由


Status Key


产生,通常是


CTRL+T


。发送给所有

< p>
Foreground Group


的进程



SIGINT



Interrupt Key


产生,通常是


CTRL+C


或者


DELETE


。发送给所有


ForeG round Group


的进程



SIGIO


异步


IO


事件



SIGIOT


实现相关的硬件异常,一般对应


SIGABRT


SIGKILL


无法处理和忽略。中止某个进程



SIGLWP



Solaris Thread Libray


内部使用



SIGPIPE



reader


中止之后写


Pipe


的时候发送

< p>


SIGPOLL


当某个事件发送给


Pollable Device


的时候发送



SIGPROF Setitimer


指定的


Profiling Interval Timer


所产生



SIGPWR


和系统相关。和


UPS


相关。



SIGQUIT


输入


Quit Key


的时候(


CTRL+


)发送给所有


Foregroun d Group


的进程



SIGSEGV


非法内存访问



SIGSTKFLT Linux


专用,数学协处理器的栈异常



SIGSTOP


中止进程。无法处理和忽略。



SIGSYS


非法系统调用



SIGTERM


请求中止进程,


ki ll


命令缺省发送



SIGTHAW Solaris


专用,从


Suspend


恢复时候发送



SIGTRAP


实现相关的硬件异常。一般是调试异常



SIGTSTP Suspend Key


,一般是

< p>
Ctrl+Z


。发送给所有


Foreground Group


的进程



SIGTTIN



Background Group


的进程尝试读取


Terminal


的时候发送

< br>


SIGTTOU



Background Group


的进程尝试写


Terminal


的时候发送


SIGURG



out- of-band data


接收的时候可能发送



SIGUSR1


用户自定义


signal 1


SIGUSR2


用户自定义


signal 2


SIGVTALRM setitimer


函数设置的


Virtual Interval Timer


超时的时候



SIGWAITING Solaris Thread Library


内部实现专用



SIGWINCH



Termina l


的窗口大小改变的时候,发送给


Foreground Group


的所有进程



SIGXCPU



CPU

< p>
时间限制超时的时候



SIGXFSZ


进程超过文件大小限制



SIGXRES Solaris


专用,进程超过资源限制的时候发送




C:Usersshijunz>adb shell kill -l


1 HUP Hangup 33 33 Signal 33


2 INT Interrupt 34 34 Signal 34


3 QUIT Quit 35 35 Signal 35


4 ILL Illegal instruction 36 36 Signal 36


5 TRAP Trap 37 37 Signal 37


6 ABRT Aborted 38 38 Signal 38


7 BUS Bus error 39 39 Signal 39


8 FPE Floating point exception 40 40 Signal 40


9 KILL Killed 41 41 Signal 41


10 USR1 User signal 1 42 42 Signal 42


11 SEGV Segmentation fault 43 43 Signal 43


12 USR2 User signal 2 44 44 Signal 44


13 PIPE Broken pipe 45 45 Signal 45


14 ALRM Alarm clock 46 46 Signal 46


15 TERM Terminated 47 47 Signal 47


16 STKFLT Stack fault 48 48 Signal 48


17 CHLD Child exited 49 49 Signal 49


18 CONT Continue 50 50 Signal 50


19 STOP Stopped (signal) 51 51 Signal 51


20 TSTP Stopped 52 52 Signal 52


21 TTIN Stopped (tty input) 53 53 Signal 53


22 TTOU Stopped (tty output) 54 54 Signal 54


23 URG Urgent I/O condition 55 55 Signal 55


24 XCPU CPU time limit exceeded 56 56 Signal 56


25 XFSZ File size limit exceeded 57 57 Signal 57


26 VTALRM Virtual timer expired 58 58 Signal 58


27 PROF Profiling timer expired 59 59 Signal 59


28 WINCH Window size changed 60 60 Signal 60


29 IO I/O possible 61 61 Signal 61


30 PWR Power failure 62 62 Signal 62


31 SYS Bad system call 63 63 Signal 63


32 32 Signal 32 64 64 Signal 64



[2] Android


信号处理




我们知道,信号处理的方式一般有三种:



1.


忽略接收到信号后不做任何反应。



2.


自定义用自定义的信号处理函数来执行特定的动作



3.


默认接收到信号后按默认得行为处理该信号。这是多数应 用采取的处理方式。




Signal handlers can be installed with the


signal()


system call. If a signal handler is not installed for a


particular signal, the default handler is used. Otherwise the signal is intercepted and the signal handler is


invoked. The process can also specify two default behaviors, without creating a handler: ignore the


signal (SIG_IGN) and use the default signal handler (SIG_DFL). There are two signals which cannot be


intercepted and handled:


SIGKILL


and


SIGSTOP


.



Android


系统信号处理的行为




我们知道,信号处理的行为是以进程级的。就是说不同的进程 可以分别设置不同的信号处理方式


而互不干扰。同一进程中的不同线程虽然可以设置不同 的信号屏蔽字,但是却共享相同的信号处


理方式(也就是说在一个线程里改变信号处理方 式,将作用于该进程中的所有线程)。



Android


也是


Linux


系 统。所以其信号处理方式不会有本质的改变。但是为了开发和调试的需要,


androi d


对一些信号的处理定义了额外的行为。下面是这些典型的信号在


Android


系统上的行为:



1. SIGQUIT


(整型值为


3




传统


U NIX


系统应用,对


SIGQUIT


信 号的默认行为是



终止


+ CORE


。也就是产生


core dump


文件


后,立即终于运行。



Android Dalvik


应用收到该信号后,会打印改应 用中所有线程的当前状态,并且并不是强制退出。


这些状态通常保存在一个特定的叫做< /p>


trace


的文件中。一般的路径是


/d ata/anr/.



2.


对于很多其他的异常信号(


SIGILL, SIGABRT, SIGBUS, SIGFPE, SIGSEGV, SIGSTKFLT ), Android


进程在


退出前,会生成


tombstone


文件。记录该进程退出前的轨迹。




Android


信号的产生和测试




我们看到,多数


signal


的产生是由于某种内部错误。我们在在开发过程中,当然也可以通过系统


调用故意生成


signal


给某进程。主要的方法如果:



1.



ker nel


里使用


kill_proc_info(




2.



native


应用中使用


kill()


或者


raise()


3. java


应用中使用


gnal()


< p>



但是在测试中,最简单的方法某过于通过


adb


工具了。一个典型场景是:



adb root


adb shell ps


adb shell kill -3 513


首先是切 换到


root


用户(普通进程只能发个自己或者同组进程,而< /p>


root


可以发送


signal


给任何进


程)。然后用


ps

命令查看当前系统中所有的进程信息。最后用


kill


命令 发送


SIGQUIT


给进程号为


513


的进程。




android kill


程序的实现很简单,他只能支持发送


signal


的值(如上例中的“


3< /p>


”)给进程,而不能


用名字(如“


SIG QUIT


”)。


android



kill


程序的代码在


system/co re/toolbox/kill.c


中。虽然移植


linux



kill


的实现就能支持名字,但是 那个完全没有必要,


android


需要的

signal


就这么几个,他


们的值应该记住的。




对于终端发送


S IGQUIT


,大多数用户可以直接得到预期的结果(生成相应的


trace


文件)。最困惑


的行为来自于终端发送

< p>
SIGILL, SIGABRT, SIGBUS, SIGFPE, SIGSEGV, SI GSTKFLT


等信号,我们常常看到


“不确定”的行为:有时 候能够看到


process


终止,有时候却不能。


core dump


也不是总能产生。


例如如下测试场景:



adb root


adb shell ps


adb shell kill -11 299



log


里看到进程


299


并没有终止


, ps


看到进程还在。< /p>


Log


只有如下输出:



F/libc( 244): Fatal signal 11 (SIGSEGV) at 0x000001c0 (code=0)


I/DEBUG ( 31): timed out waiting for pid=244 tid=244 uid=10009 to die



再试一次:



adb shell ps


adb shell kill -11 299



这次进程


299


被终止掉了,却没有产 生


core dump


文件:



D/Zygote ( 34): Process 244 terminated by signal (11)


I/ActivityManager( 78): Process ar (pid 244) has died.



其实,我们观察到得不确定行为是因为没有了解


android


这些信号的处理。他的行为的确是确定


的,要 点是:



要产生


core dump< /p>


并终止某进程,我们需要连续发送两次改信号,并且中间间隔在


0 .2


秒到


3



之间。



如果间隔过小,


< p>
Android


可能无法接收第一个


signal


。如果时间过久,


android


将简 单的终止进程,


而没有


core dump


产生。我们会在后面详细的介绍产生这种行为的原因。




好了,了解到上面的行为后,我们再次试验,连续两次发送


SIGSEGV


给进程,看看行为:



adb shell ps


adb shell kill -11 307


adb shell kill -11 307


我们看到,该进程被终止,


tombstone


被打到


log



,


并且存储在


/data/tombstone_00


中:



也可能在其他的在


/data/tombstone_XX






Signal Catcher


这个线程是用来捕获


linux


信号和 做一些后续处理的。比如说,当一个


SIGQUIT


(Ctr l-)


信号到达后,


这个线程就会挂起虚拟机,并且将所有线程 的状态信息输出到


log


。其源代码在


dalvik/vm/SignalCatcher.*


部分。





[3] What is si_codes


Documentation of a sort (indexed Linux source code) for SEGV_MAPERR is here:


/source/include/uapi/asm- generic/siginfo.h#L167



166 /*


167 *si_code values


168 * Digital reserves positive values for kernel- generated signals.


169 */


170 #define SI_USER 0 /* sent by kill, sigsend, raise */


171 #define SI_KERNEL 0x80 /* sent by the kernel from somewhere */


172 #define SI_QUEUE -1 /* sent by sigqueue */

-


-


-


-


-


-


-


-



本文更新与2021-02-11 01:20,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/633062.html

android中的Signal的相关文章