aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/DataFormatters/StringPrinter.h
blob: a849c4e011094339dfbfc4b38418f0f275bb2cf3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//===-- StringPrinter.h -----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_StringPrinter_h_
#define liblldb_StringPrinter_h_

// C Includes
// C++ Includes
#include <functional>
#include <string>

// Other libraries and framework includes
// Project includes
#include "lldb/lldb-forward.h"

#include "lldb/Core/DataExtractor.h"

namespace lldb_private {
    namespace formatters
    {
        class StringPrinter
        {
        public:
            enum class StringElementType
            {
                ASCII,
                UTF8,
                UTF16,
                UTF32
            };
            
            enum class GetPrintableElementType
            {
                ASCII,
                UTF8
            };
            
            class ReadStringAndDumpToStreamOptions
            {
            public:
                ReadStringAndDumpToStreamOptions () :
                m_location(0),
                m_process_sp(),
                m_stream(nullptr),
                m_prefix_token(),
                m_suffix_token(),
                m_quote('"'),
                m_source_size(0),
                m_needs_zero_termination(true),
                m_escape_non_printables(true),
                m_ignore_max_length(false),
                m_zero_is_terminator(true),
                m_language_type(lldb::eLanguageTypeUnknown)
                {
                }
                
                ReadStringAndDumpToStreamOptions (ValueObject& valobj);
                
                ReadStringAndDumpToStreamOptions&
                SetLocation (uint64_t l)
                {
                    m_location = l;
                    return *this;
                }
                
                uint64_t
                GetLocation () const
                {
                    return m_location;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetProcessSP (lldb::ProcessSP p)
                {
                    m_process_sp = p;
                    return *this;
                }
                
                lldb::ProcessSP
                GetProcessSP () const
                {
                    return m_process_sp;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetStream (Stream* s)
                {
                    m_stream = s;
                    return *this;
                }
                
                Stream*
                GetStream () const
                {
                    return m_stream;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetPrefixToken (const std::string& p)
                {
                    m_prefix_token = p;
                    return *this;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetPrefixToken (std::nullptr_t)
                {
                    m_prefix_token.clear();
                    return *this;
                }
                
                const char*
                GetPrefixToken () const
                {
                    return m_prefix_token.c_str();
                }

                ReadStringAndDumpToStreamOptions&
                SetSuffixToken (const std::string& p)
                {
                    m_suffix_token = p;
                    return *this;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetSuffixToken (std::nullptr_t)
                {
                    m_suffix_token.clear();
                    return *this;
                }
                
                const char*
                GetSuffixToken () const
                {
                    return m_suffix_token.c_str();
                }
                
                ReadStringAndDumpToStreamOptions&
                SetQuote (char q)
                {
                    m_quote = q;
                    return *this;
                }
                
                char
                GetQuote () const
                {
                    return m_quote;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetSourceSize (uint32_t s)
                {
                    m_source_size = s;
                    return *this;
                }
                
                uint32_t
                GetSourceSize () const
                {
                    return m_source_size;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetNeedsZeroTermination (bool z)
                {
                    m_needs_zero_termination = z;
                    return *this;
                }
                
                bool
                GetNeedsZeroTermination () const
                {
                    return m_needs_zero_termination;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetBinaryZeroIsTerminator (bool e)
                {
                    m_zero_is_terminator = e;
                    return *this;
                }
                
                bool
                GetBinaryZeroIsTerminator () const
                {
                    return m_zero_is_terminator;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetEscapeNonPrintables (bool e)
                {
                    m_escape_non_printables = e;
                    return *this;
                }
                
                bool
                GetEscapeNonPrintables () const
                {
                    return m_escape_non_printables;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetIgnoreMaxLength (bool e)
                {
                    m_ignore_max_length = e;
                    return *this;
                }
                
                bool
                GetIgnoreMaxLength () const
                {
                    return m_ignore_max_length;
                }
                
                ReadStringAndDumpToStreamOptions&
                SetLanguage (lldb::LanguageType l)
                {
                    m_language_type = l;
                    return *this;
                }
                
                lldb::LanguageType
                GetLanguage () const
                
                {
                    return m_language_type;
                }
                
            private:
                uint64_t m_location;
                lldb::ProcessSP m_process_sp;
                Stream* m_stream;
                std::string m_prefix_token;
                std::string m_suffix_token;
                char m_quote;
                uint32_t m_source_size;
                bool m_needs_zero_termination;
                bool m_escape_non_printables;
                bool m_ignore_max_length;
                bool m_zero_is_terminator;
                lldb::LanguageType m_language_type;
            };
            
            class ReadBufferAndDumpToStreamOptions
            {
            public:
                ReadBufferAndDumpToStreamOptions () :
                m_data(),
                m_stream(nullptr),
                m_prefix_token(),
                m_suffix_token(),
                m_quote('"'),
                m_source_size(0),
                m_escape_non_printables(true),
                m_zero_is_terminator(true),
                m_is_truncated(false),
                m_language_type(lldb::eLanguageTypeUnknown)
                {
                }
                
                ReadBufferAndDumpToStreamOptions (ValueObject& valobj);
                
                ReadBufferAndDumpToStreamOptions (const ReadStringAndDumpToStreamOptions& options);
                
                ReadBufferAndDumpToStreamOptions&
                SetData (DataExtractor d)
                {
                    m_data = d;
                    return *this;
                }
                
                lldb_private::DataExtractor
                GetData () const
                {
                    return m_data;
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetStream (Stream* s)
                {
                    m_stream = s;
                    return *this;
                }
                
                Stream*
                GetStream () const
                {
                    return m_stream;
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetPrefixToken (const std::string& p)
                {
                    m_prefix_token = p;
                    return *this;
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetPrefixToken (std::nullptr_t)
                {
                    m_prefix_token.clear();
                    return *this;
                }
                
                const char*
                GetPrefixToken () const
                {
                    return m_prefix_token.c_str();
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetSuffixToken (const std::string& p)
                {
                    m_suffix_token = p;
                    return *this;
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetSuffixToken (std::nullptr_t)
                {
                    m_suffix_token.clear();
                    return *this;
                }
                
                const char*
                GetSuffixToken () const
                {
                    return m_suffix_token.c_str();
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetQuote (char q)
                {
                    m_quote = q;
                    return *this;
                }
                
                char
                GetQuote () const
                {
                    return m_quote;
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetSourceSize (uint32_t s)
                {
                    m_source_size = s;
                    return *this;
                }
                
                uint32_t
                GetSourceSize () const
                {
                    return m_source_size;
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetEscapeNonPrintables (bool e)
                {
                    m_escape_non_printables = e;
                    return *this;
                }
                
                bool
                GetEscapeNonPrintables () const
                {
                    return m_escape_non_printables;
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetBinaryZeroIsTerminator (bool e)
                {
                    m_zero_is_terminator = e;
                    return *this;
                }
                
                bool
                GetBinaryZeroIsTerminator () const
                {
                    return m_zero_is_terminator;
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetIsTruncated (bool t)
                {
                    m_is_truncated = t;
                    return *this;
                }
                
                bool
                GetIsTruncated () const
                {
                    return m_is_truncated;
                }
                
                ReadBufferAndDumpToStreamOptions&
                SetLanguage (lldb::LanguageType l)
                {
                    m_language_type = l;
                    return *this;
                }
                
                lldb::LanguageType
                GetLanguage () const
                
                {
                    return m_language_type;
                }
                
            private:
                DataExtractor m_data;
                Stream* m_stream;
                std::string m_prefix_token;
                std::string m_suffix_token;
                char m_quote;
                uint32_t m_source_size;
                bool m_escape_non_printables;
                bool m_zero_is_terminator;
                bool m_is_truncated;
                lldb::LanguageType m_language_type;
            };

            // I can't use a std::unique_ptr for this because the Deleter is a template argument there
            // and I want the same type to represent both pointers I want to free and pointers I don't need
            // to free - which is what this class essentially is
            // It's very specialized to the needs of this file, and not suggested for general use
            template <typename T = uint8_t, typename U = char, typename S = size_t>
            struct StringPrinterBufferPointer
            {
            public:
                typedef std::function<void(const T*)> Deleter;
                
                StringPrinterBufferPointer (std::nullptr_t ptr) :
                m_data(nullptr),
                m_size(0),
                m_deleter()
                {}
                
                StringPrinterBufferPointer(const T* bytes, S size, Deleter deleter = nullptr) :
                m_data(bytes),
                m_size(size),
                m_deleter(deleter)
                {}

                StringPrinterBufferPointer(const U* bytes, S size, Deleter deleter = nullptr) :
                m_data(reinterpret_cast<const T*>(bytes)),
                m_size(size),
                m_deleter(deleter)
                {}

                StringPrinterBufferPointer(StringPrinterBufferPointer&& rhs) :
                m_data(rhs.m_data),
                m_size(rhs.m_size),
                m_deleter(rhs.m_deleter)
                {
                    rhs.m_data = nullptr;
                }
                
                StringPrinterBufferPointer(const StringPrinterBufferPointer& rhs) :
                m_data(rhs.m_data),
                m_size(rhs.m_size),
                m_deleter(rhs.m_deleter)
                {
                    rhs.m_data = nullptr; // this is why m_data has to be mutable
                }
                
                ~StringPrinterBufferPointer()
                {
                    if (m_data && m_deleter)
                        m_deleter(m_data);
                    m_data = nullptr;
                }

                const T*
                GetBytes () const
                {
                    return m_data;
                }
                
                const S
                GetSize () const
                {
                    return m_size;
                }
                
                StringPrinterBufferPointer&
                operator = (const StringPrinterBufferPointer& rhs)
                {
                    if (m_data && m_deleter)
                        m_deleter(m_data);
                    m_data = rhs.m_data;
                    m_size = rhs.m_size;
                    m_deleter = rhs.m_deleter;
                    rhs.m_data = nullptr;
                    return *this;
                }
                
            private:
                mutable const T* m_data;
                size_t m_size;
                Deleter m_deleter;
            };
            
            typedef std::function<StringPrinter::StringPrinterBufferPointer<uint8_t,char,size_t>(uint8_t*, uint8_t*, uint8_t*&)> EscapingHelper;
            typedef std::function<EscapingHelper(GetPrintableElementType)> EscapingHelperGenerator;
            
            static EscapingHelper
            GetDefaultEscapingHelper (GetPrintableElementType elem_type);
            
            template <StringElementType element_type>
            static bool
            ReadStringAndDumpToStream (const ReadStringAndDumpToStreamOptions& options);
            
            template <StringElementType element_type>
            static bool
            ReadBufferAndDumpToStream (const ReadBufferAndDumpToStreamOptions& options);
        };
        
    } // namespace formatters
} // namespace lldb_private

#endif // liblldb_StringPrinter_h_