xref: /openbmc/linux/drivers/acpi/acpica/utdebug.c (revision 0a73d21e)
1 /******************************************************************************
2  *
3  * Module Name: utdebug - Debug print/trace routines
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2018, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #define EXPORT_ACPI_INTERFACES
45 
46 #include <acpi/acpi.h>
47 #include "accommon.h"
48 #include "acinterp.h"
49 
50 #define _COMPONENT          ACPI_UTILITIES
51 ACPI_MODULE_NAME("utdebug")
52 
53 #ifdef ACPI_DEBUG_OUTPUT
54 static acpi_thread_id acpi_gbl_previous_thread_id = (acpi_thread_id) 0xFFFFFFFF;
55 static const char *acpi_gbl_function_entry_prefix = "----Entry";
56 static const char *acpi_gbl_function_exit_prefix = "----Exit-";
57 
58 /*******************************************************************************
59  *
60  * FUNCTION:    acpi_ut_init_stack_ptr_trace
61  *
62  * PARAMETERS:  None
63  *
64  * RETURN:      None
65  *
66  * DESCRIPTION: Save the current CPU stack pointer at subsystem startup
67  *
68  ******************************************************************************/
69 
70 void acpi_ut_init_stack_ptr_trace(void)
71 {
72 	acpi_size current_sp;
73 
74 	acpi_gbl_entry_stack_pointer = &current_sp;
75 }
76 
77 /*******************************************************************************
78  *
79  * FUNCTION:    acpi_ut_track_stack_ptr
80  *
81  * PARAMETERS:  None
82  *
83  * RETURN:      None
84  *
85  * DESCRIPTION: Save the current CPU stack pointer
86  *
87  ******************************************************************************/
88 
89 void acpi_ut_track_stack_ptr(void)
90 {
91 	acpi_size current_sp;
92 
93 	if (&current_sp < acpi_gbl_lowest_stack_pointer) {
94 		acpi_gbl_lowest_stack_pointer = &current_sp;
95 	}
96 
97 	if (acpi_gbl_nesting_level > acpi_gbl_deepest_nesting) {
98 		acpi_gbl_deepest_nesting = acpi_gbl_nesting_level;
99 	}
100 }
101 
102 /*******************************************************************************
103  *
104  * FUNCTION:    acpi_ut_trim_function_name
105  *
106  * PARAMETERS:  function_name       - Ascii string containing a procedure name
107  *
108  * RETURN:      Updated pointer to the function name
109  *
110  * DESCRIPTION: Remove the "Acpi" prefix from the function name, if present.
111  *              This allows compiler macros such as __func__ to be used
112  *              with no change to the debug output.
113  *
114  ******************************************************************************/
115 
116 static const char *acpi_ut_trim_function_name(const char *function_name)
117 {
118 
119 	/* All Function names are longer than 4 chars, check is safe */
120 
121 	if (*(ACPI_CAST_PTR(u32, function_name)) == ACPI_PREFIX_MIXED) {
122 
123 		/* This is the case where the original source has not been modified */
124 
125 		return (function_name + 4);
126 	}
127 
128 	if (*(ACPI_CAST_PTR(u32, function_name)) == ACPI_PREFIX_LOWER) {
129 
130 		/* This is the case where the source has been 'linuxized' */
131 
132 		return (function_name + 5);
133 	}
134 
135 	return (function_name);
136 }
137 
138 /*******************************************************************************
139  *
140  * FUNCTION:    acpi_debug_print
141  *
142  * PARAMETERS:  requested_debug_level - Requested debug print level
143  *              line_number         - Caller's line number (for error output)
144  *              function_name       - Caller's procedure name
145  *              module_name         - Caller's module name
146  *              component_id        - Caller's component ID
147  *              format              - Printf format field
148  *              ...                 - Optional printf arguments
149  *
150  * RETURN:      None
151  *
152  * DESCRIPTION: Print error message with prefix consisting of the module name,
153  *              line number, and component ID.
154  *
155  ******************************************************************************/
156 
157 void ACPI_INTERNAL_VAR_XFACE
158 acpi_debug_print(u32 requested_debug_level,
159 		 u32 line_number,
160 		 const char *function_name,
161 		 const char *module_name,
162 		 u32 component_id, const char *format, ...)
163 {
164 	acpi_thread_id thread_id;
165 	va_list args;
166 #ifdef ACPI_APPLICATION
167 	int fill_count;
168 #endif
169 
170 	/* Check if debug output enabled */
171 
172 	if (!ACPI_IS_DEBUG_ENABLED(requested_debug_level, component_id)) {
173 		return;
174 	}
175 
176 	/*
177 	 * Thread tracking and context switch notification
178 	 */
179 	thread_id = acpi_os_get_thread_id();
180 	if (thread_id != acpi_gbl_previous_thread_id) {
181 		if (ACPI_LV_THREADS & acpi_dbg_level) {
182 			acpi_os_printf
183 			    ("\n**** Context Switch from TID %u to TID %u ****\n\n",
184 			     (u32)acpi_gbl_previous_thread_id, (u32)thread_id);
185 		}
186 
187 		acpi_gbl_previous_thread_id = thread_id;
188 		acpi_gbl_nesting_level = 0;
189 	}
190 
191 	/*
192 	 * Display the module name, current line number, thread ID (if requested),
193 	 * current procedure nesting level, and the current procedure name
194 	 */
195 	acpi_os_printf("%9s-%04ld ", module_name, line_number);
196 
197 #ifdef ACPI_APPLICATION
198 	/*
199 	 * For acpi_exec/iASL only, emit the thread ID and nesting level.
200 	 * Note: nesting level is really only useful during a single-thread
201 	 * execution. Otherwise, multiple threads will keep resetting the
202 	 * level.
203 	 */
204 	if (ACPI_LV_THREADS & acpi_dbg_level) {
205 		acpi_os_printf("[%u] ", (u32)thread_id);
206 	}
207 
208 	fill_count = 48 - acpi_gbl_nesting_level -
209 	    strlen(acpi_ut_trim_function_name(function_name));
210 	if (fill_count < 0) {
211 		fill_count = 0;
212 	}
213 
214 	acpi_os_printf("[%02ld] %*s",
215 		       acpi_gbl_nesting_level, acpi_gbl_nesting_level + 1, " ");
216 	acpi_os_printf("%s%*s: ",
217 		       acpi_ut_trim_function_name(function_name), fill_count,
218 		       " ");
219 
220 #else
221 	acpi_os_printf("%-22.22s: ", acpi_ut_trim_function_name(function_name));
222 #endif
223 
224 	va_start(args, format);
225 	acpi_os_vprintf(format, args);
226 	va_end(args);
227 }
228 
229 ACPI_EXPORT_SYMBOL(acpi_debug_print)
230 
231 /*******************************************************************************
232  *
233  * FUNCTION:    acpi_debug_print_raw
234  *
235  * PARAMETERS:  requested_debug_level - Requested debug print level
236  *              line_number         - Caller's line number
237  *              function_name       - Caller's procedure name
238  *              module_name         - Caller's module name
239  *              component_id        - Caller's component ID
240  *              format              - Printf format field
241  *              ...                 - Optional printf arguments
242  *
243  * RETURN:      None
244  *
245  * DESCRIPTION: Print message with no headers. Has same interface as
246  *              debug_print so that the same macros can be used.
247  *
248  ******************************************************************************/
249 void ACPI_INTERNAL_VAR_XFACE
250 acpi_debug_print_raw(u32 requested_debug_level,
251 		     u32 line_number,
252 		     const char *function_name,
253 		     const char *module_name,
254 		     u32 component_id, const char *format, ...)
255 {
256 	va_list args;
257 
258 	/* Check if debug output enabled */
259 
260 	if (!ACPI_IS_DEBUG_ENABLED(requested_debug_level, component_id)) {
261 		return;
262 	}
263 
264 	va_start(args, format);
265 	acpi_os_vprintf(format, args);
266 	va_end(args);
267 }
268 
269 ACPI_EXPORT_SYMBOL(acpi_debug_print_raw)
270 
271 /*******************************************************************************
272  *
273  * FUNCTION:    acpi_ut_trace
274  *
275  * PARAMETERS:  line_number         - Caller's line number
276  *              function_name       - Caller's procedure name
277  *              module_name         - Caller's module name
278  *              component_id        - Caller's component ID
279  *
280  * RETURN:      None
281  *
282  * DESCRIPTION: Function entry trace. Prints only if TRACE_FUNCTIONS bit is
283  *              set in debug_level
284  *
285  ******************************************************************************/
286 void
287 acpi_ut_trace(u32 line_number,
288 	      const char *function_name,
289 	      const char *module_name, u32 component_id)
290 {
291 
292 	acpi_gbl_nesting_level++;
293 	acpi_ut_track_stack_ptr();
294 
295 	/* Check if enabled up-front for performance */
296 
297 	if (ACPI_IS_DEBUG_ENABLED(ACPI_LV_FUNCTIONS, component_id)) {
298 		acpi_debug_print(ACPI_LV_FUNCTIONS,
299 				 line_number, function_name, module_name,
300 				 component_id, "%s\n",
301 				 acpi_gbl_function_entry_prefix);
302 	}
303 }
304 
305 ACPI_EXPORT_SYMBOL(acpi_ut_trace)
306 
307 /*******************************************************************************
308  *
309  * FUNCTION:    acpi_ut_trace_ptr
310  *
311  * PARAMETERS:  line_number         - Caller's line number
312  *              function_name       - Caller's procedure name
313  *              module_name         - Caller's module name
314  *              component_id        - Caller's component ID
315  *              pointer             - Pointer to display
316  *
317  * RETURN:      None
318  *
319  * DESCRIPTION: Function entry trace. Prints only if TRACE_FUNCTIONS bit is
320  *              set in debug_level
321  *
322  ******************************************************************************/
323 void
324 acpi_ut_trace_ptr(u32 line_number,
325 		  const char *function_name,
326 		  const char *module_name,
327 		  u32 component_id, const void *pointer)
328 {
329 
330 	acpi_gbl_nesting_level++;
331 	acpi_ut_track_stack_ptr();
332 
333 	/* Check if enabled up-front for performance */
334 
335 	if (ACPI_IS_DEBUG_ENABLED(ACPI_LV_FUNCTIONS, component_id)) {
336 		acpi_debug_print(ACPI_LV_FUNCTIONS,
337 				 line_number, function_name, module_name,
338 				 component_id, "%s %p\n",
339 				 acpi_gbl_function_entry_prefix, pointer);
340 	}
341 }
342 
343 /*******************************************************************************
344  *
345  * FUNCTION:    acpi_ut_trace_str
346  *
347  * PARAMETERS:  line_number         - Caller's line number
348  *              function_name       - Caller's procedure name
349  *              module_name         - Caller's module name
350  *              component_id        - Caller's component ID
351  *              string              - Additional string to display
352  *
353  * RETURN:      None
354  *
355  * DESCRIPTION: Function entry trace. Prints only if TRACE_FUNCTIONS bit is
356  *              set in debug_level
357  *
358  ******************************************************************************/
359 
360 void
361 acpi_ut_trace_str(u32 line_number,
362 		  const char *function_name,
363 		  const char *module_name, u32 component_id, const char *string)
364 {
365 
366 	acpi_gbl_nesting_level++;
367 	acpi_ut_track_stack_ptr();
368 
369 	/* Check if enabled up-front for performance */
370 
371 	if (ACPI_IS_DEBUG_ENABLED(ACPI_LV_FUNCTIONS, component_id)) {
372 		acpi_debug_print(ACPI_LV_FUNCTIONS,
373 				 line_number, function_name, module_name,
374 				 component_id, "%s %s\n",
375 				 acpi_gbl_function_entry_prefix, string);
376 	}
377 }
378 
379 /*******************************************************************************
380  *
381  * FUNCTION:    acpi_ut_trace_u32
382  *
383  * PARAMETERS:  line_number         - Caller's line number
384  *              function_name       - Caller's procedure name
385  *              module_name         - Caller's module name
386  *              component_id        - Caller's component ID
387  *              integer             - Integer to display
388  *
389  * RETURN:      None
390  *
391  * DESCRIPTION: Function entry trace. Prints only if TRACE_FUNCTIONS bit is
392  *              set in debug_level
393  *
394  ******************************************************************************/
395 
396 void
397 acpi_ut_trace_u32(u32 line_number,
398 		  const char *function_name,
399 		  const char *module_name, u32 component_id, u32 integer)
400 {
401 
402 	acpi_gbl_nesting_level++;
403 	acpi_ut_track_stack_ptr();
404 
405 	/* Check if enabled up-front for performance */
406 
407 	if (ACPI_IS_DEBUG_ENABLED(ACPI_LV_FUNCTIONS, component_id)) {
408 		acpi_debug_print(ACPI_LV_FUNCTIONS,
409 				 line_number, function_name, module_name,
410 				 component_id, "%s %08X\n",
411 				 acpi_gbl_function_entry_prefix, integer);
412 	}
413 }
414 
415 /*******************************************************************************
416  *
417  * FUNCTION:    acpi_ut_exit
418  *
419  * PARAMETERS:  line_number         - Caller's line number
420  *              function_name       - Caller's procedure name
421  *              module_name         - Caller's module name
422  *              component_id        - Caller's component ID
423  *
424  * RETURN:      None
425  *
426  * DESCRIPTION: Function exit trace. Prints only if TRACE_FUNCTIONS bit is
427  *              set in debug_level
428  *
429  ******************************************************************************/
430 
431 void
432 acpi_ut_exit(u32 line_number,
433 	     const char *function_name,
434 	     const char *module_name, u32 component_id)
435 {
436 
437 	/* Check if enabled up-front for performance */
438 
439 	if (ACPI_IS_DEBUG_ENABLED(ACPI_LV_FUNCTIONS, component_id)) {
440 		acpi_debug_print(ACPI_LV_FUNCTIONS,
441 				 line_number, function_name, module_name,
442 				 component_id, "%s\n",
443 				 acpi_gbl_function_exit_prefix);
444 	}
445 
446 	if (acpi_gbl_nesting_level) {
447 		acpi_gbl_nesting_level--;
448 	}
449 }
450 
451 ACPI_EXPORT_SYMBOL(acpi_ut_exit)
452 
453 /*******************************************************************************
454  *
455  * FUNCTION:    acpi_ut_status_exit
456  *
457  * PARAMETERS:  line_number         - Caller's line number
458  *              function_name       - Caller's procedure name
459  *              module_name         - Caller's module name
460  *              component_id        - Caller's component ID
461  *              status              - Exit status code
462  *
463  * RETURN:      None
464  *
465  * DESCRIPTION: Function exit trace. Prints only if TRACE_FUNCTIONS bit is
466  *              set in debug_level. Prints exit status also.
467  *
468  ******************************************************************************/
469 void
470 acpi_ut_status_exit(u32 line_number,
471 		    const char *function_name,
472 		    const char *module_name,
473 		    u32 component_id, acpi_status status)
474 {
475 
476 	/* Check if enabled up-front for performance */
477 
478 	if (ACPI_IS_DEBUG_ENABLED(ACPI_LV_FUNCTIONS, component_id)) {
479 		if (ACPI_SUCCESS(status)) {
480 			acpi_debug_print(ACPI_LV_FUNCTIONS,
481 					 line_number, function_name,
482 					 module_name, component_id, "%s %s\n",
483 					 acpi_gbl_function_exit_prefix,
484 					 acpi_format_exception(status));
485 		} else {
486 			acpi_debug_print(ACPI_LV_FUNCTIONS,
487 					 line_number, function_name,
488 					 module_name, component_id,
489 					 "%s ****Exception****: %s\n",
490 					 acpi_gbl_function_exit_prefix,
491 					 acpi_format_exception(status));
492 		}
493 	}
494 
495 	if (acpi_gbl_nesting_level) {
496 		acpi_gbl_nesting_level--;
497 	}
498 }
499 
500 ACPI_EXPORT_SYMBOL(acpi_ut_status_exit)
501 
502 /*******************************************************************************
503  *
504  * FUNCTION:    acpi_ut_value_exit
505  *
506  * PARAMETERS:  line_number         - Caller's line number
507  *              function_name       - Caller's procedure name
508  *              module_name         - Caller's module name
509  *              component_id        - Caller's component ID
510  *              value               - Value to be printed with exit msg
511  *
512  * RETURN:      None
513  *
514  * DESCRIPTION: Function exit trace. Prints only if TRACE_FUNCTIONS bit is
515  *              set in debug_level. Prints exit value also.
516  *
517  ******************************************************************************/
518 void
519 acpi_ut_value_exit(u32 line_number,
520 		   const char *function_name,
521 		   const char *module_name, u32 component_id, u64 value)
522 {
523 
524 	/* Check if enabled up-front for performance */
525 
526 	if (ACPI_IS_DEBUG_ENABLED(ACPI_LV_FUNCTIONS, component_id)) {
527 		acpi_debug_print(ACPI_LV_FUNCTIONS,
528 				 line_number, function_name, module_name,
529 				 component_id, "%s %8.8X%8.8X\n",
530 				 acpi_gbl_function_exit_prefix,
531 				 ACPI_FORMAT_UINT64(value));
532 	}
533 
534 	if (acpi_gbl_nesting_level) {
535 		acpi_gbl_nesting_level--;
536 	}
537 }
538 
539 ACPI_EXPORT_SYMBOL(acpi_ut_value_exit)
540 
541 /*******************************************************************************
542  *
543  * FUNCTION:    acpi_ut_ptr_exit
544  *
545  * PARAMETERS:  line_number         - Caller's line number
546  *              function_name       - Caller's procedure name
547  *              module_name         - Caller's module name
548  *              component_id        - Caller's component ID
549  *              ptr                 - Pointer to display
550  *
551  * RETURN:      None
552  *
553  * DESCRIPTION: Function exit trace. Prints only if TRACE_FUNCTIONS bit is
554  *              set in debug_level. Prints exit value also.
555  *
556  ******************************************************************************/
557 void
558 acpi_ut_ptr_exit(u32 line_number,
559 		 const char *function_name,
560 		 const char *module_name, u32 component_id, u8 *ptr)
561 {
562 
563 	/* Check if enabled up-front for performance */
564 
565 	if (ACPI_IS_DEBUG_ENABLED(ACPI_LV_FUNCTIONS, component_id)) {
566 		acpi_debug_print(ACPI_LV_FUNCTIONS,
567 				 line_number, function_name, module_name,
568 				 component_id, "%s %p\n",
569 				 acpi_gbl_function_exit_prefix, ptr);
570 	}
571 
572 	if (acpi_gbl_nesting_level) {
573 		acpi_gbl_nesting_level--;
574 	}
575 }
576 
577 /*******************************************************************************
578  *
579  * FUNCTION:    acpi_ut_str_exit
580  *
581  * PARAMETERS:  line_number         - Caller's line number
582  *              function_name       - Caller's procedure name
583  *              module_name         - Caller's module name
584  *              component_id        - Caller's component ID
585  *              string              - String to display
586  *
587  * RETURN:      None
588  *
589  * DESCRIPTION: Function exit trace. Prints only if TRACE_FUNCTIONS bit is
590  *              set in debug_level. Prints exit value also.
591  *
592  ******************************************************************************/
593 
594 void
595 acpi_ut_str_exit(u32 line_number,
596 		 const char *function_name,
597 		 const char *module_name, u32 component_id, const char *string)
598 {
599 
600 	/* Check if enabled up-front for performance */
601 
602 	if (ACPI_IS_DEBUG_ENABLED(ACPI_LV_FUNCTIONS, component_id)) {
603 		acpi_debug_print(ACPI_LV_FUNCTIONS,
604 				 line_number, function_name, module_name,
605 				 component_id, "%s %s\n",
606 				 acpi_gbl_function_exit_prefix, string);
607 	}
608 
609 	if (acpi_gbl_nesting_level) {
610 		acpi_gbl_nesting_level--;
611 	}
612 }
613 
614 /*******************************************************************************
615  *
616  * FUNCTION:    acpi_trace_point
617  *
618  * PARAMETERS:  type                - Trace event type
619  *              begin               - TRUE if before execution
620  *              aml                 - Executed AML address
621  *              pathname            - Object path
622  *              pointer             - Pointer to the related object
623  *
624  * RETURN:      None
625  *
626  * DESCRIPTION: Interpreter execution trace.
627  *
628  ******************************************************************************/
629 
630 void
631 acpi_trace_point(acpi_trace_event_type type, u8 begin, u8 *aml, char *pathname)
632 {
633 
634 	ACPI_FUNCTION_ENTRY();
635 
636 	acpi_ex_trace_point(type, begin, aml, pathname);
637 
638 #ifdef ACPI_USE_SYSTEM_TRACER
639 	acpi_os_trace_point(type, begin, aml, pathname);
640 #endif
641 }
642 
643 ACPI_EXPORT_SYMBOL(acpi_trace_point)
644 
645 #endif
646