auxiliary/os,auxiliary/util: Fix the `‘noreturn’ function does return` warning.
[mesa.git] / src / gallium / auxiliary / util / u_debug.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright (c) 2008 VMware, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29
30 #include "pipe/p_config.h"
31
32 #include "pipe/p_compiler.h"
33 #include "util/u_debug.h"
34 #include "pipe/p_format.h"
35 #include "pipe/p_state.h"
36 #include "util/u_inlines.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_string.h"
40 #include "util/u_math.h"
41 #include "util/u_tile.h"
42 #include "util/u_prim.h"
43 #include "util/u_surface.h"
44
45 #include <stdio.h>
46 #include <limits.h> /* CHAR_BIT */
47 #include <ctype.h> /* isalnum */
48
49 void _debug_vprintf(const char *format, va_list ap)
50 {
51 static char buf[4096] = {'\0'};
52 #if defined(PIPE_OS_WINDOWS) || defined(PIPE_SUBSYSTEM_EMBEDDED)
53 /* We buffer until we find a newline. */
54 size_t len = strlen(buf);
55 int ret = util_vsnprintf(buf + len, sizeof(buf) - len, format, ap);
56 if(ret > (int)(sizeof(buf) - len - 1) || util_strchr(buf + len, '\n')) {
57 os_log_message(buf);
58 buf[0] = '\0';
59 }
60 #else
61 util_vsnprintf(buf, sizeof(buf), format, ap);
62 os_log_message(buf);
63 #endif
64 }
65
66
67 #ifdef DEBUG
68 void debug_print_blob( const char *name,
69 const void *blob,
70 unsigned size )
71 {
72 const unsigned *ublob = (const unsigned *)blob;
73 unsigned i;
74
75 debug_printf("%s (%d dwords%s)\n", name, size/4,
76 size%4 ? "... plus a few bytes" : "");
77
78 for (i = 0; i < size/4; i++) {
79 debug_printf("%d:\t%08x\n", i, ublob[i]);
80 }
81 }
82 #endif
83
84
85 static boolean
86 debug_get_option_should_print(void)
87 {
88 static boolean first = TRUE;
89 static boolean value = FALSE;
90
91 if (!first)
92 return value;
93
94 /* Oh hey this will call into this function,
95 * but its cool since we set first to false
96 */
97 first = FALSE;
98 value = debug_get_bool_option("GALLIUM_PRINT_OPTIONS", FALSE);
99 /* XXX should we print this option? Currently it wont */
100 return value;
101 }
102
103 const char *
104 debug_get_option(const char *name, const char *dfault)
105 {
106 const char *result;
107
108 result = os_get_option(name);
109 if(!result)
110 result = dfault;
111
112 if (debug_get_option_should_print())
113 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? result : "(null)");
114
115 return result;
116 }
117
118 boolean
119 debug_get_bool_option(const char *name, boolean dfault)
120 {
121 const char *str = os_get_option(name);
122 boolean result;
123
124 if(str == NULL)
125 result = dfault;
126 else if(!util_strcmp(str, "n"))
127 result = FALSE;
128 else if(!util_strcmp(str, "no"))
129 result = FALSE;
130 else if(!util_strcmp(str, "0"))
131 result = FALSE;
132 else if(!util_strcmp(str, "f"))
133 result = FALSE;
134 else if(!util_strcmp(str, "F"))
135 result = FALSE;
136 else if(!util_strcmp(str, "false"))
137 result = FALSE;
138 else if(!util_strcmp(str, "FALSE"))
139 result = FALSE;
140 else
141 result = TRUE;
142
143 if (debug_get_option_should_print())
144 debug_printf("%s: %s = %s\n", __FUNCTION__, name, result ? "TRUE" : "FALSE");
145
146 return result;
147 }
148
149
150 long
151 debug_get_num_option(const char *name, long dfault)
152 {
153 long result;
154 const char *str;
155
156 str = os_get_option(name);
157 if(!str)
158 result = dfault;
159 else {
160 long sign;
161 char c;
162 c = *str++;
163 if(c == '-') {
164 sign = -1;
165 c = *str++;
166 }
167 else {
168 sign = 1;
169 }
170 result = 0;
171 while('0' <= c && c <= '9') {
172 result = result*10 + (c - '0');
173 c = *str++;
174 }
175 result *= sign;
176 }
177
178 if (debug_get_option_should_print())
179 debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
180
181 return result;
182 }
183
184 static boolean str_has_option(const char *str, const char *name)
185 {
186 /* Empty string. */
187 if (!*str) {
188 return FALSE;
189 }
190
191 /* OPTION=all */
192 if (!util_strcmp(str, "all")) {
193 return TRUE;
194 }
195
196 /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
197 {
198 const char *start = str;
199 unsigned name_len = strlen(name);
200
201 /* 'start' is the beginning of the currently-parsed word,
202 * we increment 'str' each iteration.
203 * if we find either the end of string or a non-alphanumeric character,
204 * we compare 'start' up to 'str-1' with 'name'. */
205
206 while (1) {
207 if (!*str || !(isalnum(*str) || *str == '_')) {
208 if (str-start == name_len &&
209 !memcmp(start, name, name_len)) {
210 return TRUE;
211 }
212
213 if (!*str) {
214 return FALSE;
215 }
216
217 start = str+1;
218 }
219
220 str++;
221 }
222 }
223
224 return FALSE;
225 }
226
227 unsigned long
228 debug_get_flags_option(const char *name,
229 const struct debug_named_value *flags,
230 unsigned long dfault)
231 {
232 unsigned long result;
233 const char *str;
234 const struct debug_named_value *orig = flags;
235 unsigned namealign = 0;
236
237 str = os_get_option(name);
238 if(!str)
239 result = dfault;
240 else if (!util_strcmp(str, "help")) {
241 result = dfault;
242 _debug_printf("%s: help for %s:\n", __FUNCTION__, name);
243 for (; flags->name; ++flags)
244 namealign = MAX2(namealign, strlen(flags->name));
245 for (flags = orig; flags->name; ++flags)
246 _debug_printf("| %*s [0x%0*lx]%s%s\n", namealign, flags->name,
247 (int)sizeof(unsigned long)*CHAR_BIT/4, flags->value,
248 flags->desc ? " " : "", flags->desc ? flags->desc : "");
249 }
250 else {
251 result = 0;
252 while( flags->name ) {
253 if (str_has_option(str, flags->name))
254 result |= flags->value;
255 ++flags;
256 }
257 }
258
259 if (debug_get_option_should_print()) {
260 if (str) {
261 debug_printf("%s: %s = 0x%lx (%s)\n", __FUNCTION__, name, result, str);
262 } else {
263 debug_printf("%s: %s = 0x%lx\n", __FUNCTION__, name, result);
264 }
265 }
266
267 return result;
268 }
269
270
271 void _debug_assert_fail(const char *expr,
272 const char *file,
273 unsigned line,
274 const char *function)
275 {
276 _debug_printf("%s:%u:%s: Assertion `%s' failed.\n", file, line, function, expr);
277 os_abort();
278 }
279
280
281 const char *
282 debug_dump_enum(const struct debug_named_value *names,
283 unsigned long value)
284 {
285 static char rest[64];
286
287 while(names->name) {
288 if(names->value == value)
289 return names->name;
290 ++names;
291 }
292
293 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
294 return rest;
295 }
296
297
298 const char *
299 debug_dump_enum_noprefix(const struct debug_named_value *names,
300 const char *prefix,
301 unsigned long value)
302 {
303 static char rest[64];
304
305 while(names->name) {
306 if(names->value == value) {
307 const char *name = names->name;
308 while (*name == *prefix) {
309 name++;
310 prefix++;
311 }
312 return name;
313 }
314 ++names;
315 }
316
317
318
319 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
320 return rest;
321 }
322
323
324 const char *
325 debug_dump_flags(const struct debug_named_value *names,
326 unsigned long value)
327 {
328 static char output[4096];
329 static char rest[256];
330 int first = 1;
331
332 output[0] = '\0';
333
334 while(names->name) {
335 if((names->value & value) == names->value) {
336 if (!first)
337 util_strncat(output, "|", sizeof(output));
338 else
339 first = 0;
340 util_strncat(output, names->name, sizeof(output) - 1);
341 output[sizeof(output) - 1] = '\0';
342 value &= ~names->value;
343 }
344 ++names;
345 }
346
347 if (value) {
348 if (!first)
349 util_strncat(output, "|", sizeof(output));
350 else
351 first = 0;
352
353 util_snprintf(rest, sizeof(rest), "0x%08lx", value);
354 util_strncat(output, rest, sizeof(output) - 1);
355 output[sizeof(output) - 1] = '\0';
356 }
357
358 if(first)
359 return "0";
360
361 return output;
362 }
363
364
365 #ifdef DEBUG
366 void debug_print_format(const char *msg, unsigned fmt )
367 {
368 debug_printf("%s: %s\n", msg, util_format_name(fmt));
369 }
370 #endif
371
372
373
374 static const struct debug_named_value pipe_prim_names[] = {
375 #ifdef DEBUG
376 DEBUG_NAMED_VALUE(PIPE_PRIM_POINTS),
377 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES),
378 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_LOOP),
379 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP),
380 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES),
381 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP),
382 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_FAN),
383 DEBUG_NAMED_VALUE(PIPE_PRIM_QUADS),
384 DEBUG_NAMED_VALUE(PIPE_PRIM_QUAD_STRIP),
385 DEBUG_NAMED_VALUE(PIPE_PRIM_POLYGON),
386 DEBUG_NAMED_VALUE(PIPE_PRIM_LINES_ADJACENCY),
387 DEBUG_NAMED_VALUE(PIPE_PRIM_LINE_STRIP_ADJACENCY),
388 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLES_ADJACENCY),
389 DEBUG_NAMED_VALUE(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY),
390 #endif
391 DEBUG_NAMED_VALUE_END
392 };
393
394
395 const char *u_prim_name( unsigned prim )
396 {
397 return debug_dump_enum(pipe_prim_names, prim);
398 }
399
400
401
402 #ifdef DEBUG
403 int fl_indent = 0;
404 const char* fl_function[1024];
405
406 int debug_funclog_enter(const char* f, const int line, const char* file)
407 {
408 int i;
409
410 for (i = 0; i < fl_indent; i++)
411 debug_printf(" ");
412 debug_printf("%s\n", f);
413
414 assert(fl_indent < 1023);
415 fl_function[fl_indent++] = f;
416
417 return 0;
418 }
419
420 void debug_funclog_exit(const char* f, const int line, const char* file)
421 {
422 --fl_indent;
423 assert(fl_indent >= 0);
424 assert(fl_function[fl_indent] == f);
425 }
426
427 void debug_funclog_enter_exit(const char* f, const int line, const char* file)
428 {
429 int i;
430 for (i = 0; i < fl_indent; i++)
431 debug_printf(" ");
432 debug_printf("%s\n", f);
433 }
434 #endif
435
436
437
438 #ifdef DEBUG
439 /**
440 * Dump an image to .ppm file.
441 * \param format PIPE_FORMAT_x
442 * \param cpp bytes per pixel
443 * \param width width in pixels
444 * \param height height in pixels
445 * \param stride row stride in bytes
446 */
447 void debug_dump_image(const char *prefix,
448 enum pipe_format format, unsigned cpp,
449 unsigned width, unsigned height,
450 unsigned stride,
451 const void *data)
452 {
453 /* write a ppm file */
454 char filename[256];
455 unsigned char *rgb8;
456 FILE *f;
457
458 util_snprintf(filename, sizeof(filename), "%s.ppm", prefix);
459
460 rgb8 = MALLOC(height * width * 3);
461 if (!rgb8) {
462 return;
463 }
464
465 util_format_translate(
466 PIPE_FORMAT_R8G8B8_UNORM,
467 rgb8, width * 3,
468 0, 0,
469 format,
470 data, stride,
471 0, 0, width, height);
472
473 /* Must be opened in binary mode or DOS line ending causes data
474 * to be read with one byte offset.
475 */
476 f = fopen(filename, "wb");
477 if (f) {
478 fprintf(f, "P6\n");
479 fprintf(f, "# ppm-file created by gallium\n");
480 fprintf(f, "%i %i\n", width, height);
481 fprintf(f, "255\n");
482 fwrite(rgb8, 1, height * width * 3, f);
483 fclose(f);
484 }
485 else {
486 fprintf(stderr, "Can't open %s for writing\n", filename);
487 }
488
489 FREE(rgb8);
490 }
491
492 /* FIXME: dump resources, not surfaces... */
493 void debug_dump_surface(struct pipe_context *pipe,
494 const char *prefix,
495 struct pipe_surface *surface)
496 {
497 struct pipe_resource *texture;
498 struct pipe_transfer *transfer;
499 void *data;
500
501 if (!surface)
502 return;
503
504 /* XXX: this doesn't necessarily work, as the driver may be using
505 * temporary storage for the surface which hasn't been propagated
506 * back into the texture. Need to nail down the semantics of views
507 * and transfers a bit better before we can say if extra work needs
508 * to be done here:
509 */
510 texture = surface->texture;
511
512 data = pipe_transfer_map(pipe, texture, surface->u.tex.level,
513 surface->u.tex.first_layer,
514 PIPE_TRANSFER_READ,
515 0, 0, surface->width, surface->height, &transfer);
516 if(!data)
517 return;
518
519 debug_dump_image(prefix,
520 texture->format,
521 util_format_get_blocksize(texture->format),
522 util_format_get_nblocksx(texture->format, surface->width),
523 util_format_get_nblocksy(texture->format, surface->height),
524 transfer->stride,
525 data);
526
527 pipe->transfer_unmap(pipe, transfer);
528 }
529
530
531 void debug_dump_texture(struct pipe_context *pipe,
532 const char *prefix,
533 struct pipe_resource *texture)
534 {
535 struct pipe_surface *surface, surf_tmpl;
536
537 if (!texture)
538 return;
539
540 /* XXX for now, just dump image for layer=0, level=0 */
541 u_surface_default_template(&surf_tmpl, texture);
542 surface = pipe->create_surface(pipe, texture, &surf_tmpl);
543 if (surface) {
544 debug_dump_surface(pipe, prefix, surface);
545 pipe->surface_destroy(pipe, surface);
546 }
547 }
548
549
550 #pragma pack(push,2)
551 struct bmp_file_header {
552 uint16_t bfType;
553 uint32_t bfSize;
554 uint16_t bfReserved1;
555 uint16_t bfReserved2;
556 uint32_t bfOffBits;
557 };
558 #pragma pack(pop)
559
560 struct bmp_info_header {
561 uint32_t biSize;
562 int32_t biWidth;
563 int32_t biHeight;
564 uint16_t biPlanes;
565 uint16_t biBitCount;
566 uint32_t biCompression;
567 uint32_t biSizeImage;
568 int32_t biXPelsPerMeter;
569 int32_t biYPelsPerMeter;
570 uint32_t biClrUsed;
571 uint32_t biClrImportant;
572 };
573
574 struct bmp_rgb_quad {
575 uint8_t rgbBlue;
576 uint8_t rgbGreen;
577 uint8_t rgbRed;
578 uint8_t rgbAlpha;
579 };
580
581 void
582 debug_dump_surface_bmp(struct pipe_context *pipe,
583 const char *filename,
584 struct pipe_surface *surface)
585 {
586 struct pipe_transfer *transfer;
587 struct pipe_resource *texture = surface->texture;
588 void *ptr;
589
590 ptr = pipe_transfer_map(pipe, texture, surface->u.tex.level,
591 surface->u.tex.first_layer, PIPE_TRANSFER_READ,
592 0, 0, surface->width, surface->height, &transfer);
593
594 debug_dump_transfer_bmp(pipe, filename, transfer, ptr);
595
596 pipe->transfer_unmap(pipe, transfer);
597 }
598
599 void
600 debug_dump_transfer_bmp(struct pipe_context *pipe,
601 const char *filename,
602 struct pipe_transfer *transfer, void *ptr)
603 {
604 float *rgba;
605
606 if (!transfer)
607 goto error1;
608
609 rgba = MALLOC(transfer->box.width *
610 transfer->box.height *
611 transfer->box.depth *
612 4*sizeof(float));
613 if(!rgba)
614 goto error1;
615
616 pipe_get_tile_rgba(transfer, ptr, 0, 0,
617 transfer->box.width, transfer->box.height,
618 rgba);
619
620 debug_dump_float_rgba_bmp(filename,
621 transfer->box.width, transfer->box.height,
622 rgba, transfer->box.width);
623
624 FREE(rgba);
625 error1:
626 ;
627 }
628
629 void
630 debug_dump_float_rgba_bmp(const char *filename,
631 unsigned width, unsigned height,
632 float *rgba, unsigned stride)
633 {
634 FILE *stream;
635 struct bmp_file_header bmfh;
636 struct bmp_info_header bmih;
637 unsigned x, y;
638
639 if(!rgba)
640 goto error1;
641
642 bmfh.bfType = 0x4d42;
643 bmfh.bfSize = 14 + 40 + height*width*4;
644 bmfh.bfReserved1 = 0;
645 bmfh.bfReserved2 = 0;
646 bmfh.bfOffBits = 14 + 40;
647
648 bmih.biSize = 40;
649 bmih.biWidth = width;
650 bmih.biHeight = height;
651 bmih.biPlanes = 1;
652 bmih.biBitCount = 32;
653 bmih.biCompression = 0;
654 bmih.biSizeImage = height*width*4;
655 bmih.biXPelsPerMeter = 0;
656 bmih.biYPelsPerMeter = 0;
657 bmih.biClrUsed = 0;
658 bmih.biClrImportant = 0;
659
660 stream = fopen(filename, "wb");
661 if(!stream)
662 goto error1;
663
664 fwrite(&bmfh, 14, 1, stream);
665 fwrite(&bmih, 40, 1, stream);
666
667 y = height;
668 while(y--) {
669 float *ptr = rgba + (stride * y * 4);
670 for(x = 0; x < width; ++x)
671 {
672 struct bmp_rgb_quad pixel;
673 pixel.rgbRed = float_to_ubyte(ptr[x*4 + 0]);
674 pixel.rgbGreen = float_to_ubyte(ptr[x*4 + 1]);
675 pixel.rgbBlue = float_to_ubyte(ptr[x*4 + 2]);
676 pixel.rgbAlpha = float_to_ubyte(ptr[x*4 + 3]);
677 fwrite(&pixel, 1, 4, stream);
678 }
679 }
680
681 fclose(stream);
682 error1:
683 ;
684 }
685
686
687 /**
688 * Print PIPE_TRANSFER_x flags with a message.
689 */
690 void
691 debug_print_transfer_flags(const char *msg, unsigned usage)
692 {
693 #define FLAG(x) { x, #x }
694 static const struct {
695 unsigned bit;
696 const char *name;
697 } flags[] = {
698 FLAG(PIPE_TRANSFER_READ),
699 FLAG(PIPE_TRANSFER_WRITE),
700 FLAG(PIPE_TRANSFER_MAP_DIRECTLY),
701 FLAG(PIPE_TRANSFER_DISCARD_RANGE),
702 FLAG(PIPE_TRANSFER_DONTBLOCK),
703 FLAG(PIPE_TRANSFER_UNSYNCHRONIZED),
704 FLAG(PIPE_TRANSFER_FLUSH_EXPLICIT),
705 FLAG(PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)
706 };
707 unsigned i;
708
709 debug_printf("%s ", msg);
710
711 for (i = 0; i < Elements(flags); i++) {
712 if (usage & flags[i].bit) {
713 debug_printf("%s", flags[i].name);
714 usage &= ~flags[i].bit;
715 if (usage) {
716 debug_printf(" | ");
717 }
718 }
719 }
720
721 debug_printf("\n");
722 #undef FLAG
723 }
724
725
726
727 #endif