/ src / client / linux / minidump_writer / proc_cpuinfo_reader.h
proc_cpuinfo_reader.h
  1  // Copyright 2013 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  #ifndef CLIENT_LINUX_MINIDUMP_WRITER_PROC_CPUINFO_READER_H_
 30  #define CLIENT_LINUX_MINIDUMP_WRITER_PROC_CPUINFO_READER_H_
 31  
 32  #include <stdint.h>
 33  #include <assert.h>
 34  #include <string.h>
 35  
 36  #include "client/linux/minidump_writer/line_reader.h"
 37  #include "common/linux/linux_libc_support.h"
 38  #include "third_party/lss/linux_syscall_support.h"
 39  
 40  namespace google_breakpad {
 41  
 42  // A class for reading /proc/cpuinfo without using fopen/fgets or other
 43  // functions which may allocate memory.
 44  class ProcCpuInfoReader {
 45  public:
 46    ProcCpuInfoReader(int fd)
 47      : line_reader_(fd), pop_count_(-1) {
 48    }
 49  
 50    // Return the next field name, or NULL in case of EOF.
 51    // field: (output) Pointer to zero-terminated field name.
 52    // Returns true on success, or false on EOF or error (line too long).
 53    bool GetNextField(const char** field) {
 54      for (;;) {
 55        const char* line;
 56        unsigned line_len;
 57  
 58        // Try to read next line.
 59        if (pop_count_ >= 0) {
 60          line_reader_.PopLine(pop_count_);
 61          pop_count_ = -1;
 62        }
 63  
 64        if (!line_reader_.GetNextLine(&line, &line_len))
 65          return false;
 66  
 67        pop_count_ = static_cast<int>(line_len);
 68  
 69        const char* line_end = line + line_len;
 70  
 71        // Expected format: <field-name> <space>+ ':' <space> <value>
 72        // Note that:
 73        //   - empty lines happen.
 74        //   - <field-name> can contain spaces.
 75        //   - some fields have an empty <value>
 76        char* sep = static_cast<char*>(my_memchr(line, ':', line_len));
 77        if (sep == NULL)
 78          continue;
 79  
 80        // Record the value. Skip leading space after the column to get
 81        // its start.
 82        const char* val = sep+1;
 83        while (val < line_end && my_isspace(*val))
 84          val++;
 85  
 86        value_ = val;
 87        value_len_ = static_cast<size_t>(line_end - val);
 88  
 89        // Remove trailing spaces before the column to properly 0-terminate
 90        // the field name.
 91        while (sep > line && my_isspace(sep[-1]))
 92          sep--;
 93  
 94        if (sep == line)
 95          continue;
 96  
 97        // zero-terminate field name.
 98        *sep = '\0';
 99  
100        *field = line;
101        return true;
102      }
103    }
104  
105    // Return the field value. This must be called after a succesful
106    // call to GetNextField().
107    const char* GetValue() {
108      assert(value_);
109      return value_;
110    }
111  
112    // Same as GetValue(), but also returns the length in characters of
113    // the value.
114    const char* GetValueAndLen(size_t* length) {
115      assert(value_);
116      *length = value_len_;
117      return value_;
118    }
119  
120  private:
121    LineReader line_reader_;
122    int pop_count_;
123    const char* value_;
124    size_t value_len_;
125  };
126  
127  }  // namespace google_breakpad
128  
129  #endif  // CLIENT_LINUX_MINIDUMP_WRITER_PROC_CPUINFO_READER_H_