xref: /haiku/src/add-ons/kernel/drivers/ports/pc_serial/Tracing.cpp (revision 4466b89c65970de4c7236ac87faa2bee4589f413)
1 /*
2  * Copyright (c) 2007-2008 by Michael Lotz
3  * Heavily based on the original usb_serial driver which is:
4  *
5  * Copyright (c) 2003 by Siarzhuk Zharski <imker@gmx.li>
6  * Distributed under the terms of the MIT License.
7  */
8 #include "Tracing.h"
9 #include "Driver.h"
10 
11 #include <stdio.h> //sprintf
12 #include <unistd.h> //posix file i/o - create, write, close
13 #include <Drivers.h>
14 #include <driver_settings.h>
15 
16 
17 #if DEBUG
18 bool gLogEnabled = true;
19 #else
20 bool gLogEnabled = false;
21 #endif
22 
23 bool gLogToFile = false;
24 bool gLogAppend = false;
25 bool gLogFunctionCalls = false;
26 bool gLogFunctionReturns = false;
27 bool gLogFunctionResults = false;
28 
29 static const char *sLogFilePath="/boot/home/"DRIVER_NAME".log";
30 static sem_id sLogLock;
31 
32 
33 void
34 load_settings()
35 {
36 	void *settingsHandle;
37 	settingsHandle = load_driver_settings(DRIVER_NAME);
38 
39 #if !DEBUG
40 	gLogEnabled = get_driver_boolean_parameter(settingsHandle,
41 		"debug_output", gLogEnabled, true);
42 #endif
43 
44 	gLogToFile = get_driver_boolean_parameter(settingsHandle,
45 		"debug_output_in_file", gLogToFile, true);
46 	gLogAppend = !get_driver_boolean_parameter(settingsHandle,
47 		"debug_output_file_rewrite", !gLogAppend, true);
48 	gLogFunctionCalls = get_driver_boolean_parameter(settingsHandle,
49 		"debug_trace_func_calls", gLogFunctionCalls, false);
50 	gLogFunctionReturns = get_driver_boolean_parameter(settingsHandle,
51 		"debug_trace_func_returns", gLogFunctionReturns, false);
52 	gLogFunctionResults = get_driver_boolean_parameter(settingsHandle,
53 		"debug_trace_func_results", gLogFunctionResults, false);
54 
55 	unload_driver_settings(settingsHandle);
56 }
57 
58 
59 void
60 create_log_file()
61 {
62 	if(!gLogToFile)
63 		return;
64 
65 	int flags = O_WRONLY | O_CREAT | (!gLogAppend ? O_TRUNC : 0);
66 	close(open(sLogFilePath, flags, 0666));
67 	sLogLock = create_sem(1, DRIVER_NAME"-logging");
68 }
69 
70 
71 void
72 usb_serial_trace(bool force, const char *format, ...)
73 {
74 	if (!gLogEnabled && !force)
75 		return;
76 
77 	static char buffer[1024];
78 	char *bufferPointer = buffer;
79 	if (!gLogToFile) {
80 		const char *prefix = "\33[32m"DRIVER_NAME":\33[0m ";
81 		strcpy(bufferPointer, prefix);
82 		bufferPointer += strlen(prefix);
83 	}
84 
85 	va_list argumentList;
86 	va_start(argumentList, format);
87 	vsprintf(bufferPointer, format, argumentList);
88 	va_end(argumentList);
89 
90 	if (gLogToFile) {
91 		acquire_sem(sLogLock);
92 		int fd = open(sLogFilePath, O_WRONLY | O_APPEND);
93 		write(fd, buffer, strlen(buffer));
94 		close(fd);
95 		release_sem(sLogLock);
96 	} else
97 		dprintf(buffer);
98 }
99 
100 
101 void
102 trace_termios(struct termios *tios)
103 {
104 	TRACE("struct termios:\n"
105 		"\tc_iflag:  0x%08x\n"
106 		"\tc_oflag:  0x%08x\n"
107 		"\tc_cflag:  0x%08x\n"
108 		"\tc_lflag:  0x%08x\n"
109 		"\tc_line:   0x%08x\n"
110 //		"\tc_ixxxxx: 0x%08x\n"
111 //		"\tc_oxxxxx: 0x%08x\n"
112 		"\tc_cc[0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x]\n",
113 		tios->c_iflag, tios->c_oflag, tios->c_cflag, tios->c_lflag,
114 		tios->c_line,
115 //		tios->c_ixxxxx, tios->c_oxxxxx,
116 		tios->c_cc[0], tios->c_cc[1], tios->c_cc[2], tios->c_cc[3],
117 		tios->c_cc[4], tios->c_cc[5], tios->c_cc[6], tios->c_cc[7],
118 		tios->c_cc[8], tios->c_cc[9], tios->c_cc[10]);
119 }
120 
121 
122 #ifdef __BEOS__
123 
124 
125 void
126 trace_ddomain(struct ddomain *dd)
127 {
128 	TRACE("struct ddomain:\n"
129 		"\tddrover: 0x%08x\n"
130 		"\tbg: %d, locked: %d\n", dd->r, dd->bg, dd->locked);
131 }
132 
133 
134 void
135 trace_str(struct str *str)
136 {
137 	TRACE("struct str:\n"
138 		"\tbuffer:    0x%08x\n"
139 		"\tbufsize:   %d\n"
140 		"\tcount:     %d\n"
141 		"\ttail:      %d\n"
142 		"\tallocated: %d\n",
143 		str->buffer, str->bufsize, str->count, str->tail, str->allocated);
144 }
145 
146 
147 void
148 trace_winsize(struct winsize *ws)
149 {
150 	TRACE("struct winsize:\n"
151 		"\tws_row:    %d\n"
152 		"\tws_col:    %d\n"
153 		"\tws_xpixel: %d\n"
154 		"\tws_ypixel: %d\n",
155 		ws->ws_row, ws->ws_col, ws->ws_xpixel, ws->ws_ypixel);
156 }
157 
158 
159 void
160 trace_tty(struct tty *tty)
161 {
162 	TRACE("struct tty:\n"
163 		"\tnopen: %d, flags: 0x%08x,\n", tty->nopen, tty->flags);
164 
165 	TRACE("ddomain dd:\n");
166 	trace_ddomain(&tty->dd);
167 	TRACE("ddomain ddi:\n");
168 	trace_ddomain(&tty->ddi);
169 
170 	TRACE("\tpgid: %08x\n", tty->pgid);
171 	TRACE("termios t:");
172 	trace_termios(&tty->t);
173 
174 	TRACE("\tiactivity: %d, ibusy: %d\n", tty->iactivity, tty->ibusy);
175 
176 	TRACE("str istr:\n");
177 	trace_str(&tty->istr);
178 	TRACE("str rstr:\n");
179 	trace_str(&tty->rstr);
180 	TRACE("str ostr:\n");
181 	trace_str(&tty->ostr);
182 
183 	TRACE("winsize wsize:\n");
184 	trace_winsize(&tty->wsize);
185 
186 	TRACE("\tservice: 0x%08x\n", tty->service);
187 }
188 
189 
190 #endif /* __BEOS__ */