printk.cc revision 8229
11039SN/A/*
21762SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan
31039SN/A * All rights reserved.
41039SN/A *
51039SN/A * Redistribution and use in source and binary forms, with or without
61039SN/A * modification, are permitted provided that the following conditions are
71039SN/A * met: redistributions of source code must retain the above copyright
81039SN/A * notice, this list of conditions and the following disclaimer;
91039SN/A * redistributions in binary form must reproduce the above copyright
101039SN/A * notice, this list of conditions and the following disclaimer in the
111039SN/A * documentation and/or other materials provided with the distribution;
121039SN/A * neither the name of the copyright holders nor the names of its
131039SN/A * contributors may be used to endorse or promote products derived from
141039SN/A * this software without specific prior written permission.
151039SN/A *
161039SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171039SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181039SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191039SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201039SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211039SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221039SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231039SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241039SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251039SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261039SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282760Sbinkertn@umich.edu * Authors: Nathan Binkert
292760Sbinkertn@umich.edu *          Ali Saidi
301039SN/A */
311039SN/A
321039SN/A#include <sys/types.h>
338229Snate@binkert.org
341039SN/A#include <algorithm>
351039SN/A
361039SN/A#include "base/trace.hh"
374429Ssaidi@eecs.umich.edu#include "kern/linux/printk.hh"
388229Snate@binkert.org#include "sim/arguments.hh"
391039SN/A
401039SN/Ausing namespace std;
411039SN/A
421039SN/A
431039SN/Avoid
444826Ssaidi@eecs.umich.eduPrintk(stringstream &out, Arguments args)
451039SN/A{
461039SN/A    char *p = (char *)args++;
471039SN/A
481039SN/A    while (*p) {
491039SN/A        switch (*p) {
501039SN/A          case '%': {
511039SN/A              bool more = true;
521039SN/A              bool islong = false;
531039SN/A              bool leftjustify = false;
541039SN/A              bool format = false;
551039SN/A              bool zero = false;
561039SN/A              int width = 0;
571039SN/A              while (more && *++p) {
581039SN/A                  switch (*p) {
591039SN/A                    case 'l':
601039SN/A                    case 'L':
611039SN/A                      islong = true;
621039SN/A                      break;
631039SN/A                    case '-':
641039SN/A                      leftjustify = true;
651039SN/A                      break;
661039SN/A                    case '#':
671039SN/A                      format = true;
681039SN/A                      break;
691039SN/A                    case '0':
701039SN/A                      if (width)
711039SN/A                          width *= 10;
721039SN/A                      else
731039SN/A                          zero = true;
741039SN/A                      break;
751039SN/A                    default:
761039SN/A                      if (*p >= '1' && *p <= '9')
771039SN/A                          width = 10 * width + *p - '0';
781039SN/A                      else
791039SN/A                          more = false;
801039SN/A                      break;
811039SN/A                  }
821039SN/A              }
831039SN/A
841039SN/A              bool hexnum = false;
851039SN/A              bool octal = false;
861039SN/A              bool sign = false;
871039SN/A              switch (*p) {
881039SN/A                case 'X':
891039SN/A                case 'x':
901039SN/A                  hexnum = true;
911039SN/A                  break;
921039SN/A                case 'O':
931039SN/A                case 'o':
941039SN/A                  octal = true;
951039SN/A                  break;
961039SN/A                case 'D':
971039SN/A                case 'd':
981039SN/A                  sign = true;
991039SN/A                  break;
1001039SN/A                case 'P':
1011039SN/A                  format = true;
1021039SN/A                case 'p':
1031039SN/A                  hexnum = true;
1041039SN/A                  break;
1051039SN/A              }
1061039SN/A
1071039SN/A              switch (*p) {
1081039SN/A                case 'D':
1091039SN/A                case 'd':
1101039SN/A                case 'U':
1111039SN/A                case 'u':
1121039SN/A                case 'X':
1131039SN/A                case 'x':
1141039SN/A                case 'O':
1151039SN/A                case 'o':
1161039SN/A                case 'P':
1171039SN/A                case 'p': {
1181039SN/A                  if (hexnum)
1194046Sbinkertn@umich.edu                      out << hex;
1201039SN/A
1211039SN/A                  if (octal)
1224046Sbinkertn@umich.edu                      out << oct;
1231039SN/A
1241039SN/A                  if (format) {
1251039SN/A                      if (!zero)
1264046Sbinkertn@umich.edu                          out.setf(ios::showbase);
1271039SN/A                      else {
1281039SN/A                          if (hexnum) {
1294046Sbinkertn@umich.edu                              out << "0x";
1301039SN/A                              width -= 2;
1311039SN/A                          } else if (octal) {
1324046Sbinkertn@umich.edu                              out << "0";
1331039SN/A                              width -= 1;
1341039SN/A                          }
1351039SN/A                      }
1361039SN/A                  }
1371039SN/A
1381039SN/A                  if (zero)
1394046Sbinkertn@umich.edu                      out.fill('0');
1401039SN/A
1411039SN/A                  if (width > 0)
1424046Sbinkertn@umich.edu                      out.width(width);
1431039SN/A
1441039SN/A                  if (leftjustify && !zero)
1454046Sbinkertn@umich.edu                      out.setf(ios::left);
1461039SN/A
1471039SN/A                  if (sign) {
1481039SN/A                      if (islong)
1494046Sbinkertn@umich.edu                          out << (int64_t)args;
1501039SN/A                      else
1514046Sbinkertn@umich.edu                          out << (int32_t)args;
1521039SN/A                  } else {
1531039SN/A                      if (islong)
1544046Sbinkertn@umich.edu                          out << (uint64_t)args;
1551039SN/A                      else
1564046Sbinkertn@umich.edu                          out << (uint32_t)args;
1571039SN/A                  }
1581039SN/A
1591039SN/A                  if (zero)
1604046Sbinkertn@umich.edu                      out.fill(' ');
1611039SN/A
1621039SN/A                  if (width > 0)
1634046Sbinkertn@umich.edu                      out.width(0);
1641039SN/A
1654046Sbinkertn@umich.edu                  out << dec;
1661039SN/A
1671039SN/A                  ++args;
1681039SN/A                }
1691039SN/A                  break;
1701039SN/A
1711039SN/A                case 's': {
1725202Sstever@gmail.com                    const char *s = (const char *)args;
1731039SN/A                    if (!s)
1741039SN/A                        s = "<NULL>";
1751039SN/A
1761039SN/A                    if (width > 0)
1774046Sbinkertn@umich.edu                        out.width(width);
1781039SN/A                    if (leftjustify)
1794046Sbinkertn@umich.edu                        out.setf(ios::left);
1801039SN/A
1814046Sbinkertn@umich.edu                    out << s;
1821039SN/A                    ++args;
1831039SN/A                }
1841039SN/A                  break;
1851039SN/A                case 'C':
1861039SN/A                case 'c': {
1871039SN/A                    uint64_t mask = (*p == 'C') ? 0xffL : 0x7fL;
1881039SN/A                    uint64_t num;
1891039SN/A                    int width;
1901039SN/A
1911039SN/A                    if (islong) {
1921039SN/A                        num = (uint64_t)args;
1931039SN/A                        width = sizeof(uint64_t);
1941039SN/A                    } else {
1951039SN/A                        num = (uint32_t)args;
1961039SN/A                        width = sizeof(uint32_t);
1971039SN/A                    }
1981039SN/A
1991039SN/A                    while (width-- > 0) {
2001039SN/A                        char c = (char)(num & mask);
2011039SN/A                        if (c)
2024046Sbinkertn@umich.edu                            out << c;
2031039SN/A                        num >>= 8;
2041039SN/A                    }
2051039SN/A
2061039SN/A                    ++args;
2071039SN/A                }
2081039SN/A                  break;
2091039SN/A                case 'b': {
2101039SN/A                  uint64_t n = (uint64_t)args++;
2111039SN/A                  char *s = (char *)args++;
2124046Sbinkertn@umich.edu                  out << s << ": " << n;
2131039SN/A                }
2141039SN/A                  break;
2151039SN/A                case 'n':
2161039SN/A                case 'N': {
2171039SN/A                    args += 2;
2181039SN/A#if 0
2191039SN/A                    uint64_t n = (uint64_t)args++;
2201039SN/A                    struct reg_values *rv = (struct reg_values *)args++;
2211039SN/A#endif
2221039SN/A                }
2231039SN/A                  break;
2241039SN/A                case 'r':
2251039SN/A                case 'R': {
2261039SN/A                    args += 2;
2271039SN/A#if 0
2281039SN/A                    uint64_t n = (uint64_t)args++;
2291039SN/A                    struct reg_desc *rd = (struct reg_desc *)args++;
2301039SN/A#endif
2311039SN/A                }
2321039SN/A                  break;
2331039SN/A                case '%':
2344046Sbinkertn@umich.edu                  out << '%';
2351039SN/A                  break;
2361039SN/A              }
2371039SN/A              ++p;
2381039SN/A          }
2391039SN/A            break;
2401039SN/A          case '\n':
2414046Sbinkertn@umich.edu            out << endl;
2421039SN/A            ++p;
2431039SN/A            break;
2441039SN/A          case '\r':
2451039SN/A            ++p;
2461039SN/A            if (*p != '\n')
2474046Sbinkertn@umich.edu                out << endl;
2481039SN/A            break;
2491039SN/A
2501039SN/A          default: {
2511039SN/A              size_t len = strcspn(p, "%\n\r\0");
2524046Sbinkertn@umich.edu              out.write(p, len);
2531039SN/A              p += len;
2541039SN/A          }
2551039SN/A        }
2561039SN/A    }
2571039SN/A
2581039SN/A}
2591039SN/A
260