blob: 58c190d275dc717376a46a5723793652e5b40aa8 [file] [log] [blame]
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -04001/*
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
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb.hh"
31#include "hb-atomic.hh"
Behdad Esfahbod491d93b2018-07-10 16:03:31 +020032#include "hb-dsalgs.hh"
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -040033
34
35#ifndef HB_DEBUG
36#define HB_DEBUG 0
37#endif
38
Behdad Esfahbod4bc16ac2018-07-31 21:05:51 -070039
40/*
41 * Global runtime options.
42 */
43
44struct hb_options_t
45{
Behdad Esfahbod38a7a8a2018-10-10 17:44:46 -040046 bool unused : 1; /* In-case sign bit is here. */
47 bool initialized : 1;
48 bool uniscribe_bug_compatible : 1;
49 bool aat : 1;
Behdad Esfahbod4bc16ac2018-07-31 21:05:51 -070050};
51
52union hb_options_union_t {
53 int i;
54 hb_options_t opts;
55};
Behdad Esfahbod606bf572018-09-16 19:33:48 +020056static_assert ((sizeof (hb_atomic_int_t) >= sizeof (hb_options_union_t)), "");
Behdad Esfahbod4bc16ac2018-07-31 21:05:51 -070057
58HB_INTERNAL void
59_hb_options_init (void);
60
61extern HB_INTERNAL hb_atomic_int_t _hb_options;
62
63static inline hb_options_t
64hb_options (void)
65{
66 /* Make a local copy, so we can access bitfield threadsafely. */
67 hb_options_union_t u;
68 u.i = _hb_options.get_relaxed ();
69
70 if (unlikely (!u.i))
Behdad Esfahbod24f1d962018-09-10 18:19:37 +020071 {
Behdad Esfahbod4bc16ac2018-07-31 21:05:51 -070072 _hb_options_init ();
Behdad Esfahbod24f1d962018-09-10 18:19:37 +020073 u.i = _hb_options.get_relaxed ();
74 }
Behdad Esfahbod4bc16ac2018-07-31 21:05:51 -070075
76 return u.opts;
77}
78
79
80/*
81 * Debug output (needs enabling at compile time.)
82 */
83
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -040084static inline bool
85_hb_debug (unsigned int level,
86 unsigned int max_level)
87{
88 return level < max_level;
89}
90
91#define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
92#define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0))
93
94static inline void
95_hb_print_func (const char *func)
96{
97 if (func)
98 {
99 unsigned int func_len = strlen (func);
100 /* Skip "static" */
101 if (0 == strncmp (func, "static ", 7))
102 func += 7;
103 /* Skip "typename" */
104 if (0 == strncmp (func, "typename ", 9))
105 func += 9;
106 /* Skip return type */
107 const char *space = strchr (func, ' ');
108 if (space)
109 func = space + 1;
110 /* Skip parameter list */
111 const char *paren = strchr (func, '(');
112 if (paren)
113 func_len = paren - func;
114 fprintf (stderr, "%.*s", func_len, func);
115 }
116}
117
118template <int max_level> static inline void
119_hb_debug_msg_va (const char *what,
120 const void *obj,
121 const char *func,
122 bool indented,
123 unsigned int level,
124 int level_dir,
125 const char *message,
126 va_list ap) HB_PRINTF_FUNC(7, 0);
127template <int max_level> static inline void
128_hb_debug_msg_va (const char *what,
129 const void *obj,
130 const char *func,
131 bool indented,
132 unsigned int level,
133 int level_dir,
134 const char *message,
135 va_list ap)
136{
137 if (!_hb_debug (level, max_level))
138 return;
139
140 fprintf (stderr, "%-10s", what ? what : "");
141
142 if (obj)
Behdad Esfahbod03b7a222017-12-05 13:12:11 -0800143 fprintf (stderr, "(%*p) ", (unsigned int) (2 * sizeof (void *)), obj);
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400144 else
145 fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
146
147 if (indented) {
148#define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
149#define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
150#define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
151#define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
152#define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
153 static const char bars[] =
154 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
155 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
156 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
157 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
158 VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR;
159 fprintf (stderr, "%2u %s" VRBAR "%s",
160 level,
161 bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars) - 1, (unsigned int) (sizeof (VBAR) - 1) * level),
162 level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR);
163 } else
164 fprintf (stderr, " " VRBAR LBAR);
165
166 _hb_print_func (func);
167
168 if (message)
169 {
170 fprintf (stderr, ": ");
171 vfprintf (stderr, message, ap);
172 }
173
174 fprintf (stderr, "\n");
175}
176template <> inline void
177_hb_debug_msg_va<0> (const char *what HB_UNUSED,
178 const void *obj HB_UNUSED,
179 const char *func HB_UNUSED,
180 bool indented HB_UNUSED,
181 unsigned int level HB_UNUSED,
182 int level_dir HB_UNUSED,
183 const char *message HB_UNUSED,
184 va_list ap HB_UNUSED) {}
185
186template <int max_level> static inline void
187_hb_debug_msg (const char *what,
188 const void *obj,
189 const char *func,
190 bool indented,
191 unsigned int level,
192 int level_dir,
193 const char *message,
194 ...) HB_PRINTF_FUNC(7, 8);
195template <int max_level> static inline void
196_hb_debug_msg (const char *what,
197 const void *obj,
198 const char *func,
199 bool indented,
200 unsigned int level,
201 int level_dir,
202 const char *message,
203 ...)
204{
205 va_list ap;
206 va_start (ap, message);
207 _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap);
208 va_end (ap);
209}
210template <> inline void
211_hb_debug_msg<0> (const char *what HB_UNUSED,
212 const void *obj HB_UNUSED,
213 const char *func HB_UNUSED,
214 bool indented HB_UNUSED,
215 unsigned int level HB_UNUSED,
216 int level_dir HB_UNUSED,
217 const char *message HB_UNUSED,
218 ...) HB_PRINTF_FUNC(7, 8);
219template <> inline void
220_hb_debug_msg<0> (const char *what HB_UNUSED,
221 const void *obj HB_UNUSED,
222 const char *func HB_UNUSED,
223 bool indented HB_UNUSED,
224 unsigned int level HB_UNUSED,
225 int level_dir HB_UNUSED,
226 const char *message HB_UNUSED,
227 ...) {}
228
229#define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), nullptr, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
230#define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), nullptr, false, 0, 0, __VA_ARGS__)
231#define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
232
233
234/*
235 * Printer
236 */
237
238template <typename T>
239struct hb_printer_t {
240 const char *print (const T&) { return "something"; }
241};
242
243template <>
244struct hb_printer_t<bool> {
245 const char *print (bool v) { return v ? "true" : "false"; }
246};
247
248template <>
249struct hb_printer_t<hb_void_t> {
250 const char *print (hb_void_t) { return ""; }
251};
252
253
254/*
255 * Trace
256 */
257
258template <typename T>
259static inline void _hb_warn_no_return (bool returned)
260{
261 if (unlikely (!returned)) {
262 fprintf (stderr, "OUCH, returned with no call to return_trace(). This is a bug, please report.\n");
263 }
264}
265template <>
266/*static*/ inline void _hb_warn_no_return<hb_void_t> (bool returned HB_UNUSED)
267{}
268
269template <int max_level, typename ret_t>
Behdad Esfahbodae2e2b02017-12-14 18:15:14 -0800270struct hb_auto_trace_t
271{
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400272 explicit inline hb_auto_trace_t (unsigned int *plevel_,
273 const char *what_,
274 const void *obj_,
275 const char *func,
276 const char *message,
Behdad Esfahbodfcd6c332017-11-14 14:40:21 -0800277 ...) HB_PRINTF_FUNC(6, 7)
278 : plevel (plevel_), what (what_), obj (obj_), returned (false)
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400279 {
280 if (plevel) ++*plevel;
281
282 va_list ap;
283 va_start (ap, message);
284 _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap);
285 va_end (ap);
286 }
287 inline ~hb_auto_trace_t (void)
288 {
289 _hb_warn_no_return<ret_t> (returned);
290 if (!returned) {
291 _hb_debug_msg<max_level> (what, obj, nullptr, true, plevel ? *plevel : 1, -1, " ");
292 }
293 if (plevel) --*plevel;
294 }
295
296 inline ret_t ret (ret_t v, unsigned int line = 0)
297 {
298 if (unlikely (returned)) {
299 fprintf (stderr, "OUCH, double calls to return_trace(). This is a bug, please report.\n");
300 return v;
301 }
302
303 _hb_debug_msg<max_level> (what, obj, nullptr, true, plevel ? *plevel : 1, -1,
304 "return %s (line %d)",
305 hb_printer_t<ret_t>().print (v), line);
306 if (plevel) --*plevel;
307 plevel = nullptr;
308 returned = true;
309 return v;
310 }
311
312 private:
313 unsigned int *plevel;
314 const char *what;
315 const void *obj;
316 bool returned;
317};
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400318template <typename ret_t> /* Make sure we don't use hb_auto_trace_t when not tracing. */
Behdad Esfahbodae2e2b02017-12-14 18:15:14 -0800319struct hb_auto_trace_t<0, ret_t>
320{
321 explicit inline hb_auto_trace_t (unsigned int *plevel_,
322 const char *what_,
323 const void *obj_,
324 const char *func,
325 const char *message,
326 ...) HB_PRINTF_FUNC(6, 7) {}
327
328 inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; }
329};
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400330
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400331/* For disabled tracing; optimize out everything.
ebraminio7c6937e2017-11-20 14:49:22 -0500332 * https://github.com/harfbuzz/harfbuzz/pull/605 */
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400333template <typename ret_t>
334struct hb_no_trace_t {
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400335 inline ret_t ret (ret_t v, unsigned int line HB_UNUSED = 0) { return v; }
336};
337
338#define return_trace(RET) return trace.ret (RET, __LINE__)
339
340
341/*
342 * Instances.
343 */
344
345#ifndef HB_DEBUG_ARABIC
346#define HB_DEBUG_ARABIC (HB_DEBUG+0)
347#endif
348
349#ifndef HB_DEBUG_BLOB
350#define HB_DEBUG_BLOB (HB_DEBUG+0)
351#endif
352
353#ifndef HB_DEBUG_CORETEXT
354#define HB_DEBUG_CORETEXT (HB_DEBUG+0)
355#endif
356
357#ifndef HB_DEBUG_DIRECTWRITE
358#define HB_DEBUG_DIRECTWRITE (HB_DEBUG+0)
359#endif
360
361#ifndef HB_DEBUG_FT
362#define HB_DEBUG_FT (HB_DEBUG+0)
363#endif
364
365#ifndef HB_DEBUG_GET_COVERAGE
366#define HB_DEBUG_GET_COVERAGE (HB_DEBUG+0)
367#endif
368
369#ifndef HB_DEBUG_OBJECT
370#define HB_DEBUG_OBJECT (HB_DEBUG+0)
371#endif
372
373#ifndef HB_DEBUG_SHAPE_PLAN
374#define HB_DEBUG_SHAPE_PLAN (HB_DEBUG+0)
375#endif
376
377#ifndef HB_DEBUG_UNISCRIBE
378#define HB_DEBUG_UNISCRIBE (HB_DEBUG+0)
379#endif
380
381/*
382 * With tracing.
383 */
384
385#ifndef HB_DEBUG_APPLY
386#define HB_DEBUG_APPLY (HB_DEBUG+0)
387#endif
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400388#if HB_DEBUG_APPLY
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400389#define TRACE_APPLY(this) \
390 hb_auto_trace_t<HB_DEBUG_APPLY, bool> trace \
391 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
392 "idx %d gid %u lookup %d", \
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400393 c->buffer->idx, c->buffer->cur().codepoint, (int) c->lookup_index)
394#else
395#define TRACE_APPLY(this) hb_no_trace_t<bool> trace
396#endif
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400397
398#ifndef HB_DEBUG_CLOSURE
399#define HB_DEBUG_CLOSURE (HB_DEBUG+0)
400#endif
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400401#if HB_DEBUG_CLOSURE
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400402#define TRACE_CLOSURE(this) \
403 hb_auto_trace_t<HB_DEBUG_CLOSURE, hb_void_t> trace \
404 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbodfcd6c332017-11-14 14:40:21 -0800405 " ")
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400406#else
Ebrahim Byagowi48f02042018-04-24 08:11:13 +0430407#define TRACE_CLOSURE(this) hb_no_trace_t<hb_void_t> trace HB_UNUSED
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400408#endif
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400409
410#ifndef HB_DEBUG_COLLECT_GLYPHS
411#define HB_DEBUG_COLLECT_GLYPHS (HB_DEBUG+0)
412#endif
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400413#if HB_DEBUG_COLLECT_GLYPHS
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400414#define TRACE_COLLECT_GLYPHS(this) \
415 hb_auto_trace_t<HB_DEBUG_COLLECT_GLYPHS, hb_void_t> trace \
416 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbodfcd6c332017-11-14 14:40:21 -0800417 " ")
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400418#else
Ebrahim Byagowi48f02042018-04-24 08:11:13 +0430419#define TRACE_COLLECT_GLYPHS(this) hb_no_trace_t<hb_void_t> trace HB_UNUSED
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400420#endif
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400421
422#ifndef HB_DEBUG_SANITIZE
423#define HB_DEBUG_SANITIZE (HB_DEBUG+0)
424#endif
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400425#if HB_DEBUG_SANITIZE
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400426#define TRACE_SANITIZE(this) \
427 hb_auto_trace_t<HB_DEBUG_SANITIZE, bool> trace \
428 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
Behdad Esfahbodfcd6c332017-11-14 14:40:21 -0800429 " ");
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400430#else
431#define TRACE_SANITIZE(this) hb_no_trace_t<bool> trace
432#endif
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400433
434#ifndef HB_DEBUG_SERIALIZE
435#define HB_DEBUG_SERIALIZE (HB_DEBUG+0)
436#endif
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400437#if HB_DEBUG_SERIALIZE
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400438#define TRACE_SERIALIZE(this) \
439 hb_auto_trace_t<HB_DEBUG_SERIALIZE, bool> trace \
440 (&c->debug_depth, "SERIALIZE", c, HB_FUNC, \
Behdad Esfahbodfcd6c332017-11-14 14:40:21 -0800441 " ");
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400442#else
443#define TRACE_SERIALIZE(this) hb_no_trace_t<bool> trace
444#endif
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400445
Rod Sheeter8b80cbb2018-02-07 09:31:31 -0800446#ifndef HB_DEBUG_SUBSET
447#define HB_DEBUG_SUBSET (HB_DEBUG+0)
448#endif
449#if HB_DEBUG_SUBSET
450#define TRACE_SUBSET(this) \
451 hb_auto_trace_t<HB_DEBUG_SUBSET, bool> trace \
452 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
453 " ");
454#else
455#define TRACE_SUBSET(this) hb_no_trace_t<bool> trace
456#endif
457
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400458#ifndef HB_DEBUG_WOULD_APPLY
459#define HB_DEBUG_WOULD_APPLY (HB_DEBUG+0)
460#endif
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400461#if HB_DEBUG_WOULD_APPLY
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400462#define TRACE_WOULD_APPLY(this) \
463 hb_auto_trace_t<HB_DEBUG_WOULD_APPLY, bool> trace \
464 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
465 "%d glyphs", c->len);
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400466#else
467#define TRACE_WOULD_APPLY(this) hb_no_trace_t<bool> trace
468#endif
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400469
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400470#ifndef HB_DEBUG_DISPATCH
471#define HB_DEBUG_DISPATCH ( \
472 HB_DEBUG_APPLY + \
473 HB_DEBUG_CLOSURE + \
474 HB_DEBUG_COLLECT_GLYPHS + \
475 HB_DEBUG_SANITIZE + \
476 HB_DEBUG_SERIALIZE + \
Rod Sheeter8b80cbb2018-02-07 09:31:31 -0800477 HB_DEBUG_SUBSET + \
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400478 HB_DEBUG_WOULD_APPLY + \
479 0)
480#endif
481#if HB_DEBUG_DISPATCH
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400482#define TRACE_DISPATCH(this, format) \
483 hb_auto_trace_t<context_t::max_debug_depth, typename context_t::return_t> trace \
484 (&c->debug_depth, c->get_name (), this, HB_FUNC, \
485 "format %d", (int) format);
Behdad Esfahbod5aad8192017-11-03 17:16:26 -0400486#else
487#define TRACE_DISPATCH(this, format) hb_no_trace_t<typename context_t::return_t> trace
488#endif
Behdad Esfahbod40ec3bb2017-11-03 16:57:30 -0400489
490
491#endif /* HB_DEBUG_HH */