!1324 Optimization of message format in verbose mode

Merge pull request !1324 from shixiaowei4/wrong_message_in_verbose_mode
This commit is contained in:
openharmony_ci 2024-04-07 02:28:15 +00:00 committed by Gitee
commit 41ac1a3851
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 76 additions and 35 deletions

View File

@ -16,6 +16,7 @@
#include "disassembler.h"
#include "mangling.h"
#include "utils/logger.h"
#include "utils/const_value.h"
#include <iomanip>
@ -1589,6 +1590,44 @@ void Disassembler::SerializeMethodAnnotations(const pandasm::Function &method, s
}
}
void Disassembler::SerializeInstructions(const pandasm::Function &method, std::ostream &os,
const std::map<std::string, MethodInfo>::const_iterator &method_info_it,
bool print_method_info) const
{
std::string delim = ": ";
size_t width = 0;
if (print_method_info) {
for (const auto &i : method.ins) {
size_t ins_size = i.ToString().size();
if (i.set_label) {
ins_size = ins_size - i.label.size() - delim.length();
}
if (ins_size > width && ins_size < ark::INSTRUCTION_WIDTH_LIMIT) {
width = i.ToString().size();
}
}
}
for (size_t i = 0; i < method.ins.size(); i++) {
std::string ins = method.ins[i].ToString("", true, method.regs_num);
if (method.ins[i].set_label) {
size_t pos = ins.find(delim);
std::string label = ins.substr(0, pos);
ins.erase(0, pos + delim.length());
os << label << ":\n";
}
if (ins != "") {
os << "\t" << std::setw(width) << std::left << ins;
if (print_method_info && i < method_info_it->second.instructions_info.size()) {
os << " # " << method_info_it->second.instructions_info.at(i);
}
os << "\n";
}
}
}
void Disassembler::Serialize(const pandasm::Function &method, std::ostream &os, bool print_information) const
{
SerializeMethodAnnotations(method, os);
@ -1615,37 +1654,11 @@ void Disassembler::Serialize(const pandasm::Function &method, std::ostream &os,
auto method_info_it = prog_info_.methods_info.find(signature);
bool print_method_info = print_information && method_info_it != prog_info_.methods_info.end();
if (print_method_info) {
const MethodInfo &method_info = method_info_it->second;
size_t width = 0;
for (const auto &i : method.ins) {
if (i.ToString().size() > width) {
width = i.ToString().size();
}
}
os << " { # " << method_info.method_info << "\n# CODE:\n";
for (size_t i = 0; i < method.ins.size(); i++) {
os << "\t" << std::setw(width) << std::left << method.ins.at(i).ToString("", true, method.regs_num) << " # "
<< method_info.instructions_info.at(i) << "\n";
}
os << " { # " << method_info_it->second.method_info << "\n# CODE:\n";
} else {
os << " {\n";
for (const auto &i : method.ins) {
if (i.set_label) {
std::string ins = i.ToString("", true, method.regs_num);
std::string delim = ": ";
size_t pos = ins.find(delim);
std::string label = ins.substr(0, pos);
ins.erase(0, pos + delim.length());
os << label << ":\n\t" << ins << "\n";
} else {
os << "\t" << i.ToString("", true, method.regs_num) << "\n";
}
}
}
SerializeInstructions(method, os, method_info_it, print_method_info);
if (method.catch_blocks.size() != 0) {
os << "\n";

View File

@ -140,6 +140,9 @@ private:
void Serialize(const pandasm::Record &record, std::ostream &os, bool print_information = false) const;
void SerializeFields(const pandasm::Record &record, std::ostream &os, bool print_information) const;
void Serialize(const pandasm::Function &method, std::ostream &os, bool print_information = false) const;
void SerializeInstructions(const pandasm::Function &method, std::ostream &os,
const std::map<std::string, MethodInfo>::const_iterator &method_info_it,
bool print_method_info = false) const;
void SerializeMethodAnnotations(const pandasm::Function &method, std::ostream &os) const;
void SerializeStrings(const panda_file::File::EntityId &offset, const std::string &name_value,
std::ostream &os) const;

View File

@ -14,6 +14,7 @@
*/
#include "disassembler.h"
#include "utils/const_value.h"
#include <sstream>
#include <iomanip>
@ -21,9 +22,6 @@
namespace panda::disasm {
void Disassembler::GetInsInfo(const panda_file::File::EntityId& code_id, MethodInfo* method_info /* out */) const {
const static size_t FORMAT_WIDTH = 20;
const static size_t INSTRUCTION_WIDTH = 4;
panda_file::CodeDataAccessor code_accessor(*file_, code_id);
auto ins_sz = code_accessor.GetCodeSize();
@ -32,11 +30,12 @@ void Disassembler::GetInsInfo(const panda_file::File::EntityId& code_id, MethodI
auto bc_ins = BytecodeInstruction(ins_arr);
auto bc_ins_last = bc_ins.JumpTo(ins_sz);
auto instruction_offset = code_id.GetOffset() + static_cast<uint32_t>(code_accessor.GetInstructions() - file_->GetSpanFromId(code_id).data());
while (bc_ins.GetAddress() != bc_ins_last.GetAddress()) {
std::stringstream ss;
ss << "offset: 0x" << std::setfill('0') << std::setw(4) << std::hex
<< code_id.GetOffset() + bc_ins.GetAddress() - BytecodeInstruction(ins_arr).GetAddress();
ss << "offset: 0x" << std::setfill('0') << std::setw(ark::INSTRUCTION_OFFSET_WIDTH) << std::hex
<< instruction_offset + bc_ins.GetAddress() - BytecodeInstruction(ins_arr).GetAddress();
ss << ", " << std::setfill('.');
BytecodeInstruction::Format format = bc_ins.GetFormat();
@ -44,7 +43,7 @@ void Disassembler::GetInsInfo(const panda_file::File::EntityId& code_id, MethodI
switch (format) {
% Panda::formats.each do |fmt|
case BytecodeInstruction::Format::<%= fmt.pretty.upcase %>:
ss << std::setw(FORMAT_WIDTH) << std::left << "[<%= fmt.pretty.upcase %>]";
ss << std::setw(ark::INSTRUCTION_FORMAT_WIDTH) << std::left << "[<%= fmt.pretty.upcase %>]";
break;
% end
default:
@ -57,7 +56,7 @@ void Disassembler::GetInsInfo(const panda_file::File::EntityId& code_id, MethodI
const size_t sz = bc_ins.GetSize();
for (size_t i = 0; i < sz; i++) {
ss << "0x" << std::setw(INSTRUCTION_WIDTH) << std::setfill('0') << std::right << std::hex << static_cast<int>(pc[i]);
ss << "0x" << std::setw(ark::INSTRUCTION_VALUE_WIDTH) << std::setfill('0') << std::right << std::hex << static_cast<int>(pc[i]);
if (i != sz - 1) {
ss << " ";

View File

@ -0,0 +1,26 @@
/**
* Copyright (c) 2024 Huawei Device Co., Ltd.
* 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.
*/
#ifndef CONST_VALUE_H
#define CONST_VALUE_H
namespace ark {
static constexpr size_t INSTRUCTION_WIDTH_LIMIT = 80;
static constexpr size_t INSTRUCTION_FORMAT_WIDTH = 20;
static constexpr size_t INSTRUCTION_VALUE_WIDTH = 2;
static constexpr size_t INSTRUCTION_OFFSET_WIDTH = 4;
} // namespace ark
#endif // CONST_VALUE_H