1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2008 VMware, Inc.
4bf215546Sopenharmony_ci * Copyright (c) 2008 VMware, Inc.
5bf215546Sopenharmony_ci * All Rights Reserved.
6bf215546Sopenharmony_ci *
7bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
8bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
9bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
10bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
11bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
12bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
13bf215546Sopenharmony_ci * the following conditions:
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
16bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
17bf215546Sopenharmony_ci * of the Software.
18bf215546Sopenharmony_ci *
19bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26bf215546Sopenharmony_ci *
27bf215546Sopenharmony_ci **************************************************************************/
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "util/u_debug.h"
31bf215546Sopenharmony_ci#include "util/u_string.h"
32bf215546Sopenharmony_ci#include "util/u_math.h"
33bf215546Sopenharmony_ci#include <inttypes.h>
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include <stdio.h>
36bf215546Sopenharmony_ci#include <limits.h> /* CHAR_BIT */
37bf215546Sopenharmony_ci#include <ctype.h> /* isalnum */
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#ifdef _WIN32
40bf215546Sopenharmony_ci#include <windows.h>
41bf215546Sopenharmony_ci#include <stdlib.h>
42bf215546Sopenharmony_ci#endif
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_civoid
46bf215546Sopenharmony_ci_debug_vprintf(const char *format, va_list ap)
47bf215546Sopenharmony_ci{
48bf215546Sopenharmony_ci   static char buf[4096] = {'\0'};
49bf215546Sopenharmony_ci#if DETECT_OS_WINDOWS || defined(EMBEDDED_DEVICE)
50bf215546Sopenharmony_ci   /* We buffer until we find a newline. */
51bf215546Sopenharmony_ci   size_t len = strlen(buf);
52bf215546Sopenharmony_ci   int ret = vsnprintf(buf + len, sizeof(buf) - len, format, ap);
53bf215546Sopenharmony_ci   if (ret > (int)(sizeof(buf) - len - 1) || strchr(buf + len, '\n')) {
54bf215546Sopenharmony_ci      os_log_message(buf);
55bf215546Sopenharmony_ci      buf[0] = '\0';
56bf215546Sopenharmony_ci   }
57bf215546Sopenharmony_ci#else
58bf215546Sopenharmony_ci   vsnprintf(buf, sizeof(buf), format, ap);
59bf215546Sopenharmony_ci   os_log_message(buf);
60bf215546Sopenharmony_ci#endif
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_civoid
65bf215546Sopenharmony_ci_util_debug_message(struct util_debug_callback *cb,
66bf215546Sopenharmony_ci                    unsigned *id,
67bf215546Sopenharmony_ci                    enum util_debug_type type,
68bf215546Sopenharmony_ci                    const char *fmt, ...)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci   va_list args;
71bf215546Sopenharmony_ci   va_start(args, fmt);
72bf215546Sopenharmony_ci   if (cb && cb->debug_message)
73bf215546Sopenharmony_ci      cb->debug_message(cb->data, id, type, fmt, args);
74bf215546Sopenharmony_ci   va_end(args);
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ci#ifdef _WIN32
79bf215546Sopenharmony_civoid
80bf215546Sopenharmony_cidebug_disable_win32_error_dialogs(void)
81bf215546Sopenharmony_ci{
82bf215546Sopenharmony_ci   /* When Windows' error message boxes are disabled for this process (as is
83bf215546Sopenharmony_ci    * typically the case when running tests in an automated fashion) we disable
84bf215546Sopenharmony_ci    * CRT message boxes too.
85bf215546Sopenharmony_ci    */
86bf215546Sopenharmony_ci   UINT uMode = SetErrorMode(0);
87bf215546Sopenharmony_ci   SetErrorMode(uMode);
88bf215546Sopenharmony_ci   if (uMode & SEM_FAILCRITICALERRORS) {
89bf215546Sopenharmony_ci      /* Disable assertion failure message box.
90bf215546Sopenharmony_ci       * http://msdn.microsoft.com/en-us/library/sas1dkb2.aspx
91bf215546Sopenharmony_ci       */
92bf215546Sopenharmony_ci      _set_error_mode(_OUT_TO_STDERR);
93bf215546Sopenharmony_ci#ifdef _MSC_VER
94bf215546Sopenharmony_ci      /* Disable abort message box.
95bf215546Sopenharmony_ci       * http://msdn.microsoft.com/en-us/library/e631wekh.aspx
96bf215546Sopenharmony_ci       */
97bf215546Sopenharmony_ci      _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
98bf215546Sopenharmony_ci#endif
99bf215546Sopenharmony_ci   }
100bf215546Sopenharmony_ci}
101bf215546Sopenharmony_ci#endif /* _WIN32 */
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci#ifdef DEBUG
105bf215546Sopenharmony_civoid
106bf215546Sopenharmony_cidebug_print_blob(const char *name, const void *blob, unsigned size)
107bf215546Sopenharmony_ci{
108bf215546Sopenharmony_ci   const unsigned *ublob = (const unsigned *)blob;
109bf215546Sopenharmony_ci   unsigned i;
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   debug_printf("%s (%d dwords%s)\n", name, size/4,
112bf215546Sopenharmony_ci                size%4 ? "... plus a few bytes" : "");
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   for (i = 0; i < size/4; i++) {
115bf215546Sopenharmony_ci      debug_printf("%d:\t%08x\n", i, ublob[i]);
116bf215546Sopenharmony_ci   }
117bf215546Sopenharmony_ci}
118bf215546Sopenharmony_ci#endif
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_cistatic bool
122bf215546Sopenharmony_cidebug_get_option_should_print(void)
123bf215546Sopenharmony_ci{
124bf215546Sopenharmony_ci   static bool initialized = false;
125bf215546Sopenharmony_ci   static bool value = false;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   if (initialized)
128bf215546Sopenharmony_ci      return value;
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   /* Oh hey this will call into this function,
131bf215546Sopenharmony_ci    * but its cool since we set first to false
132bf215546Sopenharmony_ci    */
133bf215546Sopenharmony_ci   initialized = true;
134bf215546Sopenharmony_ci   value = debug_get_bool_option("GALLIUM_PRINT_OPTIONS", false);
135bf215546Sopenharmony_ci   /* XXX should we print this option? Currently it wont */
136bf215546Sopenharmony_ci   return value;
137bf215546Sopenharmony_ci}
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ciconst char *
141bf215546Sopenharmony_cidebug_get_option(const char *name, const char *dfault)
142bf215546Sopenharmony_ci{
143bf215546Sopenharmony_ci   const char *result;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   result = os_get_option(name);
146bf215546Sopenharmony_ci   if (!result)
147bf215546Sopenharmony_ci      result = dfault;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   if (debug_get_option_should_print())
150bf215546Sopenharmony_ci      debug_printf("%s: %s = %s\n", __FUNCTION__, name,
151bf215546Sopenharmony_ci                   result ? result : "(null)");
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   return result;
154bf215546Sopenharmony_ci}
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_cibool
158bf215546Sopenharmony_cidebug_get_bool_option(const char *name, bool dfault)
159bf215546Sopenharmony_ci{
160bf215546Sopenharmony_ci   const char *str = os_get_option(name);
161bf215546Sopenharmony_ci   bool result;
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   if (str == NULL)
164bf215546Sopenharmony_ci      result = dfault;
165bf215546Sopenharmony_ci   else if (!strcmp(str, "n"))
166bf215546Sopenharmony_ci      result = false;
167bf215546Sopenharmony_ci   else if (!strcmp(str, "no"))
168bf215546Sopenharmony_ci      result = false;
169bf215546Sopenharmony_ci   else if (!strcmp(str, "0"))
170bf215546Sopenharmony_ci      result = false;
171bf215546Sopenharmony_ci   else if (!strcmp(str, "f"))
172bf215546Sopenharmony_ci      result = false;
173bf215546Sopenharmony_ci   else if (!strcmp(str, "F"))
174bf215546Sopenharmony_ci      result = false;
175bf215546Sopenharmony_ci   else if (!strcmp(str, "false"))
176bf215546Sopenharmony_ci      result = false;
177bf215546Sopenharmony_ci   else if (!strcmp(str, "FALSE"))
178bf215546Sopenharmony_ci      result = false;
179bf215546Sopenharmony_ci   else
180bf215546Sopenharmony_ci      result = true;
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   if (debug_get_option_should_print())
183bf215546Sopenharmony_ci      debug_printf("%s: %s = %s\n", __FUNCTION__, name,
184bf215546Sopenharmony_ci                   result ? "TRUE" : "FALSE");
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci   return result;
187bf215546Sopenharmony_ci}
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_cilong
191bf215546Sopenharmony_cidebug_get_num_option(const char *name, long dfault)
192bf215546Sopenharmony_ci{
193bf215546Sopenharmony_ci   long result;
194bf215546Sopenharmony_ci   const char *str;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   str = os_get_option(name);
197bf215546Sopenharmony_ci   if (!str) {
198bf215546Sopenharmony_ci      result = dfault;
199bf215546Sopenharmony_ci   } else {
200bf215546Sopenharmony_ci      char *endptr;
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci      result = strtol(str, &endptr, 0);
203bf215546Sopenharmony_ci      if (str == endptr) {
204bf215546Sopenharmony_ci         /* Restore the default value when no digits were found. */
205bf215546Sopenharmony_ci         result = dfault;
206bf215546Sopenharmony_ci      }
207bf215546Sopenharmony_ci   }
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci   if (debug_get_option_should_print())
210bf215546Sopenharmony_ci      debug_printf("%s: %s = %li\n", __FUNCTION__, name, result);
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   return result;
213bf215546Sopenharmony_ci}
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_civoid
216bf215546Sopenharmony_cidebug_get_version_option(const char *name, unsigned *major, unsigned *minor)
217bf215546Sopenharmony_ci{
218bf215546Sopenharmony_ci   const char *str;
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci   str = os_get_option(name);
221bf215546Sopenharmony_ci   if (str) {
222bf215546Sopenharmony_ci      unsigned v_maj, v_min;
223bf215546Sopenharmony_ci      int n;
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci      n = sscanf(str, "%u.%u", &v_maj, &v_min);
226bf215546Sopenharmony_ci      if (n != 2) {
227bf215546Sopenharmony_ci         debug_printf("Illegal version specified for %s : %s\n", name, str);
228bf215546Sopenharmony_ci         return;
229bf215546Sopenharmony_ci      }
230bf215546Sopenharmony_ci      *major = v_maj;
231bf215546Sopenharmony_ci      *minor = v_min;
232bf215546Sopenharmony_ci   }
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci   if (debug_get_option_should_print())
235bf215546Sopenharmony_ci      debug_printf("%s: %s = %u.%u\n", __FUNCTION__, name, *major, *minor);
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci   return;
238bf215546Sopenharmony_ci}
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_cistatic bool
241bf215546Sopenharmony_cistr_has_option(const char *str, const char *name)
242bf215546Sopenharmony_ci{
243bf215546Sopenharmony_ci   /* Empty string. */
244bf215546Sopenharmony_ci   if (!*str) {
245bf215546Sopenharmony_ci      return false;
246bf215546Sopenharmony_ci   }
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci   /* OPTION=all */
249bf215546Sopenharmony_ci   if (!strcmp(str, "all")) {
250bf215546Sopenharmony_ci      return true;
251bf215546Sopenharmony_ci   }
252bf215546Sopenharmony_ci
253bf215546Sopenharmony_ci   /* Find 'name' in 'str' surrounded by non-alphanumeric characters. */
254bf215546Sopenharmony_ci   {
255bf215546Sopenharmony_ci      const char *start = str;
256bf215546Sopenharmony_ci      unsigned name_len = strlen(name);
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci      /* 'start' is the beginning of the currently-parsed word,
259bf215546Sopenharmony_ci       * we increment 'str' each iteration.
260bf215546Sopenharmony_ci       * if we find either the end of string or a non-alphanumeric character,
261bf215546Sopenharmony_ci       * we compare 'start' up to 'str-1' with 'name'. */
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_ci      while (1) {
264bf215546Sopenharmony_ci         if (!*str || !(isalnum(*str) || *str == '_')) {
265bf215546Sopenharmony_ci            if (str-start == name_len &&
266bf215546Sopenharmony_ci                !memcmp(start, name, name_len)) {
267bf215546Sopenharmony_ci               return true;
268bf215546Sopenharmony_ci            }
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci            if (!*str) {
271bf215546Sopenharmony_ci               return false;
272bf215546Sopenharmony_ci            }
273bf215546Sopenharmony_ci
274bf215546Sopenharmony_ci            start = str+1;
275bf215546Sopenharmony_ci         }
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci         str++;
278bf215546Sopenharmony_ci      }
279bf215546Sopenharmony_ci   }
280bf215546Sopenharmony_ci
281bf215546Sopenharmony_ci   return false;
282bf215546Sopenharmony_ci}
283bf215546Sopenharmony_ci
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ciuint64_t
286bf215546Sopenharmony_cidebug_get_flags_option(const char *name,
287bf215546Sopenharmony_ci                       const struct debug_named_value *flags,
288bf215546Sopenharmony_ci                       uint64_t dfault)
289bf215546Sopenharmony_ci{
290bf215546Sopenharmony_ci   uint64_t result;
291bf215546Sopenharmony_ci   const char *str;
292bf215546Sopenharmony_ci   const struct debug_named_value *orig = flags;
293bf215546Sopenharmony_ci   unsigned namealign = 0;
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci   str = os_get_option(name);
296bf215546Sopenharmony_ci   if (!str)
297bf215546Sopenharmony_ci      result = dfault;
298bf215546Sopenharmony_ci   else if (!strcmp(str, "help")) {
299bf215546Sopenharmony_ci      result = dfault;
300bf215546Sopenharmony_ci      _debug_printf("%s: help for %s:\n", __FUNCTION__, name);
301bf215546Sopenharmony_ci      for (; flags->name; ++flags)
302bf215546Sopenharmony_ci         namealign = MAX2(namealign, strlen(flags->name));
303bf215546Sopenharmony_ci      for (flags = orig; flags->name; ++flags)
304bf215546Sopenharmony_ci         _debug_printf("| %*s [0x%0*"PRIx64"]%s%s\n", namealign, flags->name,
305bf215546Sopenharmony_ci                      (int)sizeof(uint64_t)*CHAR_BIT/4, flags->value,
306bf215546Sopenharmony_ci                      flags->desc ? " " : "", flags->desc ? flags->desc : "");
307bf215546Sopenharmony_ci   }
308bf215546Sopenharmony_ci   else {
309bf215546Sopenharmony_ci      result = 0;
310bf215546Sopenharmony_ci      while (flags->name) {
311bf215546Sopenharmony_ci	 if (str_has_option(str, flags->name))
312bf215546Sopenharmony_ci	    result |= flags->value;
313bf215546Sopenharmony_ci	 ++flags;
314bf215546Sopenharmony_ci      }
315bf215546Sopenharmony_ci   }
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci   if (debug_get_option_should_print()) {
318bf215546Sopenharmony_ci      if (str) {
319bf215546Sopenharmony_ci         debug_printf("%s: %s = 0x%"PRIx64" (%s)\n",
320bf215546Sopenharmony_ci                      __FUNCTION__, name, result, str);
321bf215546Sopenharmony_ci      } else {
322bf215546Sopenharmony_ci         debug_printf("%s: %s = 0x%"PRIx64"\n", __FUNCTION__, name, result);
323bf215546Sopenharmony_ci      }
324bf215546Sopenharmony_ci   }
325bf215546Sopenharmony_ci
326bf215546Sopenharmony_ci   return result;
327bf215546Sopenharmony_ci}
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ciconst char *
331bf215546Sopenharmony_cidebug_dump_enum(const struct debug_named_value *names,
332bf215546Sopenharmony_ci                unsigned long value)
333bf215546Sopenharmony_ci{
334bf215546Sopenharmony_ci   static char rest[64];
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci   while (names->name) {
337bf215546Sopenharmony_ci      if (names->value == value)
338bf215546Sopenharmony_ci	 return names->name;
339bf215546Sopenharmony_ci      ++names;
340bf215546Sopenharmony_ci   }
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_ci   snprintf(rest, sizeof(rest), "0x%08lx", value);
343bf215546Sopenharmony_ci   return rest;
344bf215546Sopenharmony_ci}
345bf215546Sopenharmony_ci
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ciconst char *
348bf215546Sopenharmony_cidebug_dump_enum_noprefix(const struct debug_named_value *names,
349bf215546Sopenharmony_ci                         const char *prefix,
350bf215546Sopenharmony_ci                         unsigned long value)
351bf215546Sopenharmony_ci{
352bf215546Sopenharmony_ci   static char rest[64];
353bf215546Sopenharmony_ci
354bf215546Sopenharmony_ci   while (names->name) {
355bf215546Sopenharmony_ci      if (names->value == value) {
356bf215546Sopenharmony_ci         const char *name = names->name;
357bf215546Sopenharmony_ci         while (*name == *prefix) {
358bf215546Sopenharmony_ci            name++;
359bf215546Sopenharmony_ci            prefix++;
360bf215546Sopenharmony_ci         }
361bf215546Sopenharmony_ci         return name;
362bf215546Sopenharmony_ci      }
363bf215546Sopenharmony_ci      ++names;
364bf215546Sopenharmony_ci   }
365bf215546Sopenharmony_ci
366bf215546Sopenharmony_ci   snprintf(rest, sizeof(rest), "0x%08lx", value);
367bf215546Sopenharmony_ci   return rest;
368bf215546Sopenharmony_ci}
369bf215546Sopenharmony_ci
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ciconst char *
372bf215546Sopenharmony_cidebug_dump_flags(const struct debug_named_value *names, unsigned long value)
373bf215546Sopenharmony_ci{
374bf215546Sopenharmony_ci   static char output[4096];
375bf215546Sopenharmony_ci   static char rest[256];
376bf215546Sopenharmony_ci   int first = 1;
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci   output[0] = '\0';
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci   while (names->name) {
381bf215546Sopenharmony_ci      if ((names->value & value) == names->value) {
382bf215546Sopenharmony_ci	 if (!first)
383bf215546Sopenharmony_ci	    strncat(output, "|", sizeof(output) - strlen(output) - 1);
384bf215546Sopenharmony_ci	 else
385bf215546Sopenharmony_ci	    first = 0;
386bf215546Sopenharmony_ci	 strncat(output, names->name, sizeof(output) - strlen(output) - 1);
387bf215546Sopenharmony_ci	 output[sizeof(output) - 1] = '\0';
388bf215546Sopenharmony_ci	 value &= ~names->value;
389bf215546Sopenharmony_ci      }
390bf215546Sopenharmony_ci      ++names;
391bf215546Sopenharmony_ci   }
392bf215546Sopenharmony_ci
393bf215546Sopenharmony_ci   if (value) {
394bf215546Sopenharmony_ci      if (!first)
395bf215546Sopenharmony_ci	 strncat(output, "|", sizeof(output) - strlen(output) - 1);
396bf215546Sopenharmony_ci      else
397bf215546Sopenharmony_ci	 first = 0;
398bf215546Sopenharmony_ci
399bf215546Sopenharmony_ci      snprintf(rest, sizeof(rest), "0x%08lx", value);
400bf215546Sopenharmony_ci      strncat(output, rest, sizeof(output) - strlen(output) - 1);
401bf215546Sopenharmony_ci      output[sizeof(output) - 1] = '\0';
402bf215546Sopenharmony_ci   }
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci   if (first)
405bf215546Sopenharmony_ci      return "0";
406bf215546Sopenharmony_ci
407bf215546Sopenharmony_ci   return output;
408bf215546Sopenharmony_ci}
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci#ifdef DEBUG
413bf215546Sopenharmony_ciint fl_indent = 0;
414bf215546Sopenharmony_ciconst char* fl_function[1024];
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ciint
417bf215546Sopenharmony_cidebug_funclog_enter(const char* f, UNUSED const int line,
418bf215546Sopenharmony_ci                    UNUSED const char* file)
419bf215546Sopenharmony_ci{
420bf215546Sopenharmony_ci   int i;
421bf215546Sopenharmony_ci
422bf215546Sopenharmony_ci   for (i = 0; i < fl_indent; i++)
423bf215546Sopenharmony_ci      debug_printf("  ");
424bf215546Sopenharmony_ci   debug_printf("%s\n", f);
425bf215546Sopenharmony_ci
426bf215546Sopenharmony_ci   assert(fl_indent < 1023);
427bf215546Sopenharmony_ci   fl_function[fl_indent++] = f;
428bf215546Sopenharmony_ci
429bf215546Sopenharmony_ci   return 0;
430bf215546Sopenharmony_ci}
431bf215546Sopenharmony_ci
432bf215546Sopenharmony_civoid
433bf215546Sopenharmony_cidebug_funclog_exit(const char* f, UNUSED const int line,
434bf215546Sopenharmony_ci                   UNUSED const char* file)
435bf215546Sopenharmony_ci{
436bf215546Sopenharmony_ci   --fl_indent;
437bf215546Sopenharmony_ci   assert(fl_indent >= 0);
438bf215546Sopenharmony_ci   assert(fl_function[fl_indent] == f);
439bf215546Sopenharmony_ci}
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_civoid
442bf215546Sopenharmony_cidebug_funclog_enter_exit(const char* f, UNUSED const int line,
443bf215546Sopenharmony_ci                         UNUSED const char* file)
444bf215546Sopenharmony_ci{
445bf215546Sopenharmony_ci   int i;
446bf215546Sopenharmony_ci   for (i = 0; i < fl_indent; i++)
447bf215546Sopenharmony_ci      debug_printf("  ");
448bf215546Sopenharmony_ci   debug_printf("%s\n", f);
449bf215546Sopenharmony_ci}
450bf215546Sopenharmony_ci#endif
451