📄 porting
字号:
I am frequently asked to port strace to a given platform. Lessfrequently I am asked how one would go about porting strace to a givenplatform. :-) Since I have ported strace to a few platforms already Ihave some advice to the would-be strace porter.The number one question is ``Does the native operating support aconcept which enables even the mere possibility of tracing?''. So farI have seen two mechanisms which support system call tracing. Theyare the SunOS originated feature of the PTRACE_SYSCALL argument to theptrace system call and the PIOCSENTRY/PIOCSEXIT ioctl for the /procfilesystem under System V release 4 style Unix derived systems. Theremay be others (surely a better one could be devised :-) but innovationis a rare commodity so unless one of these is supported you may beSOL.Therefore the first step is to try `man 2 ptrace' and `man 4 proc' tosee if there is even a glimmer of hope. Without assistance from theoperating system, system call tracing is a lost cause. If there is anative system call tracing program (however pathetic it might be :-),you may be able to use it to trace itself to determine what mechanismit is using for tracing the system calls of another process. If theinterface is documented you should be a happy camper. Otherwise,unless you can tolerate the thought of many thankless hourssingle-stepping in a debugger with little or nothing to show for it,you should consider other tasks to distract you from your friends,family, education, job, spouse and/or significant other.If you have arrived here, your OS should have ptrace or proc or youshould have a high tolerance for pain. Then again, curious butdetached readers are invited to continue with little to risk butboredom. If the mechanism is neither ptrace nor proc then examine howit is intended to work and see how well it fits with what stracealready does. If it fits, fine, add a few more ifdefs. If there is agross mismatch, write a whole new event loop.At this point you are responsible for determining three things: how isthe specific system call communicated, how are system call argumentshandled, and how is errno handled. These things can usually beresolved in a day or two using a decent assembly level debugger andsome educated guesswork. For example, set a breakpoint on `read'.Then disassemble the code and see where the arguments go. Do they goon the stack? Do they go into registers? Some combination of thetwo? Find the point where the transition from user mode to kernelmode is made. Can you identify the arguments at this point? When thecall returns where does errno go? Into a specific register? Into aglobal variable?Next you need to determine how to decode numeric system call numbersinto system call names (syscallent.h), errno values into errno names(errnoent.h) and ioctl values into ioctl names (ioctlent.h). Oftenthis fragile step can be accomplished by massaging system header fileswith ad hoc shell scripts. Expect your scripts to break with everydot rev of each OS release.Finally, once you have the basic framework in which system calls andtheir arguments can be decoded, you must do the dirty work of decodingevery useful call. Some may be similar or identical to like-namedcalls in other operating systems. Go ahead and tweak what is thereto achieve what you want. If there is more difference than similarity,then just write your own version of it using one of the existingimplementations for ideas.The first order of decoding is the generation of suitable system call,errno, ioctl and signal tables. Sample scripts are included to assistwith the generation of a first pass at these tables.Good luck and feel free to contact me before and/or during any majorproject.Rick Sladkey <jrs@world.std.com>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -