debug_sh.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. *
  6. * Kernel Debug Shell
  7. */
  8. #include <kernel/system.h>
  9. #include <kernel/fs.h>
  10. #include <kernel/printf.h>
  11. #include <kernel/logging.h>
  12. #include <kernel/process.h>
  13. #include <kernel/version.h>
  14. #include <kernel/tokenize.h>
  15. #include <kernel/pci.h>
  16. #include <kernel/pipe.h>
  17. #include <kernel/elf.h>
  18. #include <kernel/module.h>
  19. #include <kernel/args.h>
  20. #include <kernel/mod/shell.h>
  21. #include <toaru/list.h>
  22. #include <toaru/hashmap.h>
  23. #include <sys/termios.h>
  24. /*
  25. * just call read_fs honestly, I don't care anymore
  26. */
  27. int debug_shell_readline(fs_node_t * dev, char * linebuf, int max) {
  28. int r = read_fs(dev, 0, max, (uint8_t *)linebuf);
  29. if (r <= 0) return -1;
  30. if (r && linebuf[r-1] == '\n') {
  31. linebuf[r-1] = '\0';
  32. return r-1;
  33. }
  34. return r;
  35. }
  36. /*
  37. * Tasklet for running a userspace application.
  38. */
  39. static void debug_shell_run_sh(void * data, char * name) {
  40. char * argv[] = {
  41. data,
  42. NULL
  43. };
  44. int argc = 0;
  45. while (argv[argc]) {
  46. argc++;
  47. }
  48. char * env[] = {
  49. "LD_LIBRARY_PATH=/lib",
  50. NULL
  51. };
  52. system(argv[0], argc, argv, env); /* Run shell */
  53. task_exit(42);
  54. }
  55. static hashmap_t * shell_commands_map = NULL;
  56. /*
  57. * Shell commands
  58. */
  59. static int shell_create_userspace_shell(fs_node_t * tty, int argc, char * argv[]) {
  60. int pid = create_kernel_tasklet(debug_shell_run_sh, "[[k-sh]]", "/bin/sh");
  61. fprintf(tty, "Shell started with pid = %d\n", pid);
  62. int status;
  63. waitpid(pid,&status,0);
  64. return status;
  65. }
  66. static int shell_replace_login(fs_node_t * tty, int argc, char * argv[]) {
  67. /* We need to fork to get a clean task space */
  68. create_kernel_tasklet(debug_shell_run_sh, "[[k-sh]]", "/bin/login");
  69. /* Then exit the shell process */
  70. task_exit(0);
  71. /* unreachable */
  72. return 0;
  73. }
  74. static int shell_echo(fs_node_t * tty, int argc, char * argv[]) {
  75. for (int i = 1; i < argc; ++i) {
  76. fprintf(tty, "%s ", argv[i]);
  77. }
  78. fprintf(tty, "\n");
  79. return 0;
  80. }
  81. static int dumb_strcmp(void * a, void *b) {
  82. return strcmp(a, b);
  83. }
  84. static void dumb_sort(void ** list, size_t length, int (*compare)(void*,void*)) {
  85. for (unsigned int i = 0; i < length-1; ++i) {
  86. for (unsigned int j = 0; j < length-1; ++j) {
  87. if (compare(list[j], list[j+1]) > 0) {
  88. void * t = list[j+1];
  89. list[j+1] = list[j];
  90. list[j] = t;
  91. }
  92. }
  93. }
  94. }
  95. static void print_spaces(fs_node_t * tty, int num_spaces) {
  96. for (int i = 0; i < num_spaces; ++i) {
  97. fprintf(tty, " ");
  98. }
  99. }
  100. static int shell_help(fs_node_t * tty, int argc, char * argv[]) {
  101. list_t * hash_keys = hashmap_keys(shell_commands_map);
  102. char ** keys = malloc(sizeof(char *) * hash_keys->length);
  103. unsigned int i = 0;
  104. unsigned int max_width = 0;
  105. foreach(_key, hash_keys) {
  106. char * key = (char *)_key->value;
  107. keys[i] = key;
  108. i++;
  109. if (strlen(key) > max_width) {
  110. max_width = strlen(key);
  111. }
  112. }
  113. dumb_sort((void **)keys, hash_keys->length, &dumb_strcmp);
  114. for (i = 0; i < hash_keys->length; ++i) {
  115. struct shell_command * c = hashmap_get(shell_commands_map, keys[i]);
  116. fprintf(tty, "\033[1;32m%s\033[0m ", c->name);
  117. print_spaces(tty, max_width- strlen(c->name));
  118. fprintf(tty, "- %s\n", c->description);
  119. }
  120. free(keys);
  121. list_free(hash_keys);
  122. free(hash_keys);
  123. return 0;
  124. }
  125. static int shell_cd(fs_node_t * tty, int argc, char * argv[]) {
  126. if (argc < 2) {
  127. return 1;
  128. }
  129. char * newdir = argv[1];
  130. char * path = canonicalize_path(current_process->wd_name, newdir);
  131. fs_node_t * chd = kopen(path, 0);
  132. if (chd) {
  133. if ((chd->flags & FS_DIRECTORY) == 0) {
  134. return 1;
  135. }
  136. close_fs(chd);
  137. free(current_process->wd_name);
  138. current_process->wd_name = malloc(strlen(path) + 1);
  139. memcpy(current_process->wd_name, path, strlen(path) + 1);
  140. return 0;
  141. } else {
  142. return 1;
  143. }
  144. }
  145. static int shell_ls(fs_node_t * tty, int argc, char * argv[]) {
  146. fs_node_t * wd;
  147. if (argc < 2) {
  148. wd = kopen(current_process->wd_name, 0);
  149. } else {
  150. wd = kopen(argv[1], 0);
  151. }
  152. uint32_t index = 0;
  153. struct dirent * kentry = readdir_fs(wd, index);
  154. while (kentry) {
  155. fprintf(tty, "%s\n", kentry->name);
  156. free(kentry);
  157. index++;
  158. kentry = readdir_fs(wd, index);
  159. }
  160. close_fs(wd);
  161. return 0;
  162. }
  163. static int shell_cat(fs_node_t * tty, int argc, char * argv[]) {
  164. if (argc < 2) {
  165. fprintf(tty, "Usage: cat <file>\n");
  166. return 1;
  167. }
  168. fs_node_t * node = kopen(argv[1], 0);
  169. if (!node) {
  170. fprintf(tty, "Could not open %s.\n", argv[1]);
  171. return 1;
  172. }
  173. #define CHUNK_SIZE 4096
  174. uint8_t * buf = malloc(CHUNK_SIZE);
  175. memset(buf, 0, CHUNK_SIZE);
  176. size_t offset = 0;
  177. while (1) {
  178. size_t r = read_fs(node, offset, CHUNK_SIZE, buf);
  179. if (!r) break;
  180. write_fs(tty, 0, r, buf);
  181. offset += r;
  182. }
  183. close_fs(node);
  184. return 0;
  185. }
  186. static int shell_log(fs_node_t * tty, int argc, char * argv[]) {
  187. if (argc < 2) {
  188. fprintf(tty, "Log level is currently %d.\n", debug_level);
  189. fprintf(tty, "Serial logging is %s.\n", !!debug_file ? "enabled" : "disabled");
  190. fprintf(tty, "Usage: log [on|off] [<level>]\n");
  191. } else {
  192. if (!strcmp(argv[1], "direct")) {
  193. debug_file = kopen("/dev/ttyS0", 0);
  194. if (argc > 2) {
  195. debug_level = atoi(argv[2]);
  196. }
  197. } else if (!strcmp(argv[1], "on")) {
  198. debug_file = tty;
  199. if (argc > 2) {
  200. debug_level = atoi(argv[2]);
  201. }
  202. } else if (!strcmp(argv[1], "off")) {
  203. debug_file = NULL;
  204. }
  205. }
  206. return 0;
  207. }
  208. static void scan_hit_list(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
  209. fs_node_t * tty = extra;
  210. fprintf(tty, "%2x:%2x.%d (%4x, %4x:%4x)\n",
  211. (int)pci_extract_bus(device),
  212. (int)pci_extract_slot(device),
  213. (int)pci_extract_func(device),
  214. (int)pci_find_type(device),
  215. vendorid,
  216. deviceid);
  217. fprintf(tty, " BAR0: 0x%8x", pci_read_field(device, PCI_BAR0, 4));
  218. fprintf(tty, " BAR1: 0x%8x", pci_read_field(device, PCI_BAR1, 4));
  219. fprintf(tty, " BAR2: 0x%8x", pci_read_field(device, PCI_BAR2, 4));
  220. fprintf(tty, " BAR3: 0x%8x", pci_read_field(device, PCI_BAR3, 4));
  221. fprintf(tty, " BAR4: 0x%8x", pci_read_field(device, PCI_BAR4, 4));
  222. fprintf(tty, " BAR5: 0x%8x\n", pci_read_field(device, PCI_BAR5, 4));
  223. fprintf(tty, " IRQ Line: %d", pci_read_field(device, 0x3C, 1));
  224. fprintf(tty, " IRQ Pin: %d", pci_read_field(device, 0x3D, 1));
  225. fprintf(tty, " Interrupt: %d", pci_get_interrupt(device));
  226. fprintf(tty, " Status: 0x%4x\n", pci_read_field(device, PCI_STATUS, 2));
  227. }
  228. static int shell_pci(fs_node_t * tty, int argc, char * argv[]) {
  229. pci_scan(&scan_hit_list, -1, tty);
  230. return 0;
  231. }
  232. static void find_isa_bridge(uint32_t device, uint16_t vendorid, uint16_t deviceid, void * extra) {
  233. if (vendorid == 0x8086 && (deviceid == 0x7000 || deviceid == 0x7110)) {
  234. *((uint32_t *)extra) = device;
  235. }
  236. }
  237. static int shell_frob_piix(fs_node_t * tty, int argc, char * argv[]) {
  238. uint32_t pci_isa = 0;
  239. pci_scan(&find_isa_bridge, -1, &pci_isa);
  240. if (pci_isa) {
  241. fprintf(tty, "PCI-to-ISA interrupt mappings by line:\n");
  242. for (int i = 0; i < 4; ++i) {
  243. fprintf(tty, "Line %d: 0x%2x\n", i+1, pci_read_field(pci_isa, 0x60+i, 1));
  244. }
  245. }
  246. return 0;
  247. }
  248. static int shell_uid(fs_node_t * tty, int argc, char * argv[]) {
  249. if (argc < 2) {
  250. fprintf(tty, "uid=%d\n", current_process->user);
  251. } else {
  252. current_process->user = atoi(argv[1]);
  253. }
  254. return 0;
  255. }
  256. char * special_thing = "I am a string from the kernel.\n";
  257. static int shell_mod(fs_node_t * tty, int argc, char * argv[]) {
  258. if (argc < 2) {
  259. fprintf(tty, "%s: expected argument\n", argv[0]);
  260. return 1;
  261. }
  262. fs_node_t * file = kopen(argv[1], 0);
  263. if (!file) {
  264. fprintf(tty, "%s: Error loading module '%s': File not found\n", argv[0], argv[1]);
  265. return 1;
  266. }
  267. close_fs(file);
  268. module_data_t * mod_info = module_load(argv[1]);
  269. if (!mod_info) {
  270. fprintf(tty, "%s: Error loading module '%s'\n", argv[0], argv[1]);
  271. return 1;
  272. }
  273. fprintf(tty, "Module '%s' loaded at 0x%x\n", mod_info->mod_info->name, mod_info->bin_data);
  274. return 0;
  275. }
  276. static int shell_symbols(fs_node_t * tty, int argc, char * argv[]) {
  277. if (argc > 1 && !strcmp(argv[1],"--all")) {
  278. list_t * hash_keys = hashmap_keys(modules_get_symbols());
  279. foreach(_key, hash_keys) {
  280. char * key = (char *)_key->value;
  281. uintptr_t a = (uintptr_t)hashmap_get(modules_get_symbols(), key);
  282. fprintf(tty, "0x%x - %s\n", a, key);
  283. }
  284. free(hash_keys);
  285. } else {
  286. extern char kernel_symbols_start[];
  287. extern char kernel_symbols_end[];
  288. struct ksym {
  289. uintptr_t addr;
  290. char name[];
  291. } * k = (void*)&kernel_symbols_start;
  292. while ((uintptr_t)k < (uintptr_t)&kernel_symbols_end) {
  293. fprintf(tty, "0x%x - %s\n", k->addr, k->name);
  294. k = (void *)((uintptr_t)k + sizeof(uintptr_t) + strlen(k->name) + 1);
  295. }
  296. }
  297. return 0;
  298. }
  299. static int shell_print(fs_node_t * tty, int argc, char * argv[]) {
  300. if (argc < 3) {
  301. fprintf(tty, "print format_string symbol_name\n");
  302. return 1;
  303. }
  304. char * format = argv[1];
  305. char * symbol = argv[2];
  306. int deref = 0;
  307. if (symbol[0] == '*') {
  308. symbol = &symbol[1];
  309. deref = 1;
  310. }
  311. void * addr = hashmap_get(modules_get_symbols(),symbol);
  312. if (!addr) return 1;
  313. if (deref) {
  314. fprintf(tty, format, addr);
  315. } else {
  316. fprintf(tty, format, *((uintptr_t *)addr));
  317. }
  318. fprintf(tty, "\n");
  319. return 0;
  320. }
  321. static int shell_call(fs_node_t * tty, int argc, char * argv[]) {
  322. if (argc < 2) {
  323. fprintf(tty, "call function_name\n");
  324. return 1;
  325. }
  326. char * symbol = argv[1];
  327. void (*addr)(void) = (void (*)(void))(uintptr_t)hashmap_get(modules_get_symbols(),symbol);
  328. if (!addr) return 1;
  329. addr();
  330. return 0;
  331. }
  332. static int shell_modules(fs_node_t * tty, int argc, char * argv[]) {
  333. list_t * hash_keys = hashmap_keys(modules_get_list());
  334. foreach(_key, hash_keys) {
  335. char * key = (char *)_key->value;
  336. module_data_t * mod_info = hashmap_get(modules_get_list(), key);
  337. fprintf(tty, "0x%x {.init=0x%x, .fini=0x%x} %s",
  338. mod_info->bin_data,
  339. mod_info->mod_info->initialize,
  340. mod_info->mod_info->finalize,
  341. mod_info->mod_info->name);
  342. if (mod_info->deps) {
  343. unsigned int i = 0;
  344. fprintf(tty, " Deps: ");
  345. while (i < mod_info->deps_length) {
  346. fprintf(tty, "%s ", &mod_info->deps[i]);
  347. i += strlen(&mod_info->deps[i]) + 1;
  348. }
  349. }
  350. fprintf(tty, "\n");
  351. }
  352. free(hash_keys);
  353. return 0;
  354. }
  355. static int shell_rdtsc(fs_node_t * tty, int argc, char * argv[]) {
  356. uint64_t x;
  357. asm volatile ("rdtsc" : "=A" (x));
  358. fprintf(tty, "0x%x%x\n", (uint32_t)(x >> 32), (uint32_t)(x & 0xFFFFFFFF));
  359. return 0;
  360. }
  361. static int shell_mhz(fs_node_t * tty, int argc, char * argv[]) {
  362. uint64_t x, y;
  363. asm volatile ("rdtsc" : "=A" (x));
  364. unsigned long s, ss;
  365. relative_time(1, 0, &s, &ss);
  366. sleep_until((process_t *)current_process, s, ss);
  367. switch_task(0);
  368. asm volatile ("rdtsc" : "=A" (y));
  369. uint64_t diff = y - x;
  370. uint32_t f = diff >> 15;
  371. uint32_t mhz = f / 30;
  372. fprintf(tty, "%d MHz\n", mhz);
  373. return 0;
  374. }
  375. /*
  376. * Determine the size of a smart terminal that we don't have direct
  377. * termios access to. This is done by sending a cursor-move command
  378. * that will put the cursor into the lower right corner and then
  379. * requesting the cursor position report. We then read and parse
  380. * the position report. In the case where the terminal on the other
  381. * end is actually dumb, we end up waiting for some input and
  382. * then timing out.
  383. * TODO with asyncio support, the timeout should actually work.
  384. * consider also using an alarm (which I also don't have)
  385. */
  386. static void divine_size(fs_node_t * dev, int * width, int * height) {
  387. char tmp[100];
  388. int read = 0;
  389. unsigned long start_tick = timer_ticks;
  390. memset(tmp, 0, sizeof(tmp));
  391. /* Move cursor, Request position, Reset cursor */
  392. fprintf(dev, "\033[1000;1000H\033[6n\033[H");
  393. while (1) {
  394. char buf[1];
  395. int r = read_fs(dev, 0, 1, (unsigned char *)buf);
  396. if (r > 0) {
  397. if (buf[0] != 'R') {
  398. if (read > 1) {
  399. tmp[read-2] = buf[0];
  400. }
  401. read++;
  402. } else {
  403. break;
  404. }
  405. }
  406. if (timer_ticks - start_tick >= 2) {
  407. /*
  408. * We've timed out. This will only be triggered
  409. * when we eventually receive something, though
  410. */
  411. *width = 80;
  412. *height = 23;
  413. /* Clear and return */
  414. fprintf(dev, "\033[J");
  415. return;
  416. }
  417. }
  418. /* Clear */
  419. fprintf(dev, "\033[J");
  420. /* Break up the result into two strings */
  421. for (unsigned int i = 0; i < strlen(tmp); i++) {
  422. if (tmp[i] == ';') {
  423. tmp[i] = '\0';
  424. break;
  425. }
  426. }
  427. char * h = (char *)((uintptr_t)tmp + strlen(tmp)+1);
  428. /* And then parse it into numbers */
  429. *height = atoi(tmp);
  430. *width = atoi(h);
  431. }
  432. static int shell_divinesize(fs_node_t * tty, int argc, char * argv[]) {
  433. struct winsize size = {0,0,0,0};
  434. /* Attempt to divine the terminal size. Changing the window size after this will do bad things */
  435. int width, height;
  436. divine_size(tty, &width, &height);
  437. fprintf(tty, "Identified size: %d x %d\n", width, height);
  438. size.ws_row = height;
  439. size.ws_col = width;
  440. ioctl_fs(tty, TIOCSWINSZ, &size);
  441. return 0;
  442. }
  443. static int shell_fix_mouse(fs_node_t * tty, int argc, char * argv[]) {
  444. fs_node_t * mouse = kopen("/dev/mouse", 0);
  445. if (mouse) {
  446. ioctl_fs(mouse, 1, NULL);
  447. close_fs(mouse);
  448. }
  449. return 0;
  450. }
  451. static int shell_mount(fs_node_t * tty, int argc, char * argv[]) {
  452. if (argc < 4) {
  453. fprintf(tty, "Usage: %s type device mountpoint\n", argv[0]);
  454. return 1;
  455. }
  456. return -vfs_mount_type(argv[1], argv[2], argv[3]);
  457. }
  458. static int shell_exit(fs_node_t * tty, int argc, char * argv[]) {
  459. kexit(0);
  460. return 0;
  461. }
  462. static int shell_cursor_off(fs_node_t * tty, int argc, char * argv[]) {
  463. outportb(0x3D4, 14);
  464. outportb(0x3D5, 0xFF);
  465. outportb(0x3D4, 15);
  466. outportb(0x3D5, 0xFF);
  467. return 0;
  468. }
  469. extern pid_t trace_pid;
  470. static int shell_debug_pid(fs_node_t * tty, int argc, char * argv[]) {
  471. trace_pid = atoi(argv[1]);
  472. return 0;
  473. }
  474. extern hashmap_t * kernel_args_map;
  475. static int shell_set(fs_node_t * tty, int argc, char * argv[]) {
  476. if (argc < 3) {
  477. fprintf(tty, "set KEY VALUE\n");
  478. return 1;
  479. }
  480. hashmap_set(kernel_args_map, argv[1], strdup(argv[2]));
  481. return 0;
  482. }
  483. static int shell_coax_irq(fs_node_t * tty, int argc, char * argv[]) {
  484. if (argc < 2) {
  485. fprintf(tty, "coax-irq IRQ\n");
  486. return 1;
  487. }
  488. irq_ack(atoi(argv[1]));
  489. return 0;
  490. }
  491. static struct shell_command shell_commands[] = {
  492. {"shell", &shell_create_userspace_shell,
  493. "Runs a userspace shell on this tty."},
  494. {"login", &shell_replace_login,
  495. "Replace the debug shell with /bin/login."},
  496. {"echo", &shell_echo,
  497. "Prints arguments."},
  498. {"help", &shell_help,
  499. "Prints a list of possible shell commands and their descriptions."},
  500. {"cd", &shell_cd,
  501. "Change current directory."},
  502. {"ls", &shell_ls,
  503. "List files in current or other directory."},
  504. {"cat", &shell_cat,
  505. "Read a file to the console."},
  506. {"log", &shell_log,
  507. "Configure serial debug logging."},
  508. {"pci", &shell_pci,
  509. "Print PCI devices, as well as their names and BARs."},
  510. {"uid", &shell_uid,
  511. "Change the effective user id of the shell."},
  512. {"mod", &shell_mod,
  513. "[testing] Module loading."},
  514. {"symbols", &shell_symbols,
  515. "Dump symbol table."},
  516. {"debug_pid", &shell_debug_pid,
  517. "Set pid to trace syscalls for."},
  518. {"print", &shell_print,
  519. "[dangerous] Print the value of a symbol using a format string."},
  520. {"call", &shell_call,
  521. "[dangerous] Call a function by name."},
  522. {"modules", &shell_modules,
  523. "Print names and addresses of all loaded modules."},
  524. {"divine-size", &shell_divinesize,
  525. "Attempt to discover TTY size of serial."},
  526. {"fix-mouse", &shell_fix_mouse,
  527. "Attempt to reset mouse device."},
  528. {"mount", &shell_mount,
  529. "Mount a filesystemp."},
  530. {"rdtsc", &shell_rdtsc,
  531. "Read the TSC, if available."},
  532. {"mhz", &shell_mhz,
  533. "Use TSC to determine clock speed."},
  534. {"cursor-off", &shell_cursor_off,
  535. "Disable VGA text mode cursor."},
  536. {"exit", &shell_exit,
  537. "Quit the shell."},
  538. {"piix", &shell_frob_piix,
  539. "frob piix"},
  540. {"set", &shell_set,
  541. "set kcmdline flag"},
  542. {"coax-irq", &shell_coax_irq,
  543. "force ack an irq"},
  544. {NULL, NULL, NULL}
  545. };
  546. void debug_shell_install(struct shell_command * sh) {
  547. hashmap_set(shell_commands_map, sh->name, sh);
  548. }
  549. /*
  550. * A TTY object to pass to the tasklets for handling
  551. * serial-tty interaction. This probably shouldn't
  552. * be done as tasklets - TTYs should just be able
  553. * to wrap existing fs_nodes themselves, but that's
  554. * a problem for another day.
  555. */
  556. struct tty_o {
  557. fs_node_t * node;
  558. fs_node_t * tty;
  559. };
  560. static void debug_shell_actual(void * data, char * name) {
  561. current_process->image.entry = 0;
  562. fs_node_t * tty = (fs_node_t *)current_process->fds->entries[1];
  563. /* Our prompt will include the version number of the current kernel */
  564. char version_number[1024];
  565. sprintf(version_number, __kernel_version_format,
  566. __kernel_version_major,
  567. __kernel_version_minor,
  568. __kernel_version_lower,
  569. __kernel_version_suffix);
  570. /* Initialize the shell commands map */
  571. int retval = 0;
  572. while (1) {
  573. char command[512];
  574. /* Print out the prompt */
  575. if (retval) {
  576. fprintf(tty, "\033[1;34m%s-%s \033[1;31m%d\033[1;34m %s#\033[0m ", __kernel_name, version_number, retval, current_process->wd_name);
  577. } else {
  578. fprintf(tty, "\033[1;34m%s-%s %s#\033[0m ", __kernel_name, version_number, current_process->wd_name);
  579. }
  580. /* Read a line */
  581. if (debug_shell_readline(current_process->fds->entries[0], command, 511) < 0) {
  582. kexit(0);
  583. }
  584. char * arg = strdup(command);
  585. char * argv[1024]; /* Command tokens (space-separated elements) */
  586. int argc = tokenize(arg, " ", argv);
  587. if (!argc) continue;
  588. /* Parse the command string */
  589. struct shell_command * sh = hashmap_get(shell_commands_map, argv[0]);
  590. if (sh) {
  591. retval = sh->function(tty, argc, argv);
  592. } else {
  593. fprintf(tty, "Unrecognized command: %s\n", argv[0]);
  594. }
  595. free(arg);
  596. }
  597. }
  598. /*
  599. * Tasklet for managing the kernel serial console.
  600. * This is basically a very simple shell, with access
  601. * to some internal kernel commands, and (eventually)
  602. * debugging routines.
  603. */
  604. static void debug_shell_run(void * data, char * name) {
  605. fs_node_t * tty = kopen("/dev/ttyS0", 0);
  606. int fd = process_append_fd((process_t *)current_process, tty);
  607. current_process->fds->modes[fd] = 03; /* rw */
  608. process_move_fd((process_t *)current_process, fd, 0);
  609. process_move_fd((process_t *)current_process, fd, 1);
  610. process_move_fd((process_t *)current_process, fd, 2);
  611. debug_shell_actual(tty, name);
  612. }
  613. int debug_shell_start(void) {
  614. /* Setup shell commands */
  615. shell_commands_map = hashmap_create(10);
  616. struct shell_command * sh = &shell_commands[0];
  617. while (sh->name) {
  618. hashmap_set(shell_commands_map, sh->name, sh);
  619. sh++;
  620. }
  621. debug_hook = debug_shell_actual;
  622. if (args_present("kdebug")) {
  623. int i = create_kernel_tasklet(debug_shell_run, "[kttydebug]", NULL);
  624. debug_print(NOTICE, "Started tasklet with pid=%d", i);
  625. }
  626. return 0;
  627. }
  628. int debug_shell_stop(void) {
  629. debug_print(NOTICE, "Tried to unload debug shell, but debug shell has no real shutdown routine. Don't do that!");
  630. return 0;
  631. }
  632. MODULE_DEF(debugshell, debug_shell_start, debug_shell_stop);
  633. MODULE_DEPENDS(serial);