Fix various problems in PC list handling

This commit is contained in:
Théophile Bastian 2018-05-11 15:52:38 +02:00
parent bc05e8270c
commit 52ea92dcf8
5 changed files with 14 additions and 4 deletions

View file

@ -19,6 +19,7 @@ CodeGenerator::CodeGenerator(
{ {
if(!settings::pc_list.empty()) { if(!settings::pc_list.empty()) {
pc_list = make_unique<PcListReader>(settings::pc_list); pc_list = make_unique<PcListReader>(settings::pc_list);
pc_list->read();
} }
} }
@ -133,6 +134,9 @@ void CodeGenerator::gen_case(uintptr_t low_bound, uintptr_t high_bound) {
pc_list->get_list().end(), pc_list->get_list().end(),
high_bound); high_bound);
if(first_it == pc_list->get_list().end())
throw CodeGenerator::InvalidPcList();
os << std::hex; os << std::hex;
for(auto it = first_it; it != last_it; ++it) for(auto it = first_it; it != last_it; ++it)
os << "\t\tcase 0x" << *it << ":\n"; os << "\t\tcase 0x" << *it << ":\n";

View file

@ -18,6 +18,8 @@ class CodeGenerator {
/// Unimplemented case /// Unimplemented case
class NotImplementedCase: public std::exception {}; class NotImplementedCase: public std::exception {};
class InvalidPcList: public std::exception {};
/** Create a CodeGenerator to generate code for the given dwarf, on the /** Create a CodeGenerator to generate code for the given dwarf, on the
* given std::ostream object (eg. cout). */ * given std::ostream object (eg. cout). */
CodeGenerator(const SimpleDwarf& dwarf, std::ostream& os, CodeGenerator(const SimpleDwarf& dwarf, std::ostream& os,

View file

@ -18,8 +18,11 @@ void PcListReader::read() {
while(!handle.eof()) { while(!handle.eof()) {
handle.read((char*)buffer, 8); handle.read((char*)buffer, 8);
if(handle.gcount() != 8) if(handle.gcount() != 8) {
if(handle.gcount() == 0)
break;
throw PcListReader::BadFormat(); throw PcListReader::BadFormat();
}
last_pc = 0; last_pc = 0;
for(int shift = 0; shift < 8; ++shift) { for(int shift = 0; shift < 8; ++shift) {

View file

@ -11,10 +11,10 @@
class PcListReader { class PcListReader {
public: public:
/// Thrown when the file is somehow not readable /// Thrown when the file is somehow not readable
class CannotReadFile: std::exception {}; class CannotReadFile: public std::exception {};
/// Thrown when the file contains bad content (probably not aligned 8B) /// Thrown when the file contains bad content (probably not aligned 8B)
class BadFormat: std::exception {}; class BadFormat: public std::exception {};
PcListReader(const std::string& path); PcListReader(const std::string& path);

View file

@ -73,7 +73,8 @@ MainOptions options_parse(int argc, char** argv) {
if(print_helptext) { if(print_helptext) {
cerr << "Usage: " cerr << "Usage: "
<< argv[0] << argv[0]
<< " [--switch-per-func | --global-switch] elf_path" << " [--switch-per-func | --global-switch] "
<< "[--pc-list PC_LIST_FILE] elf_path"
<< endl; << endl;
} }
if(exit_status >= 0) if(exit_status >= 0)