Ejemplo n.º 1
0
MMap::MMap(std::stringstream& ss)
{
    std::string map_declaration;
    std::getline(ss, map_declaration);
    // parse declaration
    boost::regex regex_declare_mapping("^([[:xdigit:]]+)-([[:xdigit:]]+)\\s([r-])([w-])([x-])([sp])\\s([[:xdigit:]]+)\\s([[:xdigit:]]+):([[:xdigit:]]+)\\s([[:digit:]]+)\\s+(.*)$");
    boost::regex regex_key_value("^([[:alpha:]]+):\\s+(.*)$");
    boost::smatch match;
    if (boost::regex_match(map_declaration, match, regex_declare_mapping))
    {
        if (match.size() == 11 + 1)
        {
            m_address_from = match[1];
            m_address_to = match[2];
            (match[3] == "r") ? m_perm_read = true : m_perm_read = false;
            (match[4] == "w") ? m_perm_write = true : m_perm_write = false;
            (match[5] == "x") ? m_perm_execute = true : m_perm_execute = false;
            (match[6] == "p") ? m_type = priv : m_type = shared;
            m_offset = std::stol(match[7]);
            m_dev_major = std::stoi(match[8], 0, 16);
            m_dev_minor = std::stoi(match[9], 0, 16);
            m_inode = std::stol(match[10]);
            m_pathname = match[11];
        }
    }
    // parse attributes
    std::string line;
    while (std::getline(ss, line))
    {
        if (boost::regex_match(line, match, regex_key_value))
        {
            if (match.size() == 2 + 1)
            {
                std::string value(match[2]);
                boost::algorithm::trim(value);
                boost::regex regex_value("^([[:digit:]])\\s.*$");
                boost::smatch match_value;
                if (boost::regex_match(value, match_value, regex_value))
                {
                    // 4 kB
                    if (match[1] == "Size")
                        m_size = std::stoi(match_value[1]);
                    else if (match[1] == "Rss")
                        m_rss = std::stoi(match_value[1]);
                    else if (match[1] == "Pss")
                        m_pss = std::stoi(match_value[1]);
                    else if (match[1] == "Shared_Clean")
                        m_shared_clean = std::stoi(match_value[1]);
                    else if (match[1] == "Shared_Dirty")
                        m_shared_dirty = std::stoi(match_value[1]);
                    else if (match[1] == "Private_Clean")
                        m_private_clean = std::stoi(match_value[1]);
                    else if (match[1] == "Private_Dirty")
                        m_private_dirty = std::stoi(match_value[1]);
                    else if (match[1] == "Referenced")
                        m_referenced = std::stoi(match_value[1]);
                    else if (match[1] == "Anonymous")
                        m_anonymous = std::stoi(match_value[1]);
                    else if (match[1] == "AnonHugePages")
                        m_anonhugepages = std::stoi(match_value[1]);
                    else if (match[1] == "Swap")
                        m_swap = std::stoi(match_value[1]);
                    else if (match[1] == "KernelPageSize")
                        m_kernelpagesize = std::stoi(match_value[1]);
                    else if (match[1] == "MMUPageSize")
                        m_mmupagesize = std::stoi(match_value[1]);
                    else if (match[1] == "Locked")
                        m_locked = std::stoi(match_value[1]);
                } else
                {
                    // VmFlags special case
                    boost::split(m_vmflags, value, boost::is_any_of(" "));
                }
            }
        }
    }
    defineCategory();
}
Ejemplo n.º 2
0
extern PSBCategoryList *parseFile(char *catFile, bool verbose){
  FILE *file;
  char firstBuffer[256], secondBuffer[256], catNameBuffer[256], modelBuffer[256];
  int num, numCategories, currentNumCategories, numFound, i;
  int numModels, currNumModels, currNumTotalModels, numTotalModels;
  PSBCategoryList *categoryList;

  file = fopen(catFile, "r");
  if (file == NULL){
    fprintf(stderr, "trouble opening %s\n", catFile);
    exit(1);
  }

  // check header
  if(fscanf(file, "%s %d", firstBuffer, &num) != 2 || strcmp(PSB, firstBuffer) || num > CURR_FORMAT){
    error(file, "%s is not a PSB file of format %d\n", catFile, CURR_FORMAT);
  }

  // get num categories
  if (fscanf(file, "%d %d", &numCategories, &numTotalModels) != 2){
    error(file, "%s does not list the number of categories or models\n", catFile);
  }

  if (numCategories < 0 || numTotalModels < 0){
    error(file, "%d categories and %d models is invalid\n", numCategories, numTotalModels);
  }
  

  categoryList = (PSBCategoryList*)malloc(sizeof(PSBCategoryList));
  assert(categoryList != NULL);
  categoryList->_numCategories = numCategories;
  categoryList->_categories = (PSBCategory **)malloc(numCategories * sizeof(PSBCategory*));
  assert(categoryList != NULL);

  currentNumCategories = 0;
  currNumTotalModels = 0;

  while(1){
    numFound = fscanf(file, "%s %s %d", firstBuffer, secondBuffer, &numModels);
    
    if (numFound == 0 || numFound == EOF){
      break; // end of file
    }else if (numFound == 1){
      error(file, "poorly formated category line, %s\n", firstBuffer);
    }else if (numFound == 2){
      error(file, "poorly formated category line, %s %s\n", firstBuffer, secondBuffer);
    }else if (numFound > 3){
      error(file, "poorly formated category line\n");
    }

    if (numModels < 0){
      error(file, "%d is an invalid number of categories\n", numModels);
    }

    if (isCategoryDefined(categoryList, firstBuffer, currentNumCategories)){
      error(file, "%s is already a defined category\n", firstBuffer);
    }

    if (!isCategoryDefined(categoryList, secondBuffer, currentNumCategories)){
      error(file, "%s is not a defined category\n", secondBuffer);
    }


    createFullName(catNameBuffer, firstBuffer, secondBuffer, categoryList, currentNumCategories);

    categoryList->_categories[currentNumCategories] = defineCategory(firstBuffer, catNameBuffer, numModels);

    if (verbose) printf("processing category %s, ", firstBuffer);

    currNumModels = 0;
    
    for(i = 0; i < numModels; ++i){
      numFound = fscanf(file,"%s", modelBuffer);
      if (numFound != 1){
        error(file, "%s has an incorrect number of models, read %d, should be %d\n", firstBuffer, currNumModels, numModels);
      }
      categoryList->_categories[currentNumCategories]->_models[currNumModels] = strdup(modelBuffer);
      currNumModels++;
      currNumTotalModels++;
    }
    if (verbose) printf("finished.\n");
    ++currentNumCategories;

  }
  
  if (currentNumCategories != numCategories){
    error(file, "expected %d categories, found %d\n", numCategories, currentNumCategories);
  }

  if (currNumTotalModels != numTotalModels){
    error(file, "expected %d models, found %d\n", numTotalModels, currNumTotalModels);
  }

  
  if (fclose(file) == EOF){
    fprintf(stderr, "trouble closing %s\n", catFile);
  }

  if (verbose) printf("Validated %s, %d categories, %d models\n", catFile, numCategories, currNumTotalModels);
  return categoryList;
}