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