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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "elf_writer_debug.h"
#include <unordered_set>
#include <vector>
#include "base/casts.h"
#include "base/stl_util.h"
#include "compiled_method.h"
#include "driver/compiler_driver.h"
#include "dex_file-inl.h"
#include "dwarf/dedup_vector.h"
#include "dwarf/headers.h"
#include "dwarf/method_debug_info.h"
#include "dwarf/register.h"
#include "elf_builder.h"
#include "oat_writer.h"
#include "utils.h"
#include "stack_map.h"
namespace art {
namespace dwarf {
static Reg GetDwarfCoreReg(InstructionSet isa, int machine_reg) {
switch (isa) {
case kArm:
case kThumb2:
return Reg::ArmCore(machine_reg);
case kArm64:
return Reg::Arm64Core(machine_reg);
case kX86:
return Reg::X86Core(machine_reg);
case kX86_64:
return Reg::X86_64Core(machine_reg);
case kMips:
return Reg::MipsCore(machine_reg);
case kMips64:
return Reg::Mips64Core(machine_reg);
default:
LOG(FATAL) << "Unknown instruction set: " << isa;
UNREACHABLE();
}
}
static Reg GetDwarfFpReg(InstructionSet isa, int machine_reg) {
switch (isa) {
case kArm:
case kThumb2:
return Reg::ArmFp(machine_reg);
case kArm64:
return Reg::Arm64Fp(machine_reg);
case kX86:
return Reg::X86Fp(machine_reg);
case kX86_64:
return Reg::X86_64Fp(machine_reg);
default:
LOG(FATAL) << "Unknown instruction set: " << isa;
UNREACHABLE();
}
}
static void WriteCIE(InstructionSet isa,
CFIFormat format,
std::vector<uint8_t>* buffer) {
// Scratch registers should be marked as undefined. This tells the
// debugger that its value in the previous frame is not recoverable.
bool is64bit = Is64BitInstructionSet(isa);
switch (isa) {
case kArm:
case kThumb2: {
DebugFrameOpCodeWriter<> opcodes;
opcodes.DefCFA(Reg::ArmCore(13), 0); // R13(SP).
// core registers.
for (int reg = 0; reg < 13; reg++) {
if (reg < 4 || reg == 12) {
opcodes.Undefined(Reg::ArmCore(reg));
} else {
opcodes.SameValue(Reg::ArmCore(reg));
}
}
// fp registers.
for (int reg = 0; reg < 32; reg++) {
if (reg < 16) {
opcodes.Undefined(Reg::ArmFp(reg));
} else {
opcodes.SameValue(Reg::ArmFp(reg));
}
}
auto return_reg = Reg::ArmCore(14); // R14(LR).
WriteCIE(is64bit, return_reg, opcodes, format, buffer);
return;
}
case kArm64: {
DebugFrameOpCodeWriter<> opcodes;
opcodes.DefCFA(Reg::Arm64Core(31), 0); // R31(SP).
// core registers.
for (int reg = 0; reg < 30; reg++) {
if (reg < 8 || reg == 16 || reg == 17) {
opcodes.Undefined(Reg::Arm64Core(reg));
} else {
opcodes.SameValue(Reg::Arm64Core(reg));
}
}
// fp registers.
for (int reg = 0; reg < 32; reg++) {
if (reg < 8 || reg >= 16) {
opcodes.Undefined(Reg::Arm64Fp(reg));
} else {
opcodes.SameValue(Reg::Arm64Fp(reg));
}
}
auto return_reg = Reg::Arm64Core(30); // R30(LR).
WriteCIE(is64bit, return_reg, opcodes, format, buffer);
return;
}
case kMips:
case kMips64: {
DebugFrameOpCodeWriter<> opcodes;
opcodes.DefCFA(Reg::MipsCore(29), 0); // R29(SP).
// core registers.
for (int reg = 1; reg < 26; reg++) {
if (reg < 16 || reg == 24 || reg == 25) { // AT, V*, A*, T*.
opcodes.Undefined(Reg::MipsCore(reg));
} else {
opcodes.SameValue(Reg::MipsCore(reg));
}
}
auto return_reg = Reg::MipsCore(31); // R31(RA).
WriteCIE(is64bit, return_reg, opcodes, format, buffer);
return;
}
case kX86: {
// FIXME: Add fp registers once libunwind adds support for them. Bug: 20491296
constexpr bool generate_opcodes_for_x86_fp = false;
DebugFrameOpCodeWriter<> opcodes;
opcodes.DefCFA(Reg::X86Core(4), 4); // R4(ESP).
opcodes.Offset(Reg::X86Core(8), -4); // R8(EIP).
// core registers.
for (int reg = 0; reg < 8; reg++) {
if (reg <= 3) {
opcodes.Undefined(Reg::X86Core(reg));
} else if (reg == 4) {
// Stack pointer.
} else {
opcodes.SameValue(Reg::X86Core(reg));
}
}
// fp registers.
if (generate_opcodes_for_x86_fp) {
for (int reg = 0; reg < 8; reg++) {
opcodes.Undefined(Reg::X86Fp(reg));
}
}
auto return_reg = Reg::X86Core(8); // R8(EIP).
WriteCIE(is64bit, return_reg, opcodes, format, buffer);
return;
}
case kX86_64: {
DebugFrameOpCodeWriter<> opcodes;
opcodes.DefCFA(Reg::X86_64Core(4), 8); // R4(RSP).
opcodes.Offset(Reg::X86_64Core(16), -8); // R16(RIP).
// core registers.
for (int reg = 0; reg < 16; reg++) {
if (reg == 4) {
// Stack pointer.
} else if (reg < 12 && reg != 3 && reg != 5) { // except EBX and EBP.
opcodes.Undefined(Reg::X86_64Core(reg));
} else {
opcodes.SameValue(Reg::X86_64Core(reg));
}
}
// fp registers.
for (int reg = 0; reg < 16; reg++) {
if (reg < 12) {
opcodes.Undefined(Reg::X86_64Fp(reg));
} else {
opcodes.SameValue(Reg::X86_64Fp(reg));
}
}
auto return_reg = Reg::X86_64Core(16); // R16(RIP).
WriteCIE(is64bit, return_reg, opcodes, format, buffer);
return;
}
case kNone:
break;
}
LOG(FATAL) << "Can not write CIE frame for ISA " << isa;
UNREACHABLE();
}
template<typename ElfTypes>
void WriteCFISection(ElfBuilder<ElfTypes>* builder,
const ArrayRef<const MethodDebugInfo>& method_infos,
CFIFormat format) {
CHECK(format == dwarf::DW_DEBUG_FRAME_FORMAT ||
format == dwarf::DW_EH_FRAME_FORMAT);
typedef typename ElfTypes::Addr Elf_Addr;
std::vector<uint32_t> binary_search_table;
std::vector<uintptr_t> patch_locations;
if (format == DW_EH_FRAME_FORMAT) {
binary_search_table.reserve(2 * method_infos.size());
} else {
patch_locations.reserve(method_infos.size());
}
// Write .eh_frame/.debug_frame section.
auto* cfi_section = (format == dwarf::DW_DEBUG_FRAME_FORMAT
? builder->GetDebugFrame()
: builder->GetEhFrame());
{
cfi_section->Start();
const bool is64bit = Is64BitInstructionSet(builder->GetIsa());
const Elf_Addr text_address = builder->GetText()->GetAddress();
const Elf_Addr cfi_address = cfi_section->GetAddress();
const Elf_Addr cie_address = cfi_address;
Elf_Addr buffer_address = cfi_address;
std::vector<uint8_t> buffer; // Small temporary buffer.
WriteCIE(builder->GetIsa(), format, &buffer);
cfi_section->WriteFully(buffer.data(), buffer.size());
buffer_address += buffer.size();
buffer.clear();
for (const MethodDebugInfo& mi : method_infos) {
if (!mi.deduped_) { // Only one FDE per unique address.
ArrayRef<const uint8_t> opcodes = mi.compiled_method_->GetCFIInfo();
if (!opcodes.empty()) {
const Elf_Addr code_address = text_address + mi.low_pc_;
if (format == DW_EH_FRAME_FORMAT) {
binary_search_table.push_back(
dchecked_integral_cast<uint32_t>(code_address));
binary_search_table.push_back(
dchecked_integral_cast<uint32_t>(buffer_address));
}
WriteFDE(is64bit, cfi_address, cie_address,
code_address, mi.high_pc_ - mi.low_pc_,
opcodes, format, buffer_address, &buffer,
&patch_locations);
cfi_section->WriteFully(buffer.data(), buffer.size());
buffer_address += buffer.size();
buffer.clear();
}
}
}
cfi_section->End();
}
if (format == DW_EH_FRAME_FORMAT) {
auto* header_section = builder->GetEhFrameHdr();
header_section->Start();
uint32_t header_address = dchecked_integral_cast<int32_t>(header_section->GetAddress());
// Write .eh_frame_hdr section.
std::vector<uint8_t> buffer;
Writer<> header(&buffer);
header.PushUint8(1); // Version.
// Encoding of .eh_frame pointer - libunwind does not honor datarel here,
// so we have to use pcrel which means relative to the pointer's location.
header.PushUint8(DW_EH_PE_pcrel | DW_EH_PE_sdata4);
// Encoding of binary search table size.
header.PushUint8(DW_EH_PE_udata4);
// Encoding of binary search table addresses - libunwind supports only this
// specific combination, which means relative to the start of .eh_frame_hdr.
header.PushUint8(DW_EH_PE_datarel | DW_EH_PE_sdata4);
// .eh_frame pointer
header.PushInt32(cfi_section->GetAddress() - (header_address + 4u));
// Binary search table size (number of entries).
header.PushUint32(dchecked_integral_cast<uint32_t>(binary_search_table.size()/2));
header_section->WriteFully(buffer.data(), buffer.size());
// Binary search table.
for (size_t i = 0; i < binary_search_table.size(); i++) {
// Make addresses section-relative since we know the header address now.
binary_search_table[i] -= header_address;
}
header_section->WriteFully(binary_search_table.data(), binary_search_table.size());
header_section->End();
} else {
builder->WritePatches(".debug_frame.oat_patches",
ArrayRef<const uintptr_t>(patch_locations));
}
}
namespace {
struct CompilationUnit {
std::vector<const MethodDebugInfo*> methods_;
size_t debug_line_offset_ = 0;
uint32_t low_pc_ = 0xFFFFFFFFU;
uint32_t high_pc_ = 0;
};
struct LocalVariable {
uint16_t vreg;
uint32_t dex_pc_low;
uint32_t dex_pc_high;
const char* name;
const char* type;
const char* sig;
};
struct DebugInfoCallback {
static void NewLocal(void* ctx,
uint16_t vreg,
uint32_t start,
uint32_t end,
const char* name,
const char* type,
const char* sig) {
auto* context = static_cast<DebugInfoCallback*>(ctx);
if (name != nullptr && type != nullptr) {
context->local_variables_.push_back({vreg, start, end, name, type, sig});
}
}
std::vector<LocalVariable> local_variables_;
};
std::vector<const char*> GetParamNames(const MethodDebugInfo* mi) {
std::vector<const char*> names;
const uint8_t* stream = mi->dex_file_->GetDebugInfoStream(mi->code_item_);
if (stream != nullptr) {
DecodeUnsignedLeb128(&stream); // line.
uint32_t parameters_size = DecodeUnsignedLeb128(&stream);
for (uint32_t i = 0; i < parameters_size; ++i) {
uint32_t id = DecodeUnsignedLeb128P1(&stream);
names.push_back(mi->dex_file_->StringDataByIdx(id));
}
}
return names;
}
struct VariableLocation {
uint32_t low_pc;
uint32_t high_pc;
DexRegisterLocation reg_lo; // May be None if the location is unknown.
DexRegisterLocation reg_hi; // Most significant bits of 64-bit value.
};
// Get the location of given dex register (e.g. stack or machine register).
// Note that the location might be different based on the current pc.
// The result will cover all ranges where the variable is in scope.
std::vector<VariableLocation> GetVariableLocations(const MethodDebugInfo* method_info,
uint16_t vreg,
bool is64bitValue,
uint32_t dex_pc_low,
uint32_t dex_pc_high) {
std::vector<VariableLocation> variable_locations;
// Get stack maps sorted by pc (they might not be sorted internally).
const CodeInfo code_info(method_info->compiled_method_->GetVmapTable().data());
const StackMapEncoding encoding = code_info.ExtractEncoding();
std::map<uint32_t, StackMap> stack_maps;
for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
StackMap stack_map = code_info.GetStackMapAt(s, encoding);
DCHECK(stack_map.IsValid());
const uint32_t low_pc = method_info->low_pc_ + stack_map.GetNativePcOffset(encoding);
DCHECK_LE(low_pc, method_info->high_pc_);
stack_maps.emplace(low_pc, stack_map);
}
// Create entries for the requested register based on stack map data.
for (auto it = stack_maps.begin(); it != stack_maps.end(); it++) {
const StackMap& stack_map = it->second;
const uint32_t low_pc = it->first;
auto next_it = it;
next_it++;
const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first
: method_info->high_pc_;
DCHECK_LE(low_pc, high_pc);
if (low_pc == high_pc) {
continue; // Ignore if the address range is empty.
}
// Check that the stack map is in the requested range.
uint32_t dex_pc = stack_map.GetDexPc(encoding);
if (!(dex_pc_low <= dex_pc && dex_pc < dex_pc_high)) {
continue;
}
// Find the location of the dex register.
DexRegisterLocation reg_lo = DexRegisterLocation::None();
DexRegisterLocation reg_hi = DexRegisterLocation::None();
if (stack_map.HasDexRegisterMap(encoding)) {
DexRegisterMap dex_register_map = code_info.GetDexRegisterMapOf(
stack_map, encoding, method_info->code_item_->registers_size_);
reg_lo = dex_register_map.GetDexRegisterLocation(
vreg, method_info->code_item_->registers_size_, code_info, encoding);
if (is64bitValue) {
reg_hi = dex_register_map.GetDexRegisterLocation(
vreg + 1, method_info->code_item_->registers_size_, code_info, encoding);
}
}
// Add location entry for this address range.
if (!variable_locations.empty() &&
variable_locations.back().reg_lo == reg_lo &&
variable_locations.back().reg_hi == reg_hi &&
variable_locations.back().high_pc == low_pc) {
// Merge with the previous entry (extend its range).
variable_locations.back().high_pc = high_pc;
} else {
variable_locations.push_back({low_pc, high_pc, reg_lo, reg_hi});
}
}
return variable_locations;
}
bool IsFromOptimizingCompiler(const MethodDebugInfo* method_info) {
return method_info->compiled_method_->GetQuickCode().size() > 0 &&
method_info->compiled_method_->GetVmapTable().size() > 0 &&
method_info->compiled_method_->GetGcMap().size() == 0 &&
method_info->code_item_ != nullptr;
}
} // namespace
// Helper class to write .debug_info and its supporting sections.
template<typename ElfTypes>
class DebugInfoWriter {
typedef typename ElfTypes::Addr Elf_Addr;
// Helper class to write one compilation unit.
// It holds helper methods and temporary state.
class CompilationUnitWriter {
public:
explicit CompilationUnitWriter(DebugInfoWriter* owner)
: owner_(owner),
info_(Is64BitInstructionSet(owner_->builder_->GetIsa()), &debug_abbrev_) {
}
void Write(const CompilationUnit& compilation_unit) {
CHECK(!compilation_unit.methods_.empty());
const Elf_Addr text_address = owner_->builder_->GetText()->GetAddress();
info_.StartTag(DW_TAG_compile_unit);
info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
info_.WriteData1(DW_AT_language, DW_LANG_Java);
info_.WriteAddr(DW_AT_low_pc, text_address + compilation_unit.low_pc_);
info_.WriteUdata(DW_AT_high_pc, compilation_unit.high_pc_ - compilation_unit.low_pc_);
info_.WriteSecOffset(DW_AT_stmt_list, compilation_unit.debug_line_offset_);
const char* last_dex_class_desc = nullptr;
for (auto mi : compilation_unit.methods_) {
const DexFile* dex = mi->dex_file_;
const DexFile::MethodId& dex_method = dex->GetMethodId(mi->dex_method_index_);
const DexFile::ProtoId& dex_proto = dex->GetMethodPrototype(dex_method);
const DexFile::TypeList* dex_params = dex->GetProtoParameters(dex_proto);
const char* dex_class_desc = dex->GetMethodDeclaringClassDescriptor(dex_method);
const bool is_static = (mi->access_flags_ & kAccStatic) != 0;
// Enclose the method in correct class definition.
if (last_dex_class_desc != dex_class_desc) {
if (last_dex_class_desc != nullptr) {
EndClassTag(last_dex_class_desc);
}
size_t offset = StartClassTag(dex_class_desc);
type_cache_.emplace(dex_class_desc, offset);
// Check that each class is defined only once.
bool unique = owner_->defined_dex_classes_.insert(dex_class_desc).second;
CHECK(unique) << "Redefinition of " << dex_class_desc;
last_dex_class_desc = dex_class_desc;
}
// Collect information about local variables and parameters.
DebugInfoCallback debug_info_callback;
std::vector<const char*> param_names;
if (mi->code_item_ != nullptr) {
dex->DecodeDebugInfo(mi->code_item_,
is_static,
mi->dex_method_index_,
nullptr,
DebugInfoCallback::NewLocal,
&debug_info_callback);
param_names = GetParamNames(mi);
}
int start_depth = info_.Depth();
info_.StartTag(DW_TAG_subprogram);
WriteName(dex->GetMethodName(dex_method));
info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_);
info_.WriteUdata(DW_AT_high_pc, mi->high_pc_ - mi->low_pc_);
uint8_t frame_base[] = { DW_OP_call_frame_cfa };
info_.WriteExprLoc(DW_AT_frame_base, &frame_base, sizeof(frame_base));
WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
uint32_t vreg = mi->code_item_ == nullptr ? 0 :
mi->code_item_->registers_size_ - mi->code_item_->ins_size_;
if (!is_static) {
info_.StartTag(DW_TAG_formal_parameter);
WriteName("this");
info_.WriteFlag(DW_AT_artificial, true);
WriteLazyType(dex_class_desc);
const bool is64bitValue = false;
WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
vreg++;
info_.EndTag();
}
if (dex_params != nullptr) {
for (uint32_t i = 0; i < dex_params->Size(); ++i) {
info_.StartTag(DW_TAG_formal_parameter);
// Parameter names may not be always available.
if (i < param_names.size() && param_names[i] != nullptr) {
WriteName(param_names[i]);
}
// Write the type.
const char* type_desc = dex->StringByTypeIdx(dex_params->GetTypeItem(i).type_idx_);
WriteLazyType(type_desc);
// Write the stack location of the parameter.
const bool is64bitValue = type_desc[0] == 'D' || type_desc[0] == 'J';
WriteRegLocation(mi, vreg, is64bitValue, compilation_unit.low_pc_);
vreg += is64bitValue ? 2 : 1;
info_.EndTag();
}
if (mi->code_item_ != nullptr) {
CHECK_EQ(vreg, mi->code_item_->registers_size_);
}
}
for (const LocalVariable& var : debug_info_callback.local_variables_) {
const uint32_t first_arg = mi->code_item_->registers_size_ - mi->code_item_->ins_size_;
if (var.vreg < first_arg) {
info_.StartTag(DW_TAG_variable);
WriteName(var.name);
WriteLazyType(var.type);
bool is64bitValue = var.type[0] == 'D' || var.type[0] == 'J';
WriteRegLocation(mi, var.vreg, is64bitValue, compilation_unit.low_pc_,
var.dex_pc_low, var.dex_pc_high);
info_.EndTag();
}
}
info_.EndTag();
CHECK_EQ(info_.Depth(), start_depth); // Balanced start/end.
}
if (last_dex_class_desc != nullptr) {
EndClassTag(last_dex_class_desc);
}
CHECK_EQ(info_.Depth(), 1);
FinishLazyTypes();
info_.EndTag(); // DW_TAG_compile_unit
std::vector<uint8_t> buffer;
buffer.reserve(info_.data()->size() + KB);
const size_t offset = owner_->builder_->GetDebugInfo()->GetSize();
const size_t debug_abbrev_offset =
owner_->debug_abbrev_.Insert(debug_abbrev_.data(), debug_abbrev_.size());
WriteDebugInfoCU(debug_abbrev_offset, info_, offset, &buffer, &owner_->debug_info_patches_);
owner_->builder_->GetDebugInfo()->WriteFully(buffer.data(), buffer.size());
}
// Write table into .debug_loc which describes location of dex register.
// The dex register might be valid only at some points and it might
// move between machine registers and stack.
void WriteRegLocation(const MethodDebugInfo* method_info,
uint16_t vreg,
bool is64bitValue,
uint32_t compilation_unit_low_pc,
uint32_t dex_pc_low = 0,
uint32_t dex_pc_high = 0xFFFFFFFF) {
using Kind = DexRegisterLocation::Kind;
if (!IsFromOptimizingCompiler(method_info)) {
return;
}
Writer<> debug_loc(&owner_->debug_loc_);
Writer<> debug_ranges(&owner_->debug_ranges_);
info_.WriteSecOffset(DW_AT_location, debug_loc.size());
info_.WriteSecOffset(DW_AT_start_scope, debug_ranges.size());
std::vector<VariableLocation> variable_locations = GetVariableLocations(
method_info,
vreg,
is64bitValue,
dex_pc_low,
dex_pc_high);
// Write .debug_loc entries.
const InstructionSet isa = owner_->builder_->GetIsa();
const bool is64bit = Is64BitInstructionSet(isa);
for (const VariableLocation& variable_location : variable_locations) {
// Translate dex register location to DWARF expression.
// Note that 64-bit value might be split to two distinct locations.
// (for example, two 32-bit machine registers, or even stack and register)
uint8_t buffer[64];
uint8_t* pos = buffer;
DexRegisterLocation reg_lo = variable_location.reg_lo;
DexRegisterLocation reg_hi = variable_location.reg_hi;
for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
DexRegisterLocation reg_loc = (piece == 0 ? reg_lo : reg_hi);
const Kind kind = reg_loc.GetKind();
const int32_t value = reg_loc.GetValue();
if (kind == Kind::kInStack) {
const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes();
*(pos++) = DW_OP_fbreg;
// The stack offset is relative to SP. Make it relative to CFA.
pos = EncodeSignedLeb128(pos, value - frame_size);
if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
reg_hi.GetValue() == value + 4) {
break; // the high word is correctly implied by the low word.
}
} else if (kind == Kind::kInRegister) {
pos = WriteOpReg(pos, GetDwarfCoreReg(isa, value).num());
if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
reg_hi.GetValue() == value) {
break; // the high word is correctly implied by the low word.
}
} else if (kind == Kind::kInFpuRegister) {
if ((isa == kArm || isa == kThumb2) &&
piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
reg_hi.GetValue() == value + 1 && value % 2 == 0) {
// Translate S register pair to D register (e.g. S4+S5 to D2).
pos = WriteOpReg(pos, Reg::ArmDp(value / 2).num());
break;
}
if (isa == kMips || isa == kMips64) {
// TODO: Find what the DWARF floating point register numbers are on MIPS.
break;
}
pos = WriteOpReg(pos, GetDwarfFpReg(isa, value).num());
if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
reg_hi.GetValue() == reg_lo.GetValue()) {
break; // the high word is correctly implied by the low word.
}
} else if (kind == Kind::kConstant) {
*(pos++) = DW_OP_consts;
pos = EncodeSignedLeb128(pos, value);
*(pos++) = DW_OP_stack_value;
} else if (kind == Kind::kNone) {
break;
} else {
// kInStackLargeOffset and kConstantLargeValue are hidden by GetKind().
// kInRegisterHigh and kInFpuRegisterHigh should be handled by
// the special cases above and they should not occur alone.
LOG(ERROR) << "Unexpected register location kind: "
<< DexRegisterLocation::PrettyDescriptor(kind);
break;
}
if (is64bitValue) {
// Write the marker which is needed by split 64-bit values.
// This code is skipped by the special cases.
*(pos++) = DW_OP_piece;
pos = EncodeUnsignedLeb128(pos, 4);
}
}
// Check that the buffer is large enough; keep half of it empty for safety.
DCHECK_LE(static_cast<size_t>(pos - buffer), sizeof(buffer) / 2);
if (pos > buffer) {
if (is64bit) {
debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc);
debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc);
} else {
debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc);
debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc);
}
// Write the expression.
debug_loc.PushUint16(pos - buffer);
debug_loc.PushData(buffer, pos - buffer);
} else {
// Do not generate .debug_loc if the location is not known.
}
}
// Write end-of-list entry.
if (is64bit) {
debug_loc.PushUint64(0);
debug_loc.PushUint64(0);
} else {
debug_loc.PushUint32(0);
debug_loc.PushUint32(0);
}
// Write .debug_ranges entries.
// This includes ranges where the variable is in scope but the location is not known.
for (size_t i = 0; i < variable_locations.size(); i++) {
uint32_t low_pc = variable_locations[i].low_pc;
uint32_t high_pc = variable_locations[i].high_pc;
while (i + 1 < variable_locations.size() && variable_locations[i+1].low_pc == high_pc) {
// Merge address range with the next entry.
high_pc = variable_locations[++i].high_pc;
}
if (is64bit) {
debug_ranges.PushUint64(low_pc - compilation_unit_low_pc);
debug_ranges.PushUint64(high_pc - compilation_unit_low_pc);
} else {
debug_ranges.PushUint32(low_pc - compilation_unit_low_pc);
debug_ranges.PushUint32(high_pc - compilation_unit_low_pc);
}
}
// Write end-of-list entry.
if (is64bit) {
debug_ranges.PushUint64(0);
debug_ranges.PushUint64(0);
} else {
debug_ranges.PushUint32(0);
debug_ranges.PushUint32(0);
}
}
// Some types are difficult to define as we go since they need
// to be enclosed in the right set of namespaces. Therefore we
// just define all types lazily at the end of compilation unit.
void WriteLazyType(const char* type_descriptor) {
DCHECK(type_descriptor != nullptr);
if (type_descriptor[0] != 'V') {
lazy_types_.emplace(type_descriptor, info_.size());
info_.WriteRef4(DW_AT_type, 0);
}
}
void FinishLazyTypes() {
for (const auto& lazy_type : lazy_types_) {
info_.UpdateUint32(lazy_type.second, WriteType(lazy_type.first));
}
lazy_types_.clear();
}
private:
void WriteName(const char* name) {
info_.WriteStrp(DW_AT_name, owner_->WriteString(name));
}
// Helper which writes DWARF expression referencing a register.
static uint8_t* WriteOpReg(uint8_t* buffer, uint32_t dwarf_reg_num) {
if (dwarf_reg_num < 32) {
*(buffer++) = DW_OP_reg0 + dwarf_reg_num;
} else {
*(buffer++) = DW_OP_regx;
buffer = EncodeUnsignedLeb128(buffer, dwarf_reg_num);
}
return buffer;
}
// Convert dex type descriptor to DWARF.
// Returns offset in the compilation unit.
size_t WriteType(const char* desc) {
const auto& it = type_cache_.find(desc);
if (it != type_cache_.end()) {
return it->second;
}
size_t offset;
if (*desc == 'L') {
// Class type. For example: Lpackage/name;
offset = StartClassTag(desc);
info_.WriteFlag(DW_AT_declaration, true);
EndClassTag(desc);
} else if (*desc == '[') {
// Array type.
size_t element_type = WriteType(desc + 1);
offset = info_.StartTag(DW_TAG_array_type);
info_.WriteRef(DW_AT_type, element_type);
info_.EndTag();
} else {
// Primitive types.
const char* name;
uint32_t encoding;
uint32_t byte_size;
switch (*desc) {
case 'B':
name = "byte";
encoding = DW_ATE_signed;
byte_size = 1;
break;
case 'C':
name = "char";
encoding = DW_ATE_UTF;
byte_size = 2;
break;
case 'D':
name = "double";
encoding = DW_ATE_float;
byte_size = 8;
break;
case 'F':
name = "float";
encoding = DW_ATE_float;
byte_size = 4;
break;
case 'I':
name = "int";
encoding = DW_ATE_signed;
byte_size = 4;
break;
case 'J':
name = "long";
encoding = DW_ATE_signed;
byte_size = 8;
break;
case 'S':
name = "short";
encoding = DW_ATE_signed;
byte_size = 2;
break;
case 'Z':
name = "boolean";
encoding = DW_ATE_boolean;
byte_size = 1;
break;
case 'V':
LOG(FATAL) << "Void type should not be encoded";
UNREACHABLE();
default:
LOG(FATAL) << "Unknown dex type descriptor: " << desc;
UNREACHABLE();
}
offset = info_.StartTag(DW_TAG_base_type);
WriteName(name);
info_.WriteData1(DW_AT_encoding, encoding);
info_.WriteData1(DW_AT_byte_size, byte_size);
info_.EndTag();
}
type_cache_.emplace(desc, offset);
return offset;
}
// Start DW_TAG_class_type tag nested in DW_TAG_namespace tags.
// Returns offset of the class tag in the compilation unit.
size_t StartClassTag(const char* desc) {
DCHECK(desc != nullptr && desc[0] == 'L');
// Enclose the type in namespace tags.
const char* end;
for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) {
info_.StartTag(DW_TAG_namespace);
WriteName(std::string(desc, end - desc).c_str());
}
// Start the class tag.
size_t offset = info_.StartTag(DW_TAG_class_type);
end = strchr(desc, ';');
CHECK(end != nullptr);
WriteName(std::string(desc, end - desc).c_str());
return offset;
}
void EndClassTag(const char* desc) {
DCHECK(desc != nullptr && desc[0] == 'L');
// End the class tag.
info_.EndTag();
// Close namespace tags.
const char* end;
for (desc = desc + 1; (end = strchr(desc, '/')) != nullptr; desc = end + 1) {
info_.EndTag();
}
}
// For access to the ELF sections.
DebugInfoWriter<ElfTypes>* owner_;
// Debug abbrevs for this compilation unit only.
std::vector<uint8_t> debug_abbrev_;
// Temporary buffer to create and store the entries.
DebugInfoEntryWriter<> info_;
// Cache of already translated type descriptors.
std::map<const char*, size_t, CStringLess> type_cache_; // type_desc -> definition_offset.
// 32-bit references which need to be resolved to a type later.
std::multimap<const char*, size_t, CStringLess> lazy_types_; // type_desc -> patch_offset.
};
public:
explicit DebugInfoWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
}
void Start() {
builder_->GetDebugInfo()->Start();
}
void WriteCompilationUnit(const CompilationUnit& compilation_unit) {
CompilationUnitWriter writer(this);
writer.Write(compilation_unit);
}
void End() {
builder_->GetDebugInfo()->End();
builder_->WritePatches(".debug_info.oat_patches",
ArrayRef<const uintptr_t>(debug_info_patches_));
builder_->WriteSection(".debug_abbrev", &debug_abbrev_.Data());
builder_->WriteSection(".debug_str", &debug_str_.Data());
builder_->WriteSection(".debug_loc", &debug_loc_);
builder_->WriteSection(".debug_ranges", &debug_ranges_);
}
private:
size_t WriteString(const char* str) {
return debug_str_.Insert(reinterpret_cast<const uint8_t*>(str), strlen(str) + 1);
}
ElfBuilder<ElfTypes>* builder_;
std::vector<uintptr_t> debug_info_patches_;
DedupVector debug_abbrev_;
DedupVector debug_str_;
std::vector<uint8_t> debug_loc_;
std::vector<uint8_t> debug_ranges_;
std::unordered_set<const char*> defined_dex_classes_; // For CHECKs only.
};
template<typename ElfTypes>
class DebugLineWriter {
typedef typename ElfTypes::Addr Elf_Addr;
public:
explicit DebugLineWriter(ElfBuilder<ElfTypes>* builder) : builder_(builder) {
}
void Start() {
builder_->GetDebugLine()->Start();
}
// Write line table for given set of methods.
// Returns the number of bytes written.
size_t WriteCompilationUnit(CompilationUnit& compilation_unit) {
const bool is64bit = Is64BitInstructionSet(builder_->GetIsa());
const Elf_Addr text_address = builder_->GetText()->GetAddress();
compilation_unit.debug_line_offset_ = builder_->GetDebugLine()->GetSize();
std::vector<FileEntry> files;
std::unordered_map<std::string, size_t> files_map;
std::vector<std::string> directories;
std::unordered_map<std::string, size_t> directories_map;
int code_factor_bits_ = 0;
int dwarf_isa = -1;
switch (builder_->GetIsa()) {
case kArm: // arm actually means thumb2.
case kThumb2:
code_factor_bits_ = 1; // 16-bit instuctions
dwarf_isa = 1; // DW_ISA_ARM_thumb.
break;
case kArm64:
case kMips:
case kMips64:
code_factor_bits_ = 2; // 32-bit instructions
break;
case kNone:
case kX86:
case kX86_64:
break;
}
DebugLineOpCodeWriter<> opcodes(is64bit, code_factor_bits_);
for (const MethodDebugInfo* mi : compilation_unit.methods_) {
// Ignore function if we have already generated line table for the same address.
// It would confuse the debugger and the DWARF specification forbids it.
if (mi->deduped_) {
continue;
}
ArrayRef<const SrcMapElem> src_mapping_table;
std::vector<SrcMapElem> src_mapping_table_from_stack_maps;
if (IsFromOptimizingCompiler(mi)) {
// Use stack maps to create mapping table from pc to dex.
const CodeInfo code_info(mi->compiled_method_->GetVmapTable().data());
const StackMapEncoding encoding = code_info.ExtractEncoding();
for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
StackMap stack_map = code_info.GetStackMapAt(s, encoding);
DCHECK(stack_map.IsValid());
const uint32_t pc = stack_map.GetNativePcOffset(encoding);
const int32_t dex = stack_map.GetDexPc(encoding);
src_mapping_table_from_stack_maps.push_back({pc, dex});
}
std::sort(src_mapping_table_from_stack_maps.begin(),
src_mapping_table_from_stack_maps.end());
src_mapping_table = ArrayRef<const SrcMapElem>(src_mapping_table_from_stack_maps);
} else {
// Use the mapping table provided by the quick compiler.
src_mapping_table = mi->compiled_method_->GetSrcMappingTable();
}
if (src_mapping_table.empty()) {
continue;
}
// Create mapping table from dex to source line.
struct DebugInfoCallbacks {
static bool NewPosition(void* ctx, uint32_t address, uint32_t line) {
auto* context = static_cast<DebugInfoCallbacks*>(ctx);
context->dex2line_.push_back({address, static_cast<int32_t>(line)});
return false;
}
DefaultSrcMap dex2line_;
} debug_info_callbacks;
Elf_Addr method_address = text_address + mi->low_pc_;
const DexFile* dex = mi->dex_file_;
if (mi->code_item_ != nullptr) {
dex->DecodeDebugInfo(mi->code_item_,
(mi->access_flags_ & kAccStatic) != 0,
mi->dex_method_index_,
DebugInfoCallbacks::NewPosition,
nullptr,
&debug_info_callbacks);
}
if (debug_info_callbacks.dex2line_.empty()) {
continue;
}
opcodes.SetAddress(method_address);
if (dwarf_isa != -1) {
opcodes.SetISA(dwarf_isa);
}
// Get and deduplicate directory and filename.
int file_index = 0; // 0 - primary source file of the compilation.
auto& dex_class_def = dex->GetClassDef(mi->class_def_index_);
const char* source_file = dex->GetSourceFile(dex_class_def);
if (source_file != nullptr) {
std::string file_name(source_file);
size_t file_name_slash = file_name.find_last_of('/');
std::string class_name(dex->GetClassDescriptor(dex_class_def));
size_t class_name_slash = class_name.find_last_of('/');
std::string full_path(file_name);
// Guess directory from package name.
int directory_index = 0; // 0 - current directory of the compilation.
if (file_name_slash == std::string::npos && // Just filename.
class_name.front() == 'L' && // Type descriptor for a class.
class_name_slash != std::string::npos) { // Has package name.
std::string package_name = class_name.substr(1, class_name_slash - 1);
auto it = directories_map.find(package_name);
if (it == directories_map.end()) {
directory_index = 1 + directories.size();
directories_map.emplace(package_name, directory_index);
directories.push_back(package_name);
} else {
directory_index = it->second;
}
full_path = package_name + "/" + file_name;
}
// Add file entry.
auto it2 = files_map.find(full_path);
if (it2 == files_map.end()) {
file_index = 1 + files.size();
files_map.emplace(full_path, file_index);
files.push_back(FileEntry {
file_name,
directory_index,
0, // Modification time - NA.
0, // File size - NA.
});
} else {
file_index = it2->second;
}
}
opcodes.SetFile(file_index);
// Generate mapping opcodes from PC to Java lines.
const DefaultSrcMap& dex2line_map = debug_info_callbacks.dex2line_;
if (file_index != 0 && !dex2line_map.empty()) {
bool first = true;
for (SrcMapElem pc2dex : src_mapping_table) {
uint32_t pc = pc2dex.from_;
int dex_pc = pc2dex.to_;
auto dex2line = dex2line_map.Find(static_cast<uint32_t>(dex_pc));
if (dex2line.first) {
int line = dex2line.second;
if (first) {
first = false;
if (pc > 0) {
// Assume that any preceding code is prologue.
int first_line = dex2line_map.front().to_;
// Prologue is not a sensible place for a breakpoint.
opcodes.NegateStmt();
opcodes.AddRow(method_address, first_line);
opcodes.NegateStmt();
opcodes.SetPrologueEnd();
}
opcodes.AddRow(method_address + pc, line);
} else if (line != opcodes.CurrentLine()) {
opcodes.AddRow(method_address + pc, line);
}
}
}
} else {
// line 0 - instruction cannot be attributed to any source line.
opcodes.AddRow(method_address, 0);
}
opcodes.AdvancePC(text_address + mi->high_pc_);
opcodes.EndSequence();
}
std::vector<uint8_t> buffer;
buffer.reserve(opcodes.data()->size() + KB);
size_t offset = builder_->GetDebugLine()->GetSize();
WriteDebugLineTable(directories, files, opcodes, offset, &buffer, &debug_line_patches);
builder_->GetDebugLine()->WriteFully(buffer.data(), buffer.size());
return buffer.size();
}
void End() {
builder_->GetDebugLine()->End();
builder_->WritePatches(".debug_line.oat_patches",
ArrayRef<const uintptr_t>(debug_line_patches));
}
private:
ElfBuilder<ElfTypes>* builder_;
std::vector<uintptr_t> debug_line_patches;
};
template<typename ElfTypes>
void WriteDebugSections(ElfBuilder<ElfTypes>* builder,
const ArrayRef<const MethodDebugInfo>& method_infos) {
// Group the methods into compilation units based on source file.
std::vector<CompilationUnit> compilation_units;
const char* last_source_file = nullptr;
for (const MethodDebugInfo& mi : method_infos) {
auto& dex_class_def = mi.dex_file_->GetClassDef(mi.class_def_index_);
const char* source_file = mi.dex_file_->GetSourceFile(dex_class_def);
if (compilation_units.empty() || source_file != last_source_file) {
compilation_units.push_back(CompilationUnit());
}
CompilationUnit& cu = compilation_units.back();
cu.methods_.push_back(&mi);
cu.low_pc_ = std::min(cu.low_pc_, mi.low_pc_);
cu.high_pc_ = std::max(cu.high_pc_, mi.high_pc_);
last_source_file = source_file;
}
// Write .debug_line section.
{
DebugLineWriter<ElfTypes> line_writer(builder);
line_writer.Start();
for (auto& compilation_unit : compilation_units) {
line_writer.WriteCompilationUnit(compilation_unit);
}
line_writer.End();
}
// Write .debug_info section.
{
DebugInfoWriter<ElfTypes> info_writer(builder);
info_writer.Start();
for (const auto& compilation_unit : compilation_units) {
info_writer.WriteCompilationUnit(compilation_unit);
}
info_writer.End();
}
}
// Explicit instantiations
template void WriteCFISection<ElfTypes32>(
ElfBuilder<ElfTypes32>* builder,
const ArrayRef<const MethodDebugInfo>& method_infos,
CFIFormat format);
template void WriteCFISection<ElfTypes64>(
ElfBuilder<ElfTypes64>* builder,
const ArrayRef<const MethodDebugInfo>& method_infos,
CFIFormat format);
template void WriteDebugSections<ElfTypes32>(
ElfBuilder<ElfTypes32>* builder,
const ArrayRef<const MethodDebugInfo>& method_infos);
template void WriteDebugSections<ElfTypes64>(
ElfBuilder<ElfTypes64>* builder,
const ArrayRef<const MethodDebugInfo>& method_infos);
} // namespace dwarf
} // namespace art
|