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.
分享到:
相关推荐
涵盖了编程和数据结构的基础知识,并为初学计算机科学的学生提供了不仅编写程序的机会,而且还提供了证明定理和分析算法的机会。
An Introduction to Computer Science Using Scheme
Gottschling introduces key concepts using examples from many technical problem domains, drawing on his extensive experience training professionals and teaching C++ to students of physics, math, and ...
This book presents the foundations of theoretical computer science in a format ... and complexity theory with an introduction to some of the open classification problems relating to the classes P and NP.
Chapter 14: Introduction to inheritance 459 14.1 POLYGONS AND RECTANGLES 460 14.2 POLYMORPHISM 467 14.3 TYPING FOR INHERITANCE 472 14.4 DYNAMIC BINDING 480 14.5 DEFERRED FEATURES AND CLASSES 482 14.6 ...
higher-level programming abstractions and the ability to in- ject new agents into a preexisting network. The unique char- acteristics of wireless sensor networks like resource scarcity and emphasis on...
Gottschling introduces key concepts using examples from many technical problem domains, drawing on his extensive experience training professionals and teaching C++ to students of physics, math, and ...
Gottschling introduces key concepts using examples from many technical problem domains, drawing on his extensive experience training professionals and teaching C++ to students of physics, math, and ...
Work on a wide array of applications, from simple batch jobs to stream processing and machine learning Explore the most common as well as some complex use-cases to perform large-scale data analysis ...
The book starts with a short introduction to the principles and the ecosystem of Redux, then moves on to show how to implement the basic elements of Redux and put them together. Afterward, you are ...
The book approaches the topic from the perspective of Zeigler's theory of modeling and simulation, introducing the theory's fundamental concepts and showing how to apply them to engineering problems....
Authors Tom Hope, Yehezkel Resheff, and Itay Lieder provide a hands-on approach to TensorFlow fundamentals for a broad technical audience—from data scientists and engineers to students and ...
Programming Abstractions in C 的源码,实在难找,还是自己不会用股沟?败毒? 呵呵,后来终于找到了。 不过我忘记链接了,不然发上来了。
creator of abstractions. Chapter 1, which has been heavily updated, introduces all the concepts. Chapter 2 is about the abstraction of the CPU into multiple processes. Chapter 3 is about the ...
This book shows you how to evaluate the performance implications of various Clojure abstractions, discover their underpinnings, and apply the right approach for optimum performance in real-world ...
This book began about 20 years ago in the form of supplementary notes for my algebra classes. I wanted to discuss some concrete topics such as symmetry, linear groups, and quadratic number fields in...
These brief tutorials are intended to get you started with TinyOS. They show you the basics of writing, compiling, and installing TinyOS applications. They introduce the basic TinyOS abstractions: ...
We then quickly look at the patterns of control flow, data the abstractions of these unique to functional programming. The next part covers how to create functional apps in Rust; mutability and ...