`
61party
  • 浏览: 1042179 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Notes of Introduction to OS Abstractions Using Plan 9 from Bell Labs(I)

 
阅读更多

1. Loaded Programs

1.1 Commandnm

Commandnmcan be used to display symbol information in both objects and binary files, because it looks at the symbol table stored in the binary for debugging purposes. Commandstripcan be used to remove the symbol table.

Option–nasksnmto sort the output by symbol address. The addresses are virtual memory addresses, because the system uses the virtual memory hardware to keep each process in its own virtual address space.

In the output of nm,etextis a symbol defined by the linker to let you know where the end of test is, andedatareports the address where the initialized data terminates.

1.2 What does the system (kernel) loader do?

·The header in the binary file reports the memory size required for the program text, and the file keeps the memory image of that text. Therefore, the system can just copy all this into memory. For a given system and architecture, there is a convention regarding which addresses the program must use. Therefore, the system knows where to load the program.

·The header in the binary reports the memory size required for initialized variables (global) and file contains a memory image for them. Thus, the system can copy those bytes to memory. Note that the system has no idea regarding where does one variable start or how big it is. The system only knows how many bytes it has to copy to memory, and at which address should they be copied.

·For uninitialized global variables, the binary header reports their total size. The system allocates that amount of memory for the program. That is all it has to do. As a courtesy, Plan 9 guarantees that such memory is initialized with all bytes being zero. This means that all your global variables are initialized to null values by default.

1.3 Memory image for the global program

The virtual memory of a process in Plan 9 has several segments. A memory segment is a portion of contiguous memory with some properties. Segments used by a Plan 9 process are:

·Thetest segment. It contains instructions that can be executed but not modified. The hardware is used by the system to enforce these permissions. The memory is initialized by the system with the program text (code) kept within the binary file for the program.

·Thedata segment. It contains the initialized data for the program. Protection is set to allow both read and write operations on it, but you cannot execute instructions on it. The memory is initialized by the system using the initialized data kept within the binary file for the program.

·The uninitialized data segment, calledbss segment, which is almost like the data segment. However, this one is initialized by zeroing its memory. The name of the segment comes from an arcane instruction used to implement it on a machine that no longer exists. How much memory is given depends on the size recorded in the binary file. Moreover, this segment cangrow, by using a system call that allocates more memory for it. Function libraries likemalloccause this segment to grow when they consume all the available memory in this segment. This is the reason for the gap between this segment and the stack segment, to leave room for the segment to grow.

·Thestack segmentis also used for reading and writing memory. Unlike other segments, this segment seems to grow automatically when more space is used. It is used to keep the stack for the process.

1.4 Process Arguments

The macrosARGBEGINandARGENDloop through the argument list, removing and processing

options. After ARGEND, both argc and argv reflect the argument list without any option.

Between both macros, we must write the body for a switch statement (supplied by

ARGBEGIN), with a case per option.

Macros defined in plan9.h

Most of the Plan 9 programs that accept multiple options use these macros to process their

argument list in search for options. This means that the invocation syntax is similar for most pro-grams. You may combine options in a single argument, use multiple arguments, supply arguments for options immediately after the option letter, or use another argument,

terminate the option list by giving a -- argument, and so on.

Source code of bind.c

1.5 System call errors

There are several ways of printing out the error string. The most convenient way is using the format “%r” in print.



There is a function that both prints a message and exits. It is calledsysfatal, and is used like follows.



The system callrerrstrreads the error string. It stores the string at the buffer you supply.



The system callwerrstrwrites a new value for the error string. It is used like theprint. Using it, we can implement a function thatpopsan element from a stack and reports errors nicely:



1.6 Environment

To obtain the value for a environment variable, from a C program, we can use thegetenvsystem call. If the variable is not defined,getenvreturns a null string. A related call isputenv, which accepts a name and a value, and set the corresponding environment variable accordingly.

1.7 Process States



1.8 Debugging

The programsrcknows how to obtain the source file name and line number that corresponds to that program counter.

; src -n -s 0x000016ff 8.hi

/sys/src/libc/fmt/dofmt.c:37

Option -n causes the source file name and line to be printed. Otherwise src would ask your editor to display your file and line. Option -s permits you to give a memory address or a symbol name to locate its source.

acidis the debugger can be used to dump the stack (function stk()), memory (function mem()) and so on.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics