《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
12 KiB

10 years ago
  1. #include <defs.h>
  2. #include <x86.h>
  3. #include <stab.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <kdebug.h>
  7. #define STACKFRAME_DEPTH 20
  8. extern const struct stab __STAB_BEGIN__[]; // beginning of stabs table
  9. extern const struct stab __STAB_END__[]; // end of stabs table
  10. extern const char __STABSTR_BEGIN__[]; // beginning of string table
  11. extern const char __STABSTR_END__[]; // end of string table
  12. /* debug information about a particular instruction pointer */
  13. struct eipdebuginfo {
  14. const char *eip_file; // source code filename for eip
  15. int eip_line; // source code line number for eip
  16. const char *eip_fn_name; // name of function containing eip
  17. int eip_fn_namelen; // length of function's name
  18. uintptr_t eip_fn_addr; // start address of function
  19. int eip_fn_narg; // number of function arguments
  20. };
  21. /* *
  22. * stab_binsearch - according to the input, the initial value of
  23. * range [*@region_left, *@region_right], find a single stab entry
  24. * that includes the address @addr and matches the type @type,
  25. * and then save its boundary to the locations that pointed
  26. * by @region_left and @region_right.
  27. *
  28. * Some stab types are arranged in increasing order by instruction address.
  29. * For example, N_FUN stabs (stab entries with n_type == N_FUN), which
  30. * mark functions, and N_SO stabs, which mark source files.
  31. *
  32. * Given an instruction address, this function finds the single stab entry
  33. * of type @type that contains that address.
  34. *
  35. * The search takes place within the range [*@region_left, *@region_right].
  36. * Thus, to search an entire set of N stabs, you might do:
  37. *
  38. * left = 0;
  39. * right = N - 1; (rightmost stab)
  40. * stab_binsearch(stabs, &left, &right, type, addr);
  41. *
  42. * The search modifies *region_left and *region_right to bracket the @addr.
  43. * *@region_left points to the matching stab that contains @addr,
  44. * and *@region_right points just before the next stab.
  45. * If *@region_left > *region_right, then @addr is not contained in any
  46. * matching stab.
  47. *
  48. * For example, given these N_SO stabs:
  49. * Index Type Address
  50. * 0 SO f0100000
  51. * 13 SO f0100040
  52. * 117 SO f0100176
  53. * 118 SO f0100178
  54. * 555 SO f0100652
  55. * 556 SO f0100654
  56. * 657 SO f0100849
  57. * this code:
  58. * left = 0, right = 657;
  59. * stab_binsearch(stabs, &left, &right, N_SO, 0xf0100184);
  60. * will exit setting left = 118, right = 554.
  61. * */
  62. static void
  63. stab_binsearch(const struct stab *stabs, int *region_left, int *region_right,
  64. int type, uintptr_t addr) {
  65. int l = *region_left, r = *region_right, any_matches = 0;
  66. while (l <= r) {
  67. int true_m = (l + r) / 2, m = true_m;
  68. // search for earliest stab with right type
  69. while (m >= l && stabs[m].n_type != type) {
  70. m --;
  71. }
  72. if (m < l) { // no match in [l, m]
  73. l = true_m + 1;
  74. continue;
  75. }
  76. // actual binary search
  77. any_matches = 1;
  78. if (stabs[m].n_value < addr) {
  79. *region_left = m;
  80. l = true_m + 1;
  81. } else if (stabs[m].n_value > addr) {
  82. *region_right = m - 1;
  83. r = m - 1;
  84. } else {
  85. // exact match for 'addr', but continue loop to find
  86. // *region_right
  87. *region_left = m;
  88. l = m;
  89. addr ++;
  90. }
  91. }
  92. if (!any_matches) {
  93. *region_right = *region_left - 1;
  94. }
  95. else {
  96. // find rightmost region containing 'addr'
  97. l = *region_right;
  98. for (; l > *region_left && stabs[l].n_type != type; l --)
  99. /* do nothing */;
  100. *region_left = l;
  101. }
  102. }
  103. /* *
  104. * debuginfo_eip - Fill in the @info structure with information about
  105. * the specified instruction address, @addr. Returns 0 if information
  106. * was found, and negative if not. But even if it returns negative it
  107. * has stored some information into '*info'.
  108. * */
  109. int
  110. debuginfo_eip(uintptr_t addr, struct eipdebuginfo *info) {
  111. const struct stab *stabs, *stab_end;
  112. const char *stabstr, *stabstr_end;
  113. info->eip_file = "<unknown>";
  114. info->eip_line = 0;
  115. info->eip_fn_name = "<unknown>";
  116. info->eip_fn_namelen = 9;
  117. info->eip_fn_addr = addr;
  118. info->eip_fn_narg = 0;
  119. stabs = __STAB_BEGIN__;
  120. stab_end = __STAB_END__;
  121. stabstr = __STABSTR_BEGIN__;
  122. stabstr_end = __STABSTR_END__;
  123. // String table validity checks
  124. if (stabstr_end <= stabstr || stabstr_end[-1] != 0) {
  125. return -1;
  126. }
  127. // Now we find the right stabs that define the function containing
  128. // 'eip'. First, we find the basic source file containing 'eip'.
  129. // Then, we look in that source file for the function. Then we look
  130. // for the line number.
  131. // Search the entire set of stabs for the source file (type N_SO).
  132. int lfile = 0, rfile = (stab_end - stabs) - 1;
  133. stab_binsearch(stabs, &lfile, &rfile, N_SO, addr);
  134. if (lfile == 0)
  135. return -1;
  136. // Search within that file's stabs for the function definition
  137. // (N_FUN).
  138. int lfun = lfile, rfun = rfile;
  139. int lline, rline;
  140. stab_binsearch(stabs, &lfun, &rfun, N_FUN, addr);
  141. if (lfun <= rfun) {
  142. // stabs[lfun] points to the function name
  143. // in the string table, but check bounds just in case.
  144. if (stabs[lfun].n_strx < stabstr_end - stabstr) {
  145. info->eip_fn_name = stabstr + stabs[lfun].n_strx;
  146. }
  147. info->eip_fn_addr = stabs[lfun].n_value;
  148. addr -= info->eip_fn_addr;
  149. // Search within the function definition for the line number.
  150. lline = lfun;
  151. rline = rfun;
  152. } else {
  153. // Couldn't find function stab! Maybe we're in an assembly
  154. // file. Search the whole file for the line number.
  155. info->eip_fn_addr = addr;
  156. lline = lfile;
  157. rline = rfile;
  158. }
  159. info->eip_fn_namelen = strfind(info->eip_fn_name, ':') - info->eip_fn_name;
  160. // Search within [lline, rline] for the line number stab.
  161. // If found, set info->eip_line to the right line number.
  162. // If not found, return -1.
  163. stab_binsearch(stabs, &lline, &rline, N_SLINE, addr);
  164. if (lline <= rline) {
  165. info->eip_line = stabs[rline].n_desc;
  166. } else {
  167. return -1;
  168. }
  169. // Search backwards from the line number for the relevant filename stab.
  170. // We can't just use the "lfile" stab because inlined functions
  171. // can interpolate code from a different file!
  172. // Such included source files use the N_SOL stab type.
  173. while (lline >= lfile
  174. && stabs[lline].n_type != N_SOL
  175. && (stabs[lline].n_type != N_SO || !stabs[lline].n_value)) {
  176. lline --;
  177. }
  178. if (lline >= lfile && stabs[lline].n_strx < stabstr_end - stabstr) {
  179. info->eip_file = stabstr + stabs[lline].n_strx;
  180. }
  181. // Set eip_fn_narg to the number of arguments taken by the function,
  182. // or 0 if there was no containing function.
  183. if (lfun < rfun) {
  184. for (lline = lfun + 1;
  185. lline < rfun && stabs[lline].n_type == N_PSYM;
  186. lline ++) {
  187. info->eip_fn_narg ++;
  188. }
  189. }
  190. return 0;
  191. }
  192. /* *
  193. * print_kerninfo - print the information about kernel, including the location
  194. * of kernel entry, the start addresses of data and text segements, the start
  195. * address of free memory and how many memory that kernel has used.
  196. * */
  197. void
  198. print_kerninfo(void) {
  199. extern char etext[], edata[], end[], kern_init[];
  200. cprintf("Special kernel symbols:\n");
  201. cprintf(" entry 0x%08x (phys)\n", kern_init);
  202. cprintf(" etext 0x%08x (phys)\n", etext);
  203. cprintf(" edata 0x%08x (phys)\n", edata);
  204. cprintf(" end 0x%08x (phys)\n", end);
  205. cprintf("Kernel executable memory footprint: %dKB\n", (end - kern_init + 1023)/1024);
  206. }
  207. /* *
  208. * print_debuginfo - read and print the stat information for the address @eip,
  209. * and info.eip_fn_addr should be the first address of the related function.
  210. * */
  211. void
  212. print_debuginfo(uintptr_t eip) {
  213. struct eipdebuginfo info;
  214. if (debuginfo_eip(eip, &info) != 0) {
  215. cprintf(" <unknow>: -- 0x%08x --\n", eip);
  216. }
  217. else {
  218. char fnname[256];
  219. int j;
  220. for (j = 0; j < info.eip_fn_namelen; j ++) {
  221. fnname[j] = info.eip_fn_name[j];
  222. }
  223. fnname[j] = '\0';
  224. cprintf(" %s:%d: %s+%d\n", info.eip_file, info.eip_line,
  225. fnname, eip - info.eip_fn_addr);
  226. }
  227. }
  228. static __noinline uint32_t
  229. read_eip(void) {
  230. uint32_t eip;
  231. asm volatile("movl 4(%%ebp), %0" : "=r" (eip));
  232. return eip;
  233. }
  234. /* *
  235. * print_stackframe - print a list of the saved eip values from the nested 'call'
  236. * instructions that led to the current point of execution
  237. *
  238. * The x86 stack pointer, namely esp, points to the lowest location on the stack
  239. * that is currently in use. Everything below that location in stack is free. Pushing
  240. * a value onto the stack will invole decreasing the stack pointer and then writing
  241. * the value to the place that stack pointer pointes to. And popping a value do the
  242. * opposite.
  243. *
  244. * The ebp (base pointer) register, in contrast, is associated with the stack
  245. * primarily by software convention. On entry to a C function, the function's
  246. * prologue code normally saves the previous function's base pointer by pushing
  247. * it onto the stack, and then copies the current esp value into ebp for the duration
  248. * of the function. If all the functions in a program obey this convention,
  249. * then at any given point during the program's execution, it is possible to trace
  250. * back through the stack by following the chain of saved ebp pointers and determining
  251. * exactly what nested sequence of function calls caused this particular point in the
  252. * program to be reached. This capability can be particularly useful, for example,
  253. * when a particular function causes an assert failure or panic because bad arguments
  254. * were passed to it, but you aren't sure who passed the bad arguments. A stack
  255. * backtrace lets you find the offending function.
  256. *
  257. * The inline function read_ebp() can tell us the value of current ebp. And the
  258. * non-inline function read_eip() is useful, it can read the value of current eip,
  259. * since while calling this function, read_eip() can read the caller's eip from
  260. * stack easily.
  261. *
  262. * In print_debuginfo(), the function debuginfo_eip() can get enough information about
  263. * calling-chain. Finally print_stackframe() will trace and print them for debugging.
  264. *
  265. * Note that, the length of ebp-chain is limited. In boot/bootasm.S, before jumping
  266. * to the kernel entry, the value of ebp has been set to zero, that's the boundary.
  267. * */
  268. void
  269. print_stackframe(void) {
  270. /* LAB1 YOUR CODE : STEP 1 */
  271. /* (1) call read_ebp() to get the value of ebp. the type is (uint32_t);
  272. * (2) call read_eip() to get the value of eip. the type is (uint32_t);
  273. * (3) from 0 .. STACKFRAME_DEPTH
  274. * (3.1) printf value of ebp, eip
  275. * (3.2) (uint32_t)calling arguments [0..4] = the contents in address (unit32_t)ebp +2 [0..4]
  276. * (3.3) cprintf("\n");
  277. * (3.4) call print_debuginfo(eip-1) to print the C calling function name and line number, etc.
  278. * (3.5) popup a calling stackframe
  279. * NOTICE: the calling funciton's return addr eip = ss:[ebp+4]
  280. * the calling funciton's ebp = ss:[ebp]
  281. */
  282. uint32_t ebp = read_ebp(), eip = read_eip();
  283. int i, j;
  284. for (i = 0; ebp != 0 && i < STACKFRAME_DEPTH; i ++) {
  285. cprintf("ebp:0x%08x eip:0x%08x args:", ebp, eip);
  286. uint32_t *args = (uint32_t *)ebp + 2;
  287. for (j = 0; j < 4; j ++) {
  288. cprintf("0x%08x ", args[j]);
  289. }
  290. cprintf("\n");
  291. print_debuginfo(eip - 1);
  292. eip = ((uint32_t *)ebp)[1];
  293. ebp = ((uint32_t *)ebp)[0];
  294. }
  295. }