Global Metrics

path: .metrics.nargs.average
old: 2.0
new: 2.7941176470588234

path: .metrics.nargs.sum
old: 2.0
new: 95.0

path: .metrics.cognitive.average
old: 18.0
new: 2.735294117647059

path: .metrics.cognitive.sum
old: 18.0
new: 93.0

path: .metrics.cyclomatic.average
old: 6.0
new: 3.2857142857142856

path: .metrics.cyclomatic.sum
old: 12.0
new: 138.0

path: .metrics.loc.ploc
old: 141.0
new: 628.0

path: .metrics.loc.sloc
old: 159.0
new: 887.0

path: .metrics.loc.blank
old: 7.0
new: 115.0

path: .metrics.loc.cloc
old: 11.0
new: 144.0

path: .metrics.loc.lloc
old: 79.0
new: 257.0

path: .metrics.nexits.sum
old: 0.0
new: 73.0

path: .metrics.nexits.average
old: 0.0
new: 2.1470588235294117

path: .metrics.mi.mi_sei
old: 3.9264791189908586
new: -67.52227507279558

path: .metrics.mi.mi_original
old: 40.61202809150572
new: -24.326038300655256

path: .metrics.mi.mi_visual_studio
old: 23.749724030120305
new: 0.0

path: .metrics.nom.functions
old: 1.0
new: 34.0

path: .metrics.nom.total
old: 1.0
new: 34.0

path: .metrics.halstead.effort
old: 337189.8766903786
new: 2654413.043504158

path: .metrics.halstead.n1
old: 27.0
new: 40.0

path: .metrics.halstead.N1
old: 521.0
new: 2151.0

path: .metrics.halstead.n2
old: 98.0
new: 317.0

path: .metrics.halstead.bugs
old: 1.6148360282170136
new: 6.390346595607394

path: .metrics.halstead.N2
old: 387.0
new: 1398.0

path: .metrics.halstead.estimated_program_length
old: 776.623527281704
new: 2846.6205963496864

path: .metrics.halstead.difficulty
old: 53.31122448979592
new: 88.2018927444795

path: .metrics.halstead.time
old: 18732.770927243255
new: 147467.39130578656

path: .metrics.halstead.volume
old: 6324.932130473175
new: 30094.740157039272

path: .metrics.halstead.level
old: 0.018757775863719017
new: 0.011337625178826896

path: .metrics.halstead.vocabulary
old: 125.0
new: 357.0

path: .metrics.halstead.purity_ratio
old: 0.8553122547155331
new: 0.8020908978161979

path: .metrics.halstead.length
old: 908.0
new: 3549.0

Spaces Data

Minimal test - lines (88, 576)

path: .spaces[0].metrics.nexits.average
old: 0.0
new: 2.1904761904761907

path: .spaces[0].metrics.nexits.sum
old: 0.0
new: 46.0

path: .spaces[0].metrics.mi.mi_original
old: 42.831803800248935
new: 2.2037843469125136

path: .spaces[0].metrics.mi.mi_sei
old: -6.282811890816257
new: -35.969269784910324

path: .spaces[0].metrics.mi.mi_visual_studio
old: 25.04783847967774
new: 1.2887627759722302

path: .spaces[0].metrics.nom.functions
old: 1.0
new: 21.0

path: .spaces[0].metrics.nom.total
old: 1.0
new: 21.0

path: .spaces[0].metrics.cyclomatic.average
old: 11.0
new: 3.0384615384615383

path: .spaces[0].metrics.cyclomatic.sum
old: 11.0
new: 79.0

path: .spaces[0].metrics.loc.ploc
old: 137.0
new: 367.0

path: .spaces[0].metrics.loc.sloc
old: 141.0
new: 489.0

path: .spaces[0].metrics.loc.lloc
old: 79.0
new: 157.0

path: .spaces[0].metrics.loc.blank
old: 3.0
new: 47.0

path: .spaces[0].metrics.loc.cloc
old: 1.0
new: 75.0

path: .spaces[0].metrics.halstead.effort
old: 342270.45088340034
new: 966289.9951361454

path: .spaces[0].metrics.halstead.bugs
old: 1.6310164825957718
new: 3.257994903193543

path: .spaces[0].metrics.halstead.n1
old: 27.0
new: 35.0

path: .spaces[0].metrics.halstead.n2
old: 95.0
new: 230.0

path: .spaces[0].metrics.halstead.vocabulary
old: 122.0
new: 265.0

path: .spaces[0].metrics.halstead.volume
old: 6272.317290494412
new: 15914.550582263762

path: .spaces[0].metrics.halstead.estimated_program_length
old: 752.5182453498537
new: 1983.98761731028

path: .spaces[0].metrics.halstead.level
old: 0.01832561728395062
new: 0.016469745793054064

path: .spaces[0].metrics.halstead.N1
old: 521.0
new: 1179.0

path: .spaces[0].metrics.halstead.N2
old: 384.0
new: 798.0

path: .spaces[0].metrics.halstead.difficulty
old: 54.56842105263158
new: 60.71739130434783

path: .spaces[0].metrics.halstead.length
old: 905.0
new: 1977.0

path: .spaces[0].metrics.halstead.purity_ratio
old: 0.8315118733147555
new: 1.0035344548863327

path: .spaces[0].metrics.halstead.time
old: 19015.0250490778
new: 53682.77750756363

path: .spaces[0].metrics.nargs.average
old: 2.0
new: 2.1904761904761907

path: .spaces[0].metrics.nargs.sum
old: 2.0
new: 46.0

path: .spaces[0].metrics.cognitive.average
old: 18.0
new: 2.4285714285714284

path: .spaces[0].metrics.cognitive.sum
old: 18.0
new: 51.0

Code

namespace {

using lul::DwarfCFIToModule;
using lul::FindElfSectionByName;
using lul::GetOffset;
using lul::IsValidElf;
using lul::Module;
using lul::scoped_ptr;
using lul::Summariser;
using lul::UniqueStringUniverse;
using std::set;
using std::string;
using std::vector;

//
// FDWrapper
//
// Wrapper class to make sure opened file is closed.
//
class FDWrapper {
 public:
  explicit FDWrapper(int fd) : fd_(fd) {}
  ~FDWrapper() {
    if (fd_ != -1) close(fd_);
  }
  int get() { return fd_; }
  int release() {
    int fd = fd_;
    fd_ = -1;
    return fd;
  }

 private:
  int fd_;
};

//
// MmapWrapper
//
// Wrapper class to make sure mapped regions are unmapped.
//
class MmapWrapper {
 public:
  MmapWrapper() : is_set_(false), base_(NULL), size_(0) {}
  ~MmapWrapper() {
    if (is_set_ && base_ != NULL) {
      MOZ_ASSERT(size_ > 0);
      munmap(base_, size_);
    }
  }
  void set(void* mapped_address, size_t mapped_size) {
    is_set_ = true;
    base_ = mapped_address;
    size_ = mapped_size;
  }
  void release() {
    MOZ_ASSERT(is_set_);
    is_set_ = false;
    base_ = NULL;
    size_ = 0;
  }

 private:
  bool is_set_;
  void* base_;
  size_t size_;
};

// Set NUM_DW_REGNAMES to be the number of Dwarf register names
// appropriate to the machine architecture given in HEADER.  Return
// true on success, or false if HEADER's machine architecture is not
// supported.
template 
bool DwarfCFIRegisterNames(const typename ElfClass::Ehdr* elf_header,
                           unsigned int* num_dw_regnames) {
  switch (elf_header->e_machine) {
    case EM_386:
      *num_dw_regnames = DwarfCFIToModule::RegisterNames::I386();
      return true;
    case EM_ARM:
      *num_dw_regnames = DwarfCFIToModule::RegisterNames::ARM();
      return true;
    case EM_X86_64:
      *num_dw_regnames = DwarfCFIToModule::RegisterNames::X86_64();
      return true;
    case EM_MIPS:
      *num_dw_regnames = DwarfCFIToModule::RegisterNames::MIPS();
      return true;
    case EM_AARCH64:
      *num_dw_regnames = DwarfCFIToModule::RegisterNames::ARM64();
      return true;
    default:
      MOZ_ASSERT(0);
      return false;
  }
}

template 
bool LoadDwarfCFI(const string& dwarf_filename,
                  const typename ElfClass::Ehdr* elf_header,
                  const char* section_name,
                  const typename ElfClass::Shdr* section, const bool eh_frame,
                  const typename ElfClass::Shdr* got_section,
                  const typename ElfClass::Shdr* text_section,
                  const bool big_endian, SecMap* smap, uintptr_t text_bias,
                  UniqueStringUniverse* usu, void (*log)(const char*)) {
  // Find the appropriate set of register names for this file's
  // architecture.
  unsigned int num_dw_regs = 0;
  if (!DwarfCFIRegisterNames(elf_header, &num_dw_regs)) {
    fprintf(stderr,
            "%s: unrecognized ELF machine architecture '%d';"
            " cannot convert DWARF call frame information\n",
            dwarf_filename.c_str(), elf_header->e_machine);
    return false;
  }

  const lul::Endianness endianness =
      big_endian ? lul::ENDIANNESS_BIG : lul::ENDIANNESS_LITTLE;

  // Find the call frame information and its size.
  const char* cfi = GetOffset(elf_header, section->sh_offset);
  size_t cfi_size = section->sh_size;

  // Plug together the parser, handler, and their entourages.

  // Here's a summariser, which will receive the output of the
  // parser, create summaries, and add them to |smap|.
  Summariser summ(smap, text_bias, log);

  lul::ByteReader reader(endianness);
  reader.SetAddressSize(ElfClass::kAddrSize);

  DwarfCFIToModule::Reporter module_reporter(log, dwarf_filename, section_name);
  DwarfCFIToModule handler(num_dw_regs, &module_reporter, &reader, usu, &summ);

  // Provide the base addresses for .eh_frame encoded pointers, if
  // possible.
  reader.SetCFIDataBase(section->sh_addr, cfi);
  if (got_section) reader.SetDataBase(got_section->sh_addr);
  if (text_section) reader.SetTextBase(text_section->sh_addr);

  lul::CallFrameInfo::Reporter dwarf_reporter(log, dwarf_filename,
                                              section_name);
  lul::CallFrameInfo parser(cfi, cfi_size, &reader, &handler, &dwarf_reporter,
                            eh_frame);
  parser.Start();

  return true;
}

bool LoadELF(const string& obj_file, MmapWrapper* map_wrapper,
             void** elf_header) {
  int obj_fd = open(obj_file.c_str(), O_RDONLY);
  if (obj_fd < 0) {
    fprintf(stderr, "Failed to open ELF file '%s': %s\n", obj_file.c_str(),
            strerror(errno));
    return false;
  }
  FDWrapper obj_fd_wrapper(obj_fd);
  struct stat st;
  if (fstat(obj_fd, &st) != 0 && st.st_size <= 0) {
    fprintf(stderr, "Unable to fstat ELF file '%s': %s\n", obj_file.c_str(),
            strerror(errno));
    return false;
  }
  // Mapping it read-only is good enough.  In any case, mapping it
  // read-write confuses Valgrind's debuginfo acquire/discard
  // heuristics, making it hard to profile the profiler.
  void* obj_base = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, obj_fd, 0);
  if (obj_base == MAP_FAILED) {
    fprintf(stderr, "Failed to mmap ELF file '%s': %s\n", obj_file.c_str(),
            strerror(errno));
    return false;
  }
  map_wrapper->set(obj_base, st.st_size);
  *elf_header = obj_base;
  if (!IsValidElf(*elf_header)) {
    fprintf(stderr, "Not a valid ELF file: %s\n", obj_file.c_str());
    return false;
  }
  return true;
}

// Get the endianness of ELF_HEADER. If it's invalid, return false.
template 
bool ElfEndianness(const typename ElfClass::Ehdr* elf_header,
                   bool* big_endian) {
  if (elf_header->e_ident[EI_DATA] == ELFDATA2LSB) {
    *big_endian = false;
    return true;
  }
  if (elf_header->e_ident[EI_DATA] == ELFDATA2MSB) {
    *big_endian = true;
    return true;
  }

  fprintf(stderr, "bad data encoding in ELF header: %d\n",
          elf_header->e_ident[EI_DATA]);
  return false;
}

//
// LoadSymbolsInfo
//
// Holds the state between the two calls to LoadSymbols() in case it's necessary
// to follow the .gnu_debuglink section and load debug information from a
// different file.
//
template 
class LoadSymbolsInfo {
 public:
  typedef typename ElfClass::Addr Addr;

  explicit LoadSymbolsInfo(const vector& dbg_dirs)
      : debug_dirs_(dbg_dirs), has_loading_addr_(false) {}

  // Keeps track of which sections have been loaded so sections don't
  // accidentally get loaded twice from two different files.
  void LoadedSection(const string& section) {
    if (loaded_sections_.count(section) == 0) {
      loaded_sections_.insert(section);
    } else {
      fprintf(stderr, "Section %s has already been loaded.\n", section.c_str());
    }
  }

  string debuglink_file() const { return debuglink_file_; }

 private:
  const vector& debug_dirs_;  // Directories in which to
                                      // search for the debug ELF file.

  string debuglink_file_;  // Full path to the debug ELF file.

  bool has_loading_addr_;  // Indicate if LOADING_ADDR_ is valid.

  set loaded_sections_;  // Tracks the Loaded ELF sections
                                 // between calls to LoadSymbols().
};

// Find the preferred loading address of the binary.
template 
typename ElfClass::Addr GetLoadingAddress(
    const typename ElfClass::Phdr* program_headers, int nheader) {
  typedef typename ElfClass::Phdr Phdr;

  // For non-PIC executables (e_type == ET_EXEC), the load address is
  // the start address of the first PT_LOAD segment.  (ELF requires
  // the segments to be sorted by load address.)  For PIC executables
  // and dynamic libraries (e_type == ET_DYN), this address will
  // normally be zero.
  for (int i = 0; i < nheader; ++i) {
    const Phdr& header = program_headers[i];
    if (header.p_type == PT_LOAD) return header.p_vaddr;
  }
  return 0;
}

template 
bool LoadSymbols(const string& obj_file, const bool big_endian,
                 const typename ElfClass::Ehdr* elf_header,
                 const bool read_gnu_debug_link,
                 LoadSymbolsInfo* info, SecMap* smap, void* rx_avma,
                 size_t rx_size, UniqueStringUniverse* usu,
                 void (*log)(const char*)) {
  typedef typename ElfClass::Phdr Phdr;
  typedef typename ElfClass::Shdr Shdr;

  char buf[500];
  SprintfLiteral(buf, "LoadSymbols: BEGIN   %s\n", obj_file.c_str());
  buf[sizeof(buf) - 1] = 0;
  log(buf);

  // This is how the text bias is calculated.
  // BEGIN CALCULATE BIAS
  uintptr_t loading_addr = GetLoadingAddress(
      GetOffset(elf_header, elf_header->e_phoff),
      elf_header->e_phnum);
  uintptr_t text_bias = ((uintptr_t)rx_avma) - loading_addr;
  SprintfLiteral(buf, "LoadSymbols:   rx_avma=%llx, text_bias=%llx",
                 (unsigned long long int)(uintptr_t)rx_avma,
                 (unsigned long long int)text_bias);
  buf[sizeof(buf) - 1] = 0;
  log(buf);
  // END CALCULATE BIAS

  const Shdr* sections =
      GetOffset(elf_header, elf_header->e_shoff);
  const Shdr* section_names = sections + elf_header->e_shstrndx;
  const char* names =
      GetOffset(elf_header, section_names->sh_offset);
  const char* names_end = names + section_names->sh_size;
  bool found_usable_info = false;

  // Dwarf Call Frame Information (CFI) is actually independent from
  // the other DWARF debugging information, and can be used alone.
  const Shdr* dwarf_cfi_section =
      FindElfSectionByName(".debug_frame", SHT_PROGBITS, sections,
                                     names, names_end, elf_header->e_shnum);
  if (dwarf_cfi_section) {
    // Ignore the return value of this function; even without call frame
    // information, the other debugging information could be perfectly
    // useful.
    info->LoadedSection(".debug_frame");
    bool result = LoadDwarfCFI(obj_file, elf_header, ".debug_frame",
                                         dwarf_cfi_section, false, 0, 0,
                                         big_endian, smap, text_bias, usu, log);
    found_usable_info = found_usable_info || result;
    if (result) log("LoadSymbols:   read CFI from .debug_frame");
  }

  // Linux C++ exception handling information can also provide
  // unwinding data.
  const Shdr* eh_frame_section =
      FindElfSectionByName(".eh_frame", SHT_PROGBITS, sections, names,
                                     names_end, elf_header->e_shnum);
#if defined(GP_PLAT_amd64_linux) || defined(GP_PLAT_amd64_android)
  if (!eh_frame_section) {
    // Possibly depending on which linker created libxul.so, on x86_64-linux
    // and -android, .eh_frame may instead have the SHT_X86_64_UNWIND type.
    eh_frame_section =
        FindElfSectionByName(".eh_frame", SHT_X86_64_UNWIND, sections,
                                       names, names_end, elf_header->e_shnum);
  }
#endif
  if (eh_frame_section) {
    // Pointers in .eh_frame data may be relative to the base addresses of
    // certain sections. Provide those sections if present.
    const Shdr* got_section = FindElfSectionByName(
        ".got", SHT_PROGBITS, sections, names, names_end, elf_header->e_shnum);
    const Shdr* text_section = FindElfSectionByName(
        ".text", SHT_PROGBITS, sections, names, names_end, elf_header->e_shnum);
    info->LoadedSection(".eh_frame");
    // As above, ignore the return value of this function.
    bool result = LoadDwarfCFI(
        obj_file, elf_header, ".eh_frame", eh_frame_section, true, got_section,
        text_section, big_endian, smap, text_bias, usu, log);
    found_usable_info = found_usable_info || result;
    if (result) log("LoadSymbols:   read CFI from .eh_frame");
  }

  SprintfLiteral(buf, "LoadSymbols: END     %s\n", obj_file.c_str());
  buf[sizeof(buf) - 1] = 0;
  log(buf);

  return found_usable_info;
}

// Return the breakpad symbol file identifier for the architecture of
// ELF_HEADER.
template 
const char* ElfArchitecture(const typename ElfClass::Ehdr* elf_header) {
  typedef typename ElfClass::Half Half;
  Half arch = elf_header->e_machine;
  switch (arch) {
    case EM_386:
      return "x86";
    case EM_ARM:
      return "arm";
    case EM_AARCH64:
      return "arm64";
    case EM_MIPS:
      return "mips";
    case EM_PPC64:
      return "ppc64";
    case EM_PPC:
      return "ppc";
    case EM_S390:
      return "s390";
    case EM_SPARC:
      return "sparc";
    case EM_SPARCV9:
      return "sparcv9";
    case EM_X86_64:
      return "x86_64";
    default:
      return NULL;
  }
}

// Format the Elf file identifier in IDENTIFIER as a UUID with the
// dashes removed.
string FormatIdentifier(unsigned char identifier[16]) {
  char identifier_str[40];
  lul::FileID::ConvertIdentifierToString(identifier, identifier_str,
                                         sizeof(identifier_str));
  string id_no_dash;
  for (int i = 0; identifier_str[i] != '\0'; ++i)
    if (identifier_str[i] != '-') id_no_dash += identifier_str[i];
  // Add an extra "0" by the end.  PDB files on Windows have an 'age'
  // number appended to the end of the file identifier; this isn't
  // really used or necessary on other platforms, but be consistent.
  id_no_dash += '0';
  return id_no_dash;
}

// Return the non-directory portion of FILENAME: the portion after the
// last slash, or the whole filename if there are no slashes.
string BaseFileName(const string& filename) {
  // Lots of copies!  basename's behavior is less than ideal.
  char* c_filename = strdup(filename.c_str());
  string base = basename(c_filename);
  free(c_filename);
  return base;
}

template 
bool ReadSymbolDataElfClass(const typename ElfClass::Ehdr* elf_header,
                            const string& obj_filename,
                            const vector& debug_dirs, SecMap* smap,
                            void* rx_avma, size_t rx_size,
                            UniqueStringUniverse* usu,
                            void (*log)(const char*)) {
  typedef typename ElfClass::Ehdr Ehdr;

  unsigned char identifier[16];
  if (!lul ::FileID::ElfFileIdentifierFromMappedFile(elf_header, identifier)) {
    fprintf(stderr, "%s: unable to generate file identifier\n",
            obj_filename.c_str());
    return false;
  }

  const char* architecture = ElfArchitecture(elf_header);
  if (!architecture) {
    fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n",
            obj_filename.c_str(), elf_header->e_machine);
    return false;
  }

  // Figure out what endianness this file is.
  bool big_endian;
  if (!ElfEndianness(elf_header, &big_endian)) return false;

  string name = BaseFileName(obj_filename);
  string os = "Linux";
  string id = FormatIdentifier(identifier);

  LoadSymbolsInfo info(debug_dirs);
  if (!LoadSymbols(obj_filename, big_endian, elf_header,
                             !debug_dirs.empty(), &info, smap, rx_avma, rx_size,
                             usu, log)) {
    const string debuglink_file = info.debuglink_file();
    if (debuglink_file.empty()) return false;

    // Load debuglink ELF file.
    fprintf(stderr, "Found debugging info in %s\n", debuglink_file.c_str());
    MmapWrapper debug_map_wrapper;
    Ehdr* debug_elf_header = NULL;
    if (!LoadELF(debuglink_file, &debug_map_wrapper,
                 reinterpret_cast(&debug_elf_header)))
      return false;
    // Sanity checks to make sure everything matches up.
    const char* debug_architecture =
        ElfArchitecture(debug_elf_header);
    if (!debug_architecture) {
      fprintf(stderr, "%s: unrecognized ELF machine architecture: %d\n",
              debuglink_file.c_str(), debug_elf_header->e_machine);
      return false;
    }
    if (strcmp(architecture, debug_architecture)) {
      fprintf(stderr,
              "%s with ELF machine architecture %s does not match "
              "%s with ELF architecture %s\n",
              debuglink_file.c_str(), debug_architecture, obj_filename.c_str(),
              architecture);
      return false;
    }

    bool debug_big_endian;
    if (!ElfEndianness(debug_elf_header, &debug_big_endian))
      return false;
    if (debug_big_endian != big_endian) {
      fprintf(stderr, "%s and %s does not match in endianness\n",
              obj_filename.c_str(), debuglink_file.c_str());
      return false;
    }

    if (!LoadSymbols(debuglink_file, debug_big_endian,
                               debug_elf_header, false, &info, smap, rx_avma,
                               rx_size, usu, log)) {
      return false;
    }
  }

  return true;
}

}  // namespace