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