xref: /haiku/src/add-ons/kernel/generic/tty/module.cpp (revision b28ed9e04a771e5de38be68abd08148c0bbafc56)
1 /*
2  * Copyright 2010, Michael Lotz, mmlr@mlotz.ch.
3  * Copyright 2004, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 #include <new>
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include <lock.h>
14 
15 #include <tty/tty_module.h>
16 
17 #include "tty_private.h"
18 
19 struct mutex gGlobalTTYLock;
20 struct mutex gTTYCookieLock;
21 struct recursive_lock gTTYRequestLock;
22 
23 
24 static void
25 dump_tty_settings(struct tty_settings& settings)
26 {
27 	kprintf("  pgrp_id:      %" B_PRId32 "\n", settings.pgrp_id);
28 	kprintf("  session_id:   %" B_PRId32 "\n", settings.session_id);
29 
30 	kprintf("  termios:\n");
31 	kprintf("    c_iflag:    0x%08" B_PRIx32 "\n", settings.termios.c_iflag);
32 	kprintf("    c_oflag:    0x%08" B_PRIx32 "\n", settings.termios.c_oflag);
33 	kprintf("    c_cflag:    0x%08" B_PRIx32 "\n", settings.termios.c_cflag);
34 	kprintf("    c_lflag:    0x%08" B_PRIx32 "\n", settings.termios.c_lflag);
35 	kprintf("    c_line:     %d\n", settings.termios.c_line);
36 	kprintf("    c_ispeed:   %u\n", settings.termios.c_ispeed);
37 	kprintf("    c_ospeed:   %u\n", settings.termios.c_ospeed);
38 	for (int i = 0; i < NCCS; i++)
39 		kprintf("    c_cc[%02d]:   %d\n", i, settings.termios.c_cc[i]);
40 
41 	kprintf("  wsize:        %u x %u c, %u x %u pxl\n",
42 		settings.window_size.ws_row, settings.window_size.ws_col,
43 		settings.window_size.ws_xpixel, settings.window_size.ws_ypixel);
44 }
45 
46 
47 static void
48 dump_tty_struct(struct tty& tty)
49 {
50 	kprintf("  tty @:        %p\n", &tty);
51 	kprintf("  is_master:    %s\n", tty.is_master ? "true" : "false");
52 	kprintf("  open_count:   %" B_PRId32 "\n", tty.open_count);
53 	kprintf("  select_pool:  %p\n", tty.select_pool);
54 	kprintf("  pending_eof:  %" B_PRIu32 "\n", tty.pending_eof);
55 
56 	kprintf("  input_buffer:\n");
57 	kprintf("    first:      %" B_PRId32 "\n", tty.input_buffer.first);
58 	kprintf("    in:         %lu\n", tty.input_buffer.in);
59 	kprintf("    size:       %lu\n", tty.input_buffer.size);
60 	kprintf("    buffer:     %p\n", tty.input_buffer.buffer);
61 
62 	kprintf("  reader queue:\n");
63 	tty.reader_queue.Dump("    ");
64 	kprintf("  writer queue:\n");
65 	tty.writer_queue.Dump("    ");
66 
67 	dump_tty_settings(*tty.settings);
68 
69 	kprintf("  cookies:     ");
70 	TTYCookieList::Iterator it = tty.cookies.GetIterator();
71 	while (tty_cookie* cookie = it.Next())
72 		kprintf(" %p", cookie);
73 	kprintf("\n");
74 }
75 
76 
77 static int
78 dump_tty(int argc, char** argv)
79 {
80 	if (argc < 2) {
81 		kprintf("Usage: %s <tty address>\n", argv[0]);
82 		return 0;
83 	}
84 
85 	char* endpointer;
86 	uintptr_t index = strtoul(argv[1], &endpointer, 0);
87 	if (*endpointer != '\0') {
88 		kprintf("Invalid tty index.\n");
89 		return 0;
90 	}
91 
92 	struct tty* tty = (struct tty*)index;
93 	dump_tty_struct(*tty);
94 
95 	return 0;
96 }
97 
98 
99 void
100 tty_add_debugger_commands()
101 {
102 	add_debugger_command("tty", &dump_tty, "Dump info on a tty");
103 }
104 
105 
106 void
107 tty_remove_debugger_commands()
108 {
109 	remove_debugger_command("tty", &dump_tty);
110 }
111 
112 
113 static status_t
114 init_tty_module()
115 {
116 	// create the request mutex
117 	recursive_lock_init(&gTTYRequestLock, "tty requests");
118 
119 	// create the global mutex
120 	mutex_init(&gGlobalTTYLock, "tty global");
121 
122 	// create the cookie mutex
123 	mutex_init(&gTTYCookieLock, "tty cookies");
124 
125 	tty_add_debugger_commands();
126 
127 	return B_OK;
128 }
129 
130 
131 static void
132 uninit_tty_module()
133 {
134 	tty_remove_debugger_commands();
135 
136 	recursive_lock_destroy(&gTTYRequestLock);
137 	mutex_destroy(&gTTYCookieLock);
138 	mutex_destroy(&gGlobalTTYLock);
139 }
140 
141 
142 static int32
143 tty_module_std_ops(int32 op, ...)
144 {
145 	switch (op) {
146 		case B_MODULE_INIT:
147 			return init_tty_module();
148 
149 		case B_MODULE_UNINIT:
150 			uninit_tty_module();
151 			return B_OK;
152 	}
153 
154 	return B_BAD_VALUE;
155 }
156 
157 
158 static struct tty_module_info sTTYModule = {
159 	{
160 		B_TTY_MODULE_NAME,
161 		0, //B_KEEP_LOADED,
162 		tty_module_std_ops
163 	},
164 
165 	&tty_create,
166 	&tty_destroy,
167 	&tty_create_cookie,
168 	&tty_close_cookie,
169 	&tty_destroy_cookie,
170 	&tty_read,
171 	&tty_write,
172 	&tty_control,
173 	&tty_select,
174 	&tty_deselect,
175 	&tty_hardware_signal
176 };
177 
178 
179 module_info *modules[] = {
180 	(module_info *)&sTTYModule,
181 	NULL
182 };
183