Imported Upstream version 2.6.4
[platform/upstream/harfbuzz.git] / src / hb-debug.hh
1 /*
2  * Copyright © 2017  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  *
24  * Google Author(s): Behdad Esfahbod
25  */
26
27 #ifndef HB_DEBUG_HH
28 #define HB_DEBUG_HH
29
30 #include "hb.hh"
31 #include "hb-atomic.hh"
32 #include "hb-algs.hh"
33
34
35 #ifndef HB_DEBUG
36 #define HB_DEBUG 0
37 #endif
38
39
40 /*
41  * Global runtime options.
42  */
43
44 struct hb_options_t
45 {
46   bool unused : 1; /* In-case sign bit is here. */
47   bool initialized : 1;
48   bool uniscribe_bug_compatible : 1;
49   bool aat : 1;
50 };
51
52 union hb_options_union_t {
53   int i;
54   hb_options_t opts;
55 };
56 static_assert ((sizeof (hb_atomic_int_t) >= sizeof (hb_options_union_t)), "");
57
58 HB_INTERNAL void
59 _hb_options_init ();
60
61 extern HB_INTERNAL hb_atomic_int_t _hb_options;
62
63 static inline hb_options_t
64 hb_options ()
65 {
66 #ifdef HB_NO_GETENV
67   return hb_options_t ();
68 #endif
69   /* Make a local copy, so we can access bitfield threadsafely. */
70   hb_options_union_t u;
71   u.i = _hb_options.get_relaxed ();
72
73   if (unlikely (!u.i))
74   {
75     _hb_options_init ();
76     u.i = _hb_options.get_relaxed ();
77   }
78
79   return u.opts;
80 }
81
82
83 /*
84  * Debug output (needs enabling at compile time.)
85  */
86
87 static inline bool
88 _hb_debug (unsigned int level,
89            unsigned int max_level)
90 {
91   return level < max_level;
92 }
93
94 #define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
95 #define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0))
96
97 static inline void
98 _hb_print_func (const char *func)
99 {
100   if (func)
101   {
102     unsigned int func_len = strlen (func);
103     /* Skip "static" */
104     if (0 == strncmp (func, "static ", 7))
105       func += 7;
106     /* Skip "typename" */
107     if (0 == strncmp (func, "typename ", 9))
108       func += 9;
109     /* Skip return type */
110     const char *space = strchr (func, ' ');
111     if (space)
112       func = space + 1;
113     /* Skip parameter list */
114     const char *paren = strchr (func, '(');
115     if (paren)
116       func_len = paren - func;
117     fprintf (stderr, "%.*s", func_len, func);
118   }
119 }
120
121 template <int max_level> static inline void
122 _hb_debug_msg_va (const char *what,
123                   const void *obj,
124                   const char *func,
125                   bool indented,
126                   unsigned int level,
127                   int level_dir,
128                   const char *message,
129                   va_list ap) HB_PRINTF_FUNC(7, 0);
130 template <int max_level> static inline void
131 _hb_debug_msg_va (const char *what,
132                   const void *obj,
133                   const char *func,
134                   bool indented,
135                   unsigned int level,
136                   int level_dir,
137                   const char *message,
138                   va_list ap)
139 {
140   if (!_hb_debug (level, max_level))
141     return;
142
143   fprintf (stderr, "%-10s", what ? what : "");
144
145   if (obj)
146     fprintf (stderr, "(%*p) ", (unsigned int) (2 * sizeof (void *)), obj);
147   else
148     fprintf (stderr, " %*s  ", (unsigned int) (2 * sizeof (void *)), "");
149
150   if (indented) {
151 #define VBAR    "\342\224\202"  /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
152 #define VRBAR   "\342\224\234"  /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
153 #define DLBAR   "\342\225\256"  /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
154 #define ULBAR   "\342\225\257"  /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
155 #define LBAR    "\342\225\264"  /* U+2574 BOX DRAWINGS LIGHT LEFT */
156     static const char bars[] =
157       VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
158       VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
159       VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
160       VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
161       VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR;
162     fprintf (stderr, "%2u %s" VRBAR "%s",
163              level,
164              bars + sizeof (bars) - 1 - hb_min ((unsigned int) sizeof (bars) - 1, (unsigned int) (sizeof (VBAR) - 1) * level),
165              level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
166   } else
167     fprintf (stderr, "   " VRBAR LBAR);
168
169   _hb_print_func (func);
170
171   if (message)
172   {
173     fprintf (stderr, ": ");
174     vfprintf (stderr, message, ap);
175   }
176
177   fprintf (stderr, "\n");
178 }
179 template <> inline void HB_PRINTF_FUNC(7, 0)
180 _hb_debug_msg_va<0> (const char *what HB_UNUSED,
181                      const void *obj HB_UNUSED,
182                      const char *func HB_UNUSED,
183                      bool indented HB_UNUSED,
184                      unsigned int level HB_UNUSED,
185                      int level_dir HB_UNUSED,
186                      const char *message HB_UNUSED,
187                      va_list ap HB_UNUSED) {}
188
189 template <int max_level> static inline void
190 _hb_debug_msg (const char *what,
191                const void *obj,
192                const char *func,
193                bool indented,
194                unsigned int level,
195                int level_dir,
196                const char *message,
197                ...) HB_PRINTF_FUNC(7, 8);
198 template <int max_level> static inline void HB_PRINTF_FUNC(7, 8)
199 _hb_debug_msg (const char *what,
200                const void *obj,
201                const char *func,
202                bool indented,
203                unsigned int level,
204                int level_dir,
205                const char *message,
206                ...)
207 {
208   va_list ap;
209   va_start (ap, message);
210   _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
211   va_end (ap);
212 }
213 template <> inline void
214 _hb_debug_msg<0> (const char *what HB_UNUSED,
215                   const void *obj HB_UNUSED,
216                   const char *func HB_UNUSED,
217                   bool indented HB_UNUSED,
218                   unsigned int level HB_UNUSED,
219                   int level_dir HB_UNUSED,
220                   const char *message HB_UNUSED,
221                   ...) HB_PRINTF_FUNC(7, 8);
222 template <> inline void HB_PRINTF_FUNC(7, 8)
223 _hb_debug_msg<0> (const char *what HB_UNUSED,
224                   const void *obj HB_UNUSED,
225                   const char *func HB_UNUSED,
226                   bool indented HB_UNUSED,
227                   unsigned int level HB_UNUSED,
228                   int level_dir HB_UNUSED,
229                   const char *message HB_UNUSED,
230                   ...) {}
231
232 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...)       _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), nullptr,    true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
233 #define DEBUG_MSG(WHAT, OBJ, ...)                               _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), nullptr,    false, 0, 0, __VA_ARGS__)
234 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...)                          _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
235
236
237 /*
238  * Printer
239  */
240
241 template <typename T>
242 struct hb_printer_t {
243   const char *print (const T&) { return "something"; }
244 };
245
246 template <>
247 struct hb_printer_t<bool> {
248   const char *print (bool v) { return v ? "true" : "false"; }
249 };
250
251 template <>
252 struct hb_printer_t<hb_empty_t> {
253   const char *print (hb_empty_t) { return ""; }
254 };
255
256
257 /*
258  * Trace
259  */
260
261 template <typename T>
262 static inline void _hb_warn_no_return (bool returned)
263 {
264   if (unlikely (!returned)) {
265     fprintf (stderr, "OUCH, returned with no call to return_trace().  This is a bug, please report.\n");
266   }
267 }
268 template <>
269 /*static*/ inline void _hb_warn_no_return<hb_empty_t> (bool returned HB_UNUSED)
270 {}
271
272 template <int max_level, typename ret_t>
273 struct hb_auto_trace_t
274 {
275   explicit inline hb_auto_trace_t (unsigned int *plevel_,
276                                    const char *what_,
277                                    const void *obj_,
278                                    const char *func,
279                                    const char *message,
280                                    ...) HB_PRINTF_FUNC(6, 7)
281                                    : plevel (plevel_), what (what_), obj (obj_), returned (false)
282   {
283     if (plevel) ++*plevel;
284
285     va_list ap;
286     va_start (ap, message);
287     _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
288     va_end (ap);
289   }
290   ~hb_auto_trace_t ()
291   {
292     _hb_warn_no_return<ret_t> (returned);
293     if (!returned) {
294       _hb_debug_msg<max_level> (what, obj, nullptr, true, plevel ? *plevel : 1, -1, " ");
295     }
296     if (plevel) --*plevel;
297   }
298
299   template <typename T>
300   T ret (T&& v,
301          const char *func = "",
302          unsigned int line = 0)
303   {
304     if (unlikely (returned)) {
305       fprintf (stderr, "OUCH, double calls to return_trace().  This is a bug, please report.\n");
306       return hb_forward<T> (v);
307     }
308
309     _hb_debug_msg<max_level> (what, obj, func, true, plevel ? *plevel : 1, -1,
310                               "return %s (line %d)",
311                               hb_printer_t<decltype (v)>().print (v), line);
312     if (plevel) --*plevel;
313     plevel = nullptr;
314     returned = true;
315     return hb_forward<T> (v);
316   }
317
318   private:
319   unsigned int *plevel;
320   const char *what;
321   const void *obj;
322   bool returned;
323 };
324 template <typename ret_t> /* Make sure we don't use hb_auto_trace_t when not tracing. */
325 struct hb_auto_trace_t<0, ret_t>
326 {
327   explicit inline hb_auto_trace_t (unsigned int *plevel_,
328                                    const char *what_,
329                                    const void *obj_,
330                                    const char *func,
331                                    const char *message,
332                                    ...) HB_PRINTF_FUNC(6, 7) {}
333
334   template <typename T>
335   T ret (T&& v,
336          const char *func HB_UNUSED = nullptr,
337          unsigned int line HB_UNUSED = 0) { return hb_forward<T> (v); }
338 };
339
340 /* For disabled tracing; optimize out everything.
341  * https://github.com/harfbuzz/harfbuzz/pull/605 */
342 template <typename ret_t>
343 struct hb_no_trace_t {
344   template <typename T>
345   T ret (T&& v,
346          const char *func HB_UNUSED = nullptr,
347          unsigned int line HB_UNUSED = 0) { return hb_forward<T> (v); }
348 };
349
350 #define return_trace(RET) return trace.ret (RET, HB_FUNC, __LINE__)
351
352
353 /*
354  * Instances.
355  */
356
357 #ifndef HB_DEBUG_ARABIC
358 #define HB_DEBUG_ARABIC (HB_DEBUG+0)
359 #endif
360
361 #ifndef HB_DEBUG_BLOB
362 #define HB_DEBUG_BLOB (HB_DEBUG+0)
363 #endif
364
365 #ifndef HB_DEBUG_CORETEXT
366 #define HB_DEBUG_CORETEXT (HB_DEBUG+0)
367 #endif
368
369 #ifndef HB_DEBUG_DIRECTWRITE
370 #define HB_DEBUG_DIRECTWRITE (HB_DEBUG+0)
371 #endif
372
373 #ifndef HB_DEBUG_FT
374 #define HB_DEBUG_FT (HB_DEBUG+0)
375 #endif
376
377 #ifndef HB_DEBUG_GET_COVERAGE
378 #define HB_DEBUG_GET_COVERAGE (HB_DEBUG+0)
379 #endif
380
381 #ifndef HB_DEBUG_OBJECT
382 #define HB_DEBUG_OBJECT (HB_DEBUG+0)
383 #endif
384
385 #ifndef HB_DEBUG_SHAPE_PLAN
386 #define HB_DEBUG_SHAPE_PLAN (HB_DEBUG+0)
387 #endif
388
389 #ifndef HB_DEBUG_UNISCRIBE
390 #define HB_DEBUG_UNISCRIBE (HB_DEBUG+0)
391 #endif
392
393 /*
394  * With tracing.
395  */
396
397 #ifndef HB_DEBUG_APPLY
398 #define HB_DEBUG_APPLY (HB_DEBUG+0)
399 #endif
400 #if HB_DEBUG_APPLY
401 #define TRACE_APPLY(this) \
402         hb_auto_trace_t<HB_DEBUG_APPLY, bool> trace \
403         (&c->debug_depth, c->get_name (), this, HB_FUNC, \
404          "idx %d gid %u lookup %d", \
405          c->buffer->idx, c->buffer->cur().codepoint, (int) c->lookup_index)
406 #else
407 #define TRACE_APPLY(this) hb_no_trace_t<bool> trace
408 #endif
409
410 #ifndef HB_DEBUG_SANITIZE
411 #define HB_DEBUG_SANITIZE (HB_DEBUG+0)
412 #endif
413 #if HB_DEBUG_SANITIZE
414 #define TRACE_SANITIZE(this) \
415         hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace \
416         (&c->debug_depth, c->get_name (), this, HB_FUNC, \
417          " ")
418 #else
419 #define TRACE_SANITIZE(this) hb_no_trace_t<bool> trace
420 #endif
421
422 #ifndef HB_DEBUG_SERIALIZE
423 #define HB_DEBUG_SERIALIZE (HB_DEBUG+0)
424 #endif
425 #if HB_DEBUG_SERIALIZE
426 #define TRACE_SERIALIZE(this) \
427         hb_auto_trace_t<HB_DEBUG_SERIALIZE, bool> trace \
428         (&c->debug_depth, "SERIALIZE", c, HB_FUNC, \
429          " ")
430 #else
431 #define TRACE_SERIALIZE(this) hb_no_trace_t<bool> trace
432 #endif
433
434 #ifndef HB_DEBUG_SUBSET
435 #define HB_DEBUG_SUBSET (HB_DEBUG+0)
436 #endif
437 #if HB_DEBUG_SUBSET
438 #define TRACE_SUBSET(this) \
439   hb_auto_trace_t<HB_DEBUG_SUBSET, bool> trace \
440   (&c->debug_depth, c->get_name (), this, HB_FUNC, \
441    " ")
442 #else
443 #define TRACE_SUBSET(this) hb_no_trace_t<bool> trace
444 #endif
445
446 #ifndef HB_DEBUG_DISPATCH
447 #define HB_DEBUG_DISPATCH ( \
448         HB_DEBUG_APPLY + \
449         HB_DEBUG_SANITIZE + \
450         HB_DEBUG_SERIALIZE + \
451         HB_DEBUG_SUBSET + \
452         0)
453 #endif
454 #if HB_DEBUG_DISPATCH
455 #define TRACE_DISPATCH(this, format) \
456         hb_auto_trace_t<context_t::max_debug_depth, typename context_t::return_t> trace \
457         (&c->debug_depth, c->get_name (), this, HB_FUNC, \
458          "format %d", (int) format)
459 #else
460 #define TRACE_DISPATCH(this, format) hb_no_trace_t<typename context_t::return_t> trace
461 #endif
462
463
464 #endif /* HB_DEBUG_HH */