forked from bytecodealliance/wasm-micro-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lldb_wasm.patch
5906 lines (5851 loc) · 217 KB
/
lldb_wasm.patch
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/lldb/bindings/CMakeLists.txt b/lldb/bindings/CMakeLists.txt
index 9759b069fdc4..25b427f8bcf2 100644
--- a/lldb/bindings/CMakeLists.txt
+++ b/lldb/bindings/CMakeLists.txt
@@ -26,8 +26,6 @@ set(SWIG_COMMON_FLAGS
-features autodoc
-I${LLDB_SOURCE_DIR}/include
-I${CMAKE_CURRENT_SOURCE_DIR}
- -D__STDC_LIMIT_MACROS
- -D__STDC_CONSTANT_MACROS
${DARWIN_EXTRAS}
)
diff --git a/lldb/bindings/interfaces.swig b/lldb/bindings/interfaces.swig
index c9a6d0f06056..021c7683d170 100644
--- a/lldb/bindings/interfaces.swig
+++ b/lldb/bindings/interfaces.swig
@@ -1,8 +1,5 @@
/* Various liblldb typedefs that SWIG needs to know about. */
#define __extension__ /* Undefine GCC keyword to make Swig happy when processing glibc's stdint.h. */
-/* The ISO C99 standard specifies that in C++ implementations limit macros such
- as INT32_MAX should only be defined if __STDC_LIMIT_MACROS is. */
-#define __STDC_LIMIT_MACROS
%include "stdint.i"
%include "lldb/lldb-defines.h"
diff --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig
index b1ace4ff3b1e..5f8f4aa678c4 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -439,7 +439,7 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
%typemap(out) lldb::FileSP {
$result = nullptr;
- lldb::FileSP &sp = $1;
+ const lldb::FileSP &sp = $1;
if (sp) {
PythonFile pyfile = unwrapOrSetPythonException(PythonFile::FromFile(*sp));
if (!pyfile.IsValid())
diff --git a/lldb/include/lldb/Breakpoint/Breakpoint.h b/lldb/include/lldb/Breakpoint/Breakpoint.h
index f2e2a0d22784..426d1129bd10 100644
--- a/lldb/include/lldb/Breakpoint/Breakpoint.h
+++ b/lldb/include/lldb/Breakpoint/Breakpoint.h
@@ -9,6 +9,7 @@
#ifndef LLDB_BREAKPOINT_BREAKPOINT_H
#define LLDB_BREAKPOINT_BREAKPOINT_H
+#include <limits>
#include <memory>
#include <string>
#include <unordered_set>
diff --git a/lldb/include/lldb/Core/Module.h b/lldb/include/lldb/Core/Module.h
index dd7100c4616c..97d70daadbdc 100644
--- a/lldb/include/lldb/Core/Module.h
+++ b/lldb/include/lldb/Core/Module.h
@@ -41,6 +41,7 @@
namespace lldb_private {
class CompilerDeclContext;
+class DWARFEvaluatorFactory;
class Function;
class Log;
class ObjectFile;
@@ -859,6 +860,8 @@ public:
/// Update the ArchSpec to a more specific variant.
bool MergeArchitecture(const ArchSpec &arch_spec);
+ DWARFEvaluatorFactory *GetDWARFExpressionEvaluatorFactory();
+
/// \class LookupInfo Module.h "lldb/Core/Module.h"
/// A class that encapsulates name lookup information.
///
@@ -985,6 +988,8 @@ protected:
m_first_file_changed_log : 1; /// See if the module was modified after it
/// was initially opened.
+ std::unique_ptr<DWARFEvaluatorFactory> m_dwarf_evaluator_factory;
+
/// Resolve a file or load virtual address.
///
/// Tries to resolve \a vm_addr as a file address (if \a
diff --git a/lldb/include/lldb/Core/PluginManager.h b/lldb/include/lldb/Core/PluginManager.h
index be91929c62e1..8d876fc1fa2f 100644
--- a/lldb/include/lldb/Core/PluginManager.h
+++ b/lldb/include/lldb/Core/PluginManager.h
@@ -508,6 +508,17 @@ public:
static bool CreateSettingForStructuredDataPlugin(
Debugger &debugger, const lldb::OptionValuePropertiesSP &properties_sp,
ConstString description, bool is_global_property);
+
+ // DWARFEvaluatorFactory
+ static bool
+ RegisterPlugin(ConstString name, const char *description,
+ DWARFEvaluatorFactoryCreateInstance create_callback);
+
+ static bool
+ UnregisterPlugin(DWARFEvaluatorFactoryCreateInstance create_callback);
+
+ static DWARFEvaluatorFactoryCreateInstance
+ GetDWARFEvaluatorFactoryCreateCallbackAtIndex(uint32_t idx);
};
} // namespace lldb_private
diff --git a/lldb/include/lldb/Expression/DWARFEvaluator.h b/lldb/include/lldb/Expression/DWARFEvaluator.h
new file mode 100644
index 000000000000..6811cbeae3d3
--- /dev/null
+++ b/lldb/include/lldb/Expression/DWARFEvaluator.h
@@ -0,0 +1,110 @@
+//===-- DWARFEvaluator.h ----------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_EXPRESSION_DWARFEVALUATOR_H
+#define LLDB_EXPRESSION_DWARFEVALUATOR_H
+
+#include "lldb/lldb-private.h"
+#include <vector>
+
+namespace lldb_private {
+
+class DWARFExpression;
+
+/// \class DWARFEvaluator DWARFEvaluator.h
+/// "lldb/Expression/DWARFEvaluator.h" Evaluates DWARF opcodes.
+///
+class DWARFEvaluator {
+public:
+ /// Crates a DWARF location expression evaluator
+ ///
+ /// \param[in] dwarf_expression
+ /// The DWARF expression to evaluate.
+ ///
+ /// \param[in] exe_ctx
+ /// The execution context in which to evaluate the location
+ /// expression. The location expression may access the target's
+ /// memory, especially if it comes from the expression parser.
+ ///
+ /// \param[in] reg_ctx
+ /// An optional parameter which provides a RegisterContext for use
+ /// when evaluating the expression (i.e. for fetching register values).
+ /// Normally this will come from the ExecutionContext's StackFrame but
+ /// in the case where an expression needs to be evaluated while building
+ /// the stack frame list, this short-cut is available.
+ ///
+ /// \param[in] initial_value_ptr
+ /// A value to put on top of the interpreter stack before evaluating
+ /// the expression, if the expression is parametrized. Can be NULL.
+ ///
+ /// \param[in] object_address_ptr
+ ///
+ DWARFEvaluator(const DWARFExpression &dwarf_expression,
+ ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
+ const Value *initial_value_ptr,
+ const Value *object_address_ptr);
+
+ /// DWARFEvaluator protocol.
+ /// \{
+
+ /// Evaluate the DWARF location expression
+ ///
+ /// \param[in] result
+ /// A value into which the result of evaluating the expression is
+ /// to be placed.
+ ///
+ /// \param[in] error_ptr
+ /// If non-NULL, used to report errors in expression evaluation.
+ ///
+ /// \return
+ /// True on success; false otherwise. If error_ptr is non-NULL,
+ /// details of the failure are provided through it.
+ virtual bool Evaluate(Value &result, Status *error_ptr);
+
+ /// Evaluate the DWARF location expression with the opcodes specified.
+ ///
+ /// \param[in] opcodes
+ /// The DWARF opcodes to evaluate.
+ ///
+ /// \param[in] result
+ /// A value into which the result of evaluating the expression is
+ /// to be placed.
+ ///
+ /// \param[in] error_ptr
+ /// If non-NULL, used to report errors in expression evaluation.
+ ///
+ /// \return
+ /// True on success; false otherwise. If error_ptr is non-NULL,
+ /// details of the failure are provided through it.
+ virtual bool Evaluate(const DataExtractor &opcodes, Value &result,
+ Status *error_ptr);
+
+ /// Evaluates a specific DWARF opcode in the context of a DWARF expression
+ virtual bool Evaluate(const uint8_t op, Process *process, StackFrame *frame,
+ std::vector<Value> &stack, const DataExtractor &opcodes,
+ lldb::offset_t &offset, Value &pieces,
+ uint64_t &op_piece_offset, Log *log, Status *error_ptr);
+
+ /// \}
+
+protected:
+ const DWARFExpression &m_dwarf_expression;
+ ExecutionContext *m_exe_ctx;
+ RegisterContext *m_reg_ctx;
+ const Value *m_initial_value_ptr;
+ const Value *m_object_address_ptr;
+
+private:
+ DWARFEvaluator(const DWARFEvaluator &);
+ const DWARFEvaluator &operator=(const DWARFEvaluator &) = delete;
+
+};
+
+} // namespace lldb_private
+
+#endif // LLDB_EXPRESSION_DWARFEVALUATOR_H
diff --git a/lldb/include/lldb/Expression/DWARFEvaluatorFactory.h b/lldb/include/lldb/Expression/DWARFEvaluatorFactory.h
new file mode 100644
index 000000000000..f3b496c580e4
--- /dev/null
+++ b/lldb/include/lldb/Expression/DWARFEvaluatorFactory.h
@@ -0,0 +1,56 @@
+//===-- DWARFEvaluatorFactory.h ---------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_EXPRESSION_DWARFEVALUATORFACTORY_H
+#define LLDB_EXPRESSION_DWARFEVALUATORFACTORY_H
+
+#include "lldb/Core/PluginInterface.h"
+#include "lldb/Utility/ConstString.h"
+#include "lldb/lldb-private.h"
+
+class DWARFUnit;
+
+namespace lldb_private {
+
+class DWARFEvaluator;
+class DWARFExpression;
+
+/// \class DWARFEvaluatorFactory DWARFEvaluatorFactory.h
+/// "lldb/Expression/DWARFEvaluatorFactory.h" Factory class that allows the
+/// registration of platform-specific DWARF expression evaluators, used to
+/// handle platform-specific DWARF opcodes.
+class DWARFEvaluatorFactory : public PluginInterface {
+public:
+ static std::unique_ptr<DWARFEvaluatorFactory> FindPlugin(Module *module);
+
+ /// PluginInterface protocol.
+ /// \{
+ ConstString GetPluginName() override;
+
+ uint32_t GetPluginVersion() override { return 1; }
+ /// \}
+
+ DWARFEvaluatorFactory() {}
+
+ /// DWARFEvaluatorFactory protocol.
+ /// \{
+ virtual std::unique_ptr<DWARFEvaluator>
+ CreateDWARFEvaluator(const DWARFExpression &dwarf_expression,
+ ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
+ const Value *initial_value_ptr,
+ const Value *object_address_ptr);
+ /// \}
+
+private:
+ DWARFEvaluatorFactory(const DWARFEvaluatorFactory &);
+ const DWARFEvaluatorFactory &operator=(const DWARFEvaluatorFactory &) = delete;
+};
+
+} // namespace lldb_private
+
+#endif // LLDB_EXPRESSION_DWARFEVALUATORFACTORY_H
diff --git a/lldb/include/lldb/Expression/DWARFExpression.h b/lldb/include/lldb/Expression/DWARFExpression.h
index 1490ac2d614a..35c741d4e6ba 100644
--- a/lldb/include/lldb/Expression/DWARFExpression.h
+++ b/lldb/include/lldb/Expression/DWARFExpression.h
@@ -120,6 +120,10 @@ public:
void SetModule(const lldb::ModuleSP &module) { m_module_wp = module; }
+ lldb::ModuleSP GetModule() const { return m_module_wp.lock(); }
+
+ const DWARFUnit *GetDWARFCompileUnit() const { return m_dwarf_cu; }
+
bool ContainsThreadLocalStorage() const;
bool LinkThreadLocalStorage(
@@ -140,7 +144,7 @@ public:
lldb::addr_t func_file_addr);
/// Return the call-frame-info style register kind
- int GetRegisterKind();
+ lldb::RegisterKind GetRegisterKind() const;
/// Set the call-frame-info style register kind
///
@@ -219,6 +223,9 @@ public:
bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op);
+ static lldb::addr_t ReadAddressFromDebugAddrSection(const DWARFUnit *dwarf_cu,
+ uint32_t index);
+
llvm::Optional<DataExtractor>
GetLocationExpression(lldb::addr_t load_function_start,
lldb::addr_t addr) const;
diff --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h
index aaa2470d2931..c15f2db52fbc 100644
--- a/lldb/include/lldb/Target/Process.h
+++ b/lldb/include/lldb/Target/Process.h
@@ -1434,7 +1434,7 @@ public:
/// vm_addr, \a buf, and \a size updated appropriately. Zero is
/// returned in the case of an error.
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size,
- Status &error);
+ Status &error, ExecutionContext *exe_ctx = nullptr);
/// Read of memory from a process.
///
diff --git a/lldb/include/lldb/Target/ProcessTrace.h b/lldb/include/lldb/Target/ProcessTrace.h
index 7b9d6b13dd6f..9525fc9750fd 100644
--- a/lldb/include/lldb/Target/ProcessTrace.h
+++ b/lldb/include/lldb/Target/ProcessTrace.h
@@ -59,7 +59,7 @@ public:
bool WarnBeforeDetach() const override { return false; }
size_t ReadMemory(lldb::addr_t addr, void *buf, size_t size,
- Status &error) override;
+ Status &error, ExecutionContext *exe_ctx = nullptr) override;
size_t DoReadMemory(lldb::addr_t addr, void *buf, size_t size,
Status &error) override;
diff --git a/lldb/include/lldb/lldb-forward.h b/lldb/include/lldb/lldb-forward.h
index ad5298151e4a..5a3c0b27a738 100644
--- a/lldb/include/lldb/lldb-forward.h
+++ b/lldb/include/lldb/lldb-forward.h
@@ -74,6 +74,7 @@ class Disassembler;
class DumpValueObjectOptions;
class DynamicCheckerFunctions;
class DynamicLoader;
+class DWARFEvaluatorFactory;
class Editline;
class EmulateInstruction;
class Environment;
diff --git a/lldb/include/lldb/lldb-private-interfaces.h b/lldb/include/lldb/lldb-private-interfaces.h
index 2ed083ec8ae9..f4d500d198e8 100644
--- a/lldb/include/lldb/lldb-private-interfaces.h
+++ b/lldb/include/lldb/lldb-private-interfaces.h
@@ -113,6 +113,8 @@ typedef lldb::REPLSP (*REPLCreateInstance)(Status &error,
const char *repl_options);
typedef int (*ComparisonFunction)(const void *, const void *);
typedef void (*DebuggerInitializeCallback)(Debugger &debugger);
+typedef DWARFEvaluatorFactory *(*DWARFEvaluatorFactoryCreateInstance)(
+ Module *module);
/// Trace
/// \{
typedef llvm::Expected<lldb::TraceSP> (*TraceCreateInstanceForSessionFile)(
diff --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp
index 19c97be15066..1647f93ec4f3 100644
--- a/lldb/source/Core/Module.cpp
+++ b/lldb/source/Core/Module.cpp
@@ -16,6 +16,7 @@
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/SearchFilter.h"
#include "lldb/Core/Section.h"
+#include "lldb/Expression/DWARFEvaluatorFactory.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
@@ -1659,3 +1660,9 @@ bool Module::GetIsDynamicLinkEditor() {
return false;
}
+
+DWARFEvaluatorFactory *Module::GetDWARFExpressionEvaluatorFactory() {
+ if (!m_dwarf_evaluator_factory)
+ m_dwarf_evaluator_factory = DWARFEvaluatorFactory::FindPlugin(this);
+ return m_dwarf_evaluator_factory.get();
+}
diff --git a/lldb/source/Core/PluginManager.cpp b/lldb/source/Core/PluginManager.cpp
index fcaa868b083e..59a404d4a7e1 100644
--- a/lldb/source/Core/PluginManager.cpp
+++ b/lldb/source/Core/PluginManager.cpp
@@ -1597,3 +1597,32 @@ bool PluginManager::CreateSettingForStructuredDataPlugin(
ConstString("Settings for structured data plug-ins"), properties_sp,
description, is_global_property);
}
+
+#pragma mark DWARFEvaluator
+
+typedef PluginInstance<DWARFEvaluatorFactoryCreateInstance>
+ DWARFEvaluatorFactoryInstance;
+typedef PluginInstances<DWARFEvaluatorFactoryInstance>
+ DWARFEvaluatorFactoryInstances;
+
+static DWARFEvaluatorFactoryInstances &GetDWARFEvaluatorFactoryInstances() {
+ static DWARFEvaluatorFactoryInstances g_instances;
+ return g_instances;
+}
+
+bool PluginManager::RegisterPlugin(
+ ConstString name, const char *description,
+ DWARFEvaluatorFactoryCreateInstance create_callback) {
+ return GetDWARFEvaluatorFactoryInstances().RegisterPlugin(name, description,
+ create_callback);
+}
+
+bool PluginManager::UnregisterPlugin(
+ DWARFEvaluatorFactoryCreateInstance create_callback) {
+ return GetDWARFEvaluatorFactoryInstances().UnregisterPlugin(create_callback);
+}
+
+DWARFEvaluatorFactoryCreateInstance
+PluginManager::GetDWARFEvaluatorFactoryCreateCallbackAtIndex(uint32_t idx) {
+ return GetDWARFEvaluatorFactoryInstances().GetCallbackAtIndex(idx);
+}
diff --git a/lldb/source/Core/Value.cpp b/lldb/source/Core/Value.cpp
index fb57c0fedf04..f92d6a54de94 100644
--- a/lldb/source/Core/Value.cpp
+++ b/lldb/source/Core/Value.cpp
@@ -538,7 +538,7 @@ Status Value::GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data,
if (process) {
const size_t bytes_read =
- process->ReadMemory(address, dst, byte_size, error);
+ process->ReadMemory(address, dst, byte_size, error, exe_ctx);
if (bytes_read != byte_size)
error.SetErrorStringWithFormat(
"read memory from 0x%" PRIx64 " failed (%u of %u bytes read)",
diff --git a/lldb/source/Core/ValueObject.cpp b/lldb/source/Core/ValueObject.cpp
index 9c1ba99da1d0..b15b214b2a2f 100644
--- a/lldb/source/Core/ValueObject.cpp
+++ b/lldb/source/Core/ValueObject.cpp
@@ -735,7 +735,7 @@ size_t ValueObject::GetPointeeData(DataExtractor &data, uint32_t item_idx,
if (process) {
heap_buf_ptr->SetByteSize(bytes);
size_t bytes_read = process->ReadMemory(
- addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
+ addr + offset, heap_buf_ptr->GetBytes(), bytes, error, &exe_ctx);
if (error.Success() || bytes_read > 0) {
data.SetData(data_sp);
return bytes_read;
diff --git a/lldb/source/Expression/CMakeLists.txt b/lldb/source/Expression/CMakeLists.txt
index bf94361dd6c1..4e76d547aeaf 100644
--- a/lldb/source/Expression/CMakeLists.txt
+++ b/lldb/source/Expression/CMakeLists.txt
@@ -1,5 +1,7 @@
add_lldb_library(lldbExpression
DiagnosticManager.cpp
+ DWARFEvaluator.cpp
+ DWARFEvaluatorFactory.cpp
DWARFExpression.cpp
Expression.cpp
ExpressionVariable.cpp
diff --git a/lldb/source/Expression/DWARFEvaluator.cpp b/lldb/source/Expression/DWARFEvaluator.cpp
new file mode 100644
index 000000000000..06107e136197
--- /dev/null
+++ b/lldb/source/Expression/DWARFEvaluator.cpp
@@ -0,0 +1,1952 @@
+//===-- DWARFEvaluator.cpp ------------ -----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Expression/DWARFEvaluator.h"
+#include "lldb/Expression/DWARFExpression.h"
+
+#include "lldb/Core/Module.h"
+#include "lldb/Core/Value.h"
+#include "lldb/Core/dwarf.h"
+
+#include "lldb/Utility/Log.h"
+#include "lldb/Utility/RegisterValue.h"
+
+#include "lldb/Target/Process.h"
+#include "lldb/Target/RegisterContext.h"
+#include "lldb/Target/StackFrame.h"
+
+#include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+DWARFEvaluator::DWARFEvaluator(const DWARFExpression &dwarf_expression,
+ ExecutionContext *exe_ctx,
+ RegisterContext *reg_ctx,
+ const Value *initial_value_ptr,
+ const Value *object_address_ptr)
+ : m_dwarf_expression(dwarf_expression), m_exe_ctx(exe_ctx),
+ m_reg_ctx(reg_ctx), m_initial_value_ptr(initial_value_ptr),
+ m_object_address_ptr(object_address_ptr) {}
+
+static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
+ lldb::RegisterKind reg_kind,
+ uint32_t reg_num, Status *error_ptr,
+ Value &value) {
+ if (reg_ctx == nullptr) {
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
+ } else {
+ uint32_t native_reg =
+ reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
+ if (native_reg == LLDB_INVALID_REGNUM) {
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat("Unable to convert register "
+ "kind=%u reg_num=%u to a native "
+ "register number.\n",
+ reg_kind, reg_num);
+ } else {
+ const RegisterInfo *reg_info =
+ reg_ctx->GetRegisterInfoAtIndex(native_reg);
+ RegisterValue reg_value;
+ if (reg_ctx->ReadRegister(reg_info, reg_value)) {
+ if (reg_value.GetScalarValue(value.GetScalar())) {
+ value.SetValueType(Value::ValueType::Scalar);
+ value.SetContext(Value::ContextType::RegisterInfo,
+ const_cast<RegisterInfo *>(reg_info));
+ if (error_ptr)
+ error_ptr->Clear();
+ return true;
+ } else {
+ // If we get this error, then we need to implement a value buffer in
+ // the dwarf expression evaluation function...
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat(
+ "register %s can't be converted to a scalar value",
+ reg_info->name);
+ }
+ } else {
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat("register %s is not available",
+ reg_info->name);
+ }
+ }
+ }
+ return false;
+}
+
+static bool Evaluate_DW_OP_entry_value(std::vector<Value> &stack,
+ ExecutionContext *exe_ctx,
+ RegisterContext *reg_ctx,
+ const DataExtractor &opcodes,
+ lldb::offset_t &opcode_offset,
+ Status *error_ptr, Log *log) {
+ // DW_OP_entry_value(sub-expr) describes the location a variable had upon
+ // function entry: this variable location is presumed to be optimized out at
+ // the current PC value. The caller of the function may have call site
+ // information that describes an alternate location for the variable (e.g. a
+ // constant literal, or a spilled stack value) in the parent frame.
+ //
+ // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
+ //
+ // void child(int &sink, int x) {
+ // ...
+ // /* "x" gets optimized out. */
+ //
+ // /* The location of "x" here is: DW_OP_entry_value($reg2). */
+ // ++sink;
+ // }
+ //
+ // void parent() {
+ // int sink;
+ //
+ // /*
+ // * The callsite information emitted here is:
+ // *
+ // * DW_TAG_call_site
+ // * DW_AT_return_pc ... (for "child(sink, 123);")
+ // * DW_TAG_call_site_parameter (for "sink")
+ // * DW_AT_location ($reg1)
+ // * DW_AT_call_value ($SP - 8)
+ // * DW_TAG_call_site_parameter (for "x")
+ // * DW_AT_location ($reg2)
+ // * DW_AT_call_value ($literal 123)
+ // *
+ // * DW_TAG_call_site
+ // * DW_AT_return_pc ... (for "child(sink, 456);")
+ // * ...
+ // */
+ // child(sink, 123);
+ // child(sink, 456);
+ // }
+ //
+ // When the program stops at "++sink" within `child`, the debugger determines
+ // the call site by analyzing the return address. Once the call site is found,
+ // the debugger determines which parameter is referenced by DW_OP_entry_value
+ // and evaluates the corresponding location for that parameter in `parent`.
+
+ // 1. Find the function which pushed the current frame onto the stack.
+ if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) {
+ LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no exe/reg context");
+ return false;
+ }
+
+ StackFrame *current_frame = exe_ctx->GetFramePtr();
+ Thread *thread = exe_ctx->GetThreadPtr();
+ if (!current_frame || !thread) {
+ LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current frame/thread");
+ return false;
+ }
+
+ Target &target = exe_ctx->GetTargetRef();
+ StackFrameSP parent_frame = nullptr;
+ addr_t return_pc = LLDB_INVALID_ADDRESS;
+ uint32_t current_frame_idx = current_frame->GetFrameIndex();
+ uint32_t num_frames = thread->GetStackFrameCount();
+ for (uint32_t parent_frame_idx = current_frame_idx + 1;
+ parent_frame_idx < num_frames; ++parent_frame_idx) {
+ parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx);
+ // Require a valid sequence of frames.
+ if (!parent_frame)
+ break;
+
+ // Record the first valid return address, even if this is an inlined frame,
+ // in order to look up the associated call edge in the first non-inlined
+ // parent frame.
+ if (return_pc == LLDB_INVALID_ADDRESS) {
+ return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
+ LLDB_LOG(log,
+ "Evaluate_DW_OP_entry_value: immediate ancestor with pc = {0:x}",
+ return_pc);
+ }
+
+ // If we've found an inlined frame, skip it (these have no call site
+ // parameters).
+ if (parent_frame->IsInlined())
+ continue;
+
+ // We've found the first non-inlined parent frame.
+ break;
+ }
+ if (!parent_frame || !parent_frame->GetRegisterContext()) {
+ LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent frame with reg ctx");
+ return false;
+ }
+
+ Function *parent_func =
+ parent_frame->GetSymbolContext(eSymbolContextFunction).function;
+ if (!parent_func) {
+ LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent function");
+ return false;
+ }
+
+ // 2. Find the call edge in the parent function responsible for creating the
+ // current activation.
+ Function *current_func =
+ current_frame->GetSymbolContext(eSymbolContextFunction).function;
+ if (!current_func) {
+ LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current function");
+ return false;
+ }
+
+ CallEdge *call_edge = nullptr;
+ ModuleList &modlist = target.GetImages();
+ ExecutionContext parent_exe_ctx = *exe_ctx;
+ parent_exe_ctx.SetFrameSP(parent_frame);
+ if (!parent_frame->IsArtificial()) {
+ // If the parent frame is not artificial, the current activation may be
+ // produced by an ambiguous tail call. In this case, refuse to proceed.
+ call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target);
+ if (!call_edge) {
+ LLDB_LOG(log,
+ "Evaluate_DW_OP_entry_value: no call edge for retn-pc = {0:x} "
+ "in parent frame {1}",
+ return_pc, parent_func->GetName());
+ return false;
+ }
+ Function *callee_func = call_edge->GetCallee(modlist, parent_exe_ctx);
+ if (callee_func != current_func) {
+ LLDB_LOG(log, "Evaluate_DW_OP_entry_value: ambiguous call sequence, "
+ "can't find real parent frame");
+ return false;
+ }
+ } else {
+ // The StackFrameList solver machinery has deduced that an unambiguous tail
+ // call sequence that produced the current activation. The first edge in
+ // the parent that points to the current function must be valid.
+ for (auto &edge : parent_func->GetTailCallingEdges()) {
+ if (edge->GetCallee(modlist, parent_exe_ctx) == current_func) {
+ call_edge = edge.get();
+ break;
+ }
+ }
+ }
+ if (!call_edge) {
+ LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no unambiguous edge from parent "
+ "to current function");
+ return false;
+ }
+
+ // 3. Attempt to locate the DW_OP_entry_value expression in the set of
+ // available call site parameters. If found, evaluate the corresponding
+ // parameter in the context of the parent frame.
+ const uint32_t subexpr_len = opcodes.GetULEB128(&opcode_offset);
+ const void *subexpr_data = opcodes.GetData(&opcode_offset, subexpr_len);
+ if (!subexpr_data) {
+ LLDB_LOG(log, "Evaluate_DW_OP_entry_value: subexpr could not be read");
+ return false;
+ }
+
+ const CallSiteParameter *matched_param = nullptr;
+ for (const CallSiteParameter ¶m : call_edge->GetCallSiteParameters()) {
+ DataExtractor param_subexpr_extractor;
+ if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor))
+ continue;
+ lldb::offset_t param_subexpr_offset = 0;
+ const void *param_subexpr_data =
+ param_subexpr_extractor.GetData(¶m_subexpr_offset, subexpr_len);
+ if (!param_subexpr_data ||
+ param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0)
+ continue;
+
+ // At this point, the DW_OP_entry_value sub-expression and the callee-side
+ // expression in the call site parameter are known to have the same length.
+ // Check whether they are equal.
+ //
+ // Note that an equality check is sufficient: the contents of the
+ // DW_OP_entry_value subexpression are only used to identify the right call
+ // site parameter in the parent, and do not require any special handling.
+ if (memcmp(subexpr_data, param_subexpr_data, subexpr_len) == 0) {
+ matched_param = ¶m;
+ break;
+ }
+ }
+ if (!matched_param) {
+ LLDB_LOG(log,
+ "Evaluate_DW_OP_entry_value: no matching call site param found");
+ return false;
+ }
+
+ // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
+ // subexpresion whenever llvm does.
+ Value result;
+ const DWARFExpression ¶m_expr = matched_param->LocationInCaller;
+ if (!param_expr.Evaluate(&parent_exe_ctx,
+ parent_frame->GetRegisterContext().get(),
+ /*loclist_base_addr=*/LLDB_INVALID_ADDRESS,
+ /*initial_value_ptr=*/nullptr,
+ /*object_address_ptr=*/nullptr, result, error_ptr)) {
+ LLDB_LOG(log,
+ "Evaluate_DW_OP_entry_value: call site param evaluation failed");
+ return false;
+ }
+
+ stack.push_back(result);
+ return true;
+}
+
+bool DWARFEvaluator::Evaluate(Value &result, Status *error_ptr) {
+ DataExtractor opcodes;
+ if (!m_dwarf_expression.GetExpressionData(opcodes)) {
+ if (error_ptr)
+ error_ptr->SetErrorString(
+ "no location, value may have been optimized out");
+ return false;
+ }
+ return Evaluate(opcodes, result, error_ptr);
+}
+
+bool DWARFEvaluator::Evaluate(const DataExtractor &opcodes, Value &result,
+ Status *error_ptr) {
+ if (opcodes.GetByteSize() == 0) {
+ if (error_ptr)
+ error_ptr->SetErrorString(
+ "no location, value may have been optimized out");
+ return false;
+ }
+ std::vector<Value> stack;
+
+ Process *process = nullptr;
+ StackFrame *frame = nullptr;
+
+ if (m_exe_ctx) {
+ process = m_exe_ctx->GetProcessPtr();
+ frame = m_exe_ctx->GetFramePtr();
+ }
+ if (m_reg_ctx == nullptr && frame)
+ m_reg_ctx = frame->GetRegisterContext().get();
+
+ if (m_initial_value_ptr)
+ stack.push_back(*m_initial_value_ptr);
+
+ lldb::offset_t offset = 0;
+
+ /// Insertion point for evaluating multi-piece expression.
+ uint64_t op_piece_offset = 0;
+ Value pieces; // Used for DW_OP_piece
+
+ Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
+
+ uint8_t _opcode = 0;
+
+ while (opcodes.ValidOffset(offset)) {
+ const lldb::offset_t op_offset = offset;
+ const uint8_t op = opcodes.GetU8(&offset);
+ _opcode = op;
+
+ if (log && log->GetVerbose()) {
+ size_t count = stack.size();
+ LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
+ (uint64_t)count);
+ for (size_t i = 0; i < count; ++i) {
+ StreamString new_value;
+ new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
+ stack[i].Dump(&new_value);
+ LLDB_LOGF(log, " %s", new_value.GetData());
+ }
+ LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
+ DW_OP_value_to_name(op));
+ }
+
+ if (!Evaluate(op, process, frame, stack, opcodes, offset, pieces,
+ op_piece_offset, log, error_ptr))
+ return false;
+ }
+
+ if (stack.empty()) {
+ // Nothing on the stack, check if we created a piece value from DW_OP_piece
+ // or DW_OP_bit_piece opcodes
+ if (pieces.GetBuffer().GetByteSize())
+ result = pieces;
+ else {
+ if (error_ptr)
+ error_ptr->SetErrorString("Stack empty after evaluation.");
+ return false;
+ }
+ } else {
+ if (log && log->GetVerbose()) {
+ size_t count = stack.size();
+ LLDB_LOGF(log, "Stack after operation has %" PRIu64 " values:",
+ (uint64_t)count);
+ for (size_t i = 0; i < count; ++i) {
+ StreamString new_value;
+ new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
+ stack[i].Dump(&new_value);
+ LLDB_LOGF(log, " %s", new_value.GetData());
+ }
+ }
+ result = stack.back();
+ }
+ return true; // Return true on success
+}
+
+bool DWARFEvaluator::Evaluate(const uint8_t op, Process *process,
+ StackFrame *frame, std::vector<Value> &stack,
+ const DataExtractor &opcodes,
+ lldb::offset_t &offset, Value &pieces,
+ uint64_t &op_piece_offset, Log *log,
+ Status *error_ptr) {
+ Value tmp;
+ uint32_t reg_num;
+
+ lldb::ModuleSP module_sp = m_dwarf_expression.GetModule();
+ const DWARFUnit *dwarf_cu = m_dwarf_expression.GetDWARFCompileUnit();
+ const lldb::RegisterKind reg_kind = m_dwarf_expression.GetRegisterKind();
+
+ switch (op) {
+ // The DW_OP_addr operation has a single operand that encodes a machine
+ // address and whose size is the size of an address on the target machine.
+ case DW_OP_addr:
+ stack.push_back(Scalar(opcodes.GetAddress(&offset)));
+ stack.back().SetValueType(Value::ValueType::FileAddress);
+ // Convert the file address to a load address, so subsequent
+ // DWARF operators can operate on it.
+ if (frame)
+ stack.back().ConvertToLoadAddress(module_sp.get(),
+ frame->CalculateTarget().get());
+ break;
+
+ // The DW_OP_addr_sect_offset4 is used for any location expressions in
+ // shared libraries that have a location like:
+ // DW_OP_addr(0x1000)
+ // If this address resides in a shared library, then this virtual address
+ // won't make sense when it is evaluated in the context of a running
+ // process where shared libraries have been slid. To account for this, this
+ // new address type where we can store the section pointer and a 4 byte
+ // offset.
+ // case DW_OP_addr_sect_offset4:
+ // {
+ // result_type = eResultTypeFileAddress;
+ // lldb::Section *sect = (lldb::Section
+ // *)opcodes.GetMaxU64(&offset, sizeof(void *));
+ // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
+ //
+ // Address so_addr (sect, sect_offset);
+ // lldb::addr_t load_addr = so_addr.GetLoadAddress();
+ // if (load_addr != LLDB_INVALID_ADDRESS)
+ // {
+ // // We successfully resolve a file address to a load
+ // // address.
+ // stack.push_back(load_addr);
+ // break;
+ // }
+ // else
+ // {
+ // // We were able
+ // if (error_ptr)
+ // error_ptr->SetErrorStringWithFormat ("Section %s in
+ // %s is not currently loaded.\n",
+ // sect->GetName().AsCString(),
+ // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
+ // return false;
+ // }
+ // }
+ // break;
+
+ // OPCODE: DW_OP_deref
+ // OPERANDS: none
+ // DESCRIPTION: Pops the top stack entry and treats it as an address.
+ // The value retrieved from that address is pushed. The size of the data
+ // retrieved from the dereferenced address is the size of an address on the
+ // target machine.
+ case DW_OP_deref: {
+ if (stack.empty()) {
+ if (error_ptr)
+ error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
+ return false;
+ }
+ Value::ValueType value_type = stack.back().GetValueType();
+ switch (value_type) {
+ case Value::ValueType::HostAddress: {
+ void *src = (void *)stack.back().GetScalar().ULongLong();
+ intptr_t ptr;
+ ::memcpy(&ptr, src, sizeof(void *));
+ stack.back().GetScalar() = ptr;
+ stack.back().ClearContext();
+ } break;
+ case Value::ValueType::FileAddress: {
+ auto file_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
+ if (!module_sp) {
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat(
+ "need module to resolve file address for DW_OP_deref");
+ return false;
+ }
+ Address so_addr;
+ if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat(
+ "failed to resolve file address in module");
+ return false;
+ }
+ addr_t load_Addr = so_addr.GetLoadAddress(m_exe_ctx->GetTargetPtr());
+ if (load_Addr == LLDB_INVALID_ADDRESS) {
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat("failed to resolve load address");
+ return false;
+ }
+ stack.back().GetScalar() = load_Addr;
+ stack.back().SetValueType(Value::ValueType::LoadAddress);
+ // Fall through to load address code below...
+ }
+ LLVM_FALLTHROUGH;
+ case Value::ValueType::LoadAddress:
+ if (m_exe_ctx) {
+ if (process) {
+ lldb::addr_t pointer_addr =
+ stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
+ Status error;
+ lldb::addr_t pointer_value =
+ process->ReadPointerFromMemory(pointer_addr, error);
+ if (pointer_value != LLDB_INVALID_ADDRESS) {
+ stack.back().GetScalar() = pointer_value;
+ stack.back().ClearContext();
+ } else {
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat(
+ "Failed to dereference pointer from 0x%" PRIx64
+ " for DW_OP_deref: %s\n",
+ pointer_addr, error.AsCString());
+ return false;
+ }
+ } else {
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat(
+ "NULL process for DW_OP_deref.\n");
+ return false;
+ }
+ } else {
+ if (error_ptr)
+ error_ptr->SetErrorStringWithFormat(
+ "NULL execution context for DW_OP_deref.\n");
+ return false;
+ }
+ break;
+
+ default: