xref: /haiku/src/add-ons/kernel/drivers/ports/pc_serial/Tracing.cpp (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
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, 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_ddomain(struct ddomain *dd)
103 {
104 	TRACE("struct ddomain:\n"
105 		"\tddrover: 0x%08x\n"
106 		"\tbg: %d, locked: %d\n", dd->r, dd->bg, dd->locked);
107 }
108 
109 
110 void
111 trace_termios(struct termios *tios)
112 {
113 	TRACE("struct termios:\n"
114 		"\tc_iflag:  0x%08x\n"
115 		"\tc_oflag:  0x%08x\n"
116 		"\tc_cflag:  0x%08x\n"
117 		"\tc_lflag:  0x%08x\n"
118 		"\tc_line:   0x%08x\n"
119 //		"\tc_ixxxxx: 0x%08x\n"
120 //		"\tc_oxxxxx: 0x%08x\n"
121 		"\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",
122 		tios->c_iflag, tios->c_oflag, tios->c_cflag, tios->c_lflag,
123 		tios->c_line,
124 //		tios->c_ixxxxx, tios->c_oxxxxx,
125 		tios->c_cc[0], tios->c_cc[1], tios->c_cc[2], tios->c_cc[3],
126 		tios->c_cc[4], tios->c_cc[5], tios->c_cc[6], tios->c_cc[7],
127 		tios->c_cc[8], tios->c_cc[9], tios->c_cc[10]);
128 }
129 
130 
131 void
132 trace_str(struct str *str)
133 {
134 	TRACE("struct str:\n"
135 		"\tbuffer:    0x%08x\n"
136 		"\tbufsize:   %d\n"
137 		"\tcount:     %d\n"
138 		"\ttail:      %d\n"
139 		"\tallocated: %d\n",
140 		str->buffer, str->bufsize, str->count, str->tail, str->allocated);
141 }
142 
143 
144 void
145 trace_winsize(struct winsize *ws)
146 {
147 	TRACE("struct winsize:\n"
148 		"\tws_row:    %d\n"
149 		"\tws_col:    %d\n"
150 		"\tws_xpixel: %d\n"
151 		"\tws_ypixel: %d\n",
152 		ws->ws_row, ws->ws_col, ws->ws_xpixel, ws->ws_ypixel);
153 }
154 
155 
156 void
157 trace_tty(struct tty *tty)
158 {
159 	TRACE("struct tty:\n"
160 		"\tnopen: %d, flags: 0x%08x,\n", tty->nopen, tty->flags);
161 
162 	TRACE("ddomain dd:\n");
163 	trace_ddomain(&tty->dd);
164 	TRACE("ddomain ddi:\n");
165 	trace_ddomain(&tty->ddi);
166 
167 	TRACE("\tpgid: %08x\n", tty->pgid);
168 	TRACE("termios t:");
169 	trace_termios(&tty->t);
170 
171 	TRACE("\tiactivity: %d, ibusy: %d\n", tty->iactivity, tty->ibusy);
172 
173 	TRACE("str istr:\n");
174 	trace_str(&tty->istr);
175 	TRACE("str rstr:\n");
176 	trace_str(&tty->rstr);
177 	TRACE("str ostr:\n");
178 	trace_str(&tty->ostr);
179 
180 	TRACE("winsize wsize:\n");
181 	trace_winsize(&tty->wsize);
182 
183 	TRACE("\tservice: 0x%08x\n", tty->service);
184 }
185