helpers.cc revision 11538:55014a40512c
1/* 2 * Copyright (c) 2016 ARM Limited 3 * All rights reserved 4 * 5 * The license below extends only to copyright in the software and shall 6 * not be construed as granting a license to any other intellectual 7 * property including but not limited to intellectual property relating 8 * to a hardware implementation of the functionality of the software 9 * licensed hereunder. You may use the software subject to the license 10 * terms below provided that you ensure that this notice is replicated 11 * unmodified and in its entirety in all distributions of the software, 12 * modified or unmodified, in source code or in binary form. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions are 16 * met: redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer; 18 * redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution; 21 * neither the name of the copyright holders nor the names of its 22 * contributors may be used to endorse or promote products derived from 23 * this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 * 37 * Authors: Andreas Sandberg 38 */ 39 40#include "kern/linux/helpers.hh" 41 42#include "arch/isa_traits.hh" 43#include "config/the_isa.hh" 44#include "cpu/thread_context.hh" 45#include "mem/fs_translating_port_proxy.hh" 46#include "sim/system.hh" 47 48struct DmesgEntry { 49 uint64_t ts_nsec; 50 uint16_t len; 51 uint16_t text_len; 52 uint16_t dict_len; 53 uint8_t facility; 54 uint8_t flags; 55} M5_ATTR_PACKED; 56 57static int 58dumpDmesgEntry(const uint8_t *base, const uint8_t *end, std::ostream &os) 59{ 60 const size_t max_length = end - base; 61 DmesgEntry de; 62 63 if (max_length < sizeof(de)) { 64 warn("Malformed dmesg entry\n"); 65 return -1; 66 } 67 68 memcpy(&de, base, sizeof(de)); 69 de.ts_nsec = TheISA::gtoh(de.ts_nsec); 70 de.len = TheISA::gtoh(de.len); 71 de.text_len = TheISA::gtoh(de.text_len); 72 73 if (de.len < sizeof(de) || 74 max_length < de.len || 75 max_length < sizeof(DmesgEntry) + de.text_len) { 76 77 warn("Malformed dmesg entry:\n"); 78 warn("\tMax length: %i\n", max_length); 79 warn("\tde.len: %i\n", de.len); 80 warn("\tde.text_len: %i\n", de.text_len); 81 return -1; 82 } 83 84 ccprintf(os, "[%.6f] ", de.ts_nsec * 10e-9); 85 os.write((char *)base + sizeof(de), de.text_len); 86 os << std::endl; 87 88 return de.len; 89} 90 91void 92Linux::dumpDmesg(ThreadContext *tc, std::ostream &os) 93{ 94 System *system = tc->getSystemPtr(); 95 const SymbolTable *symtab = system->kernelSymtab; 96 FSTranslatingPortProxy proxy(tc); 97 98 Addr addr_lb = 0, addr_lb_len = 0, addr_first = 0, addr_next = 0; 99 const bool found_symbols = 100 symtab->findAddress("__log_buf", addr_lb) && 101 symtab->findAddress("log_buf_len", addr_lb_len) && 102 symtab->findAddress("log_first_idx", addr_first) && 103 symtab->findAddress("log_next_idx", addr_next); 104 105 if (!found_symbols) { 106 warn("Failed to find kernel dmesg symbols.\n"); 107 return; 108 } 109 110 uint32_t log_buf_len = proxy.readGtoH<uint32_t>(addr_lb_len); 111 uint32_t log_first_idx = proxy.readGtoH<uint32_t>(addr_first); 112 uint32_t log_next_idx = proxy.readGtoH<uint32_t>(addr_next); 113 114 if (log_first_idx >= log_buf_len || log_next_idx >= log_buf_len) { 115 warn("dmesg pointers/length corrupted\n"); 116 return; 117 } 118 119 // Normalize and read the dmesg ring buffer 120 std::vector<uint8_t> log_buf(log_buf_len); 121 int length; 122 if (log_first_idx < log_next_idx) { 123 length = log_next_idx - log_first_idx; 124 if (length < 0 || length > log_buf.size()) { 125 warn("Unexpected dmesg buffer length\n"); 126 return; 127 } 128 proxy.readBlob(addr_lb + log_first_idx, log_buf.data(), length); 129 } else { 130 const int length_2 = log_buf_len - log_first_idx; 131 if (length_2 < 0 || length_2 + log_next_idx > log_buf.size()) { 132 warn("Unexpected dmesg buffer length\n"); 133 return; 134 } 135 length = log_buf_len; 136 proxy.readBlob(addr_lb + log_first_idx, log_buf.data(), length_2); 137 proxy.readBlob(addr_lb, log_buf.data() + length_2, log_next_idx); 138 } 139 140 // Print dmesg buffer content 141 const uint8_t *cur = log_buf.data(), *end = log_buf.data() + length; 142 while (cur < end) { 143 int ret = dumpDmesgEntry(cur, end, os); 144 if (ret < 0) 145 return; 146 cur += ret; 147 } 148} 149