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
3211793Sbrandon.potter@amd.com#include "kern/linux/printk.hh"
3311793Sbrandon.potter@amd.com
341039SN/A#include <sys/types.h>
358229Snate@binkert.org
361039SN/A#include <algorithm>
371039SN/A
381039SN/A#include "base/trace.hh"
3910468Sandreas.hansson@arm.com#include "cpu/thread_context.hh"
408229Snate@binkert.org#include "sim/arguments.hh"
411039SN/A
421039SN/Ausing namespace std;
431039SN/A
441039SN/A
451039SN/Avoid
464826Ssaidi@eecs.umich.eduPrintk(stringstream &out, Arguments args)
471039SN/A{
481039SN/A    char *p = (char *)args++;
491039SN/A
501039SN/A    while (*p) {
511039SN/A        switch (*p) {
521039SN/A          case '%': {
531039SN/A              bool more = true;
541039SN/A              bool islong = false;
551039SN/A              bool leftjustify = false;
561039SN/A              bool format = false;
571039SN/A              bool zero = false;
581039SN/A              int width = 0;
591039SN/A              while (more && *++p) {
601039SN/A                  switch (*p) {
611039SN/A                    case 'l':
621039SN/A                    case 'L':
631039SN/A                      islong = true;
641039SN/A                      break;
651039SN/A                    case '-':
661039SN/A                      leftjustify = true;
671039SN/A                      break;
681039SN/A                    case '#':
691039SN/A                      format = true;
701039SN/A                      break;
711039SN/A                    case '0':
721039SN/A                      if (width)
731039SN/A                          width *= 10;
741039SN/A                      else
751039SN/A                          zero = true;
761039SN/A                      break;
771039SN/A                    default:
781039SN/A                      if (*p >= '1' && *p <= '9')
791039SN/A                          width = 10 * width + *p - '0';
801039SN/A                      else
811039SN/A                          more = false;
821039SN/A                      break;
831039SN/A                  }
841039SN/A              }
851039SN/A
861039SN/A              bool hexnum = false;
871039SN/A              bool octal = false;
881039SN/A              bool sign = false;
891039SN/A              switch (*p) {
901039SN/A                case 'X':
911039SN/A                case 'x':
921039SN/A                  hexnum = true;
931039SN/A                  break;
941039SN/A                case 'O':
951039SN/A                case 'o':
961039SN/A                  octal = true;
971039SN/A                  break;
981039SN/A                case 'D':
991039SN/A                case 'd':
1001039SN/A                  sign = true;
1011039SN/A                  break;
1021039SN/A                case 'P':
1031039SN/A                  format = true;
10412392Sjason@lowepower.com                  M5_FALLTHROUGH;
1051039SN/A                case 'p':
1061039SN/A                  hexnum = true;
1071039SN/A                  break;
1081039SN/A              }
1091039SN/A
1101039SN/A              switch (*p) {
1111039SN/A                case 'D':
1121039SN/A                case 'd':
1131039SN/A                case 'U':
1141039SN/A                case 'u':
1151039SN/A                case 'X':
1161039SN/A                case 'x':
1171039SN/A                case 'O':
1181039SN/A                case 'o':
1191039SN/A                case 'P':
1201039SN/A                case 'p': {
1211039SN/A                  if (hexnum)
1224046Sbinkertn@umich.edu                      out << hex;
1231039SN/A
1241039SN/A                  if (octal)
1254046Sbinkertn@umich.edu                      out << oct;
1261039SN/A
1271039SN/A                  if (format) {
1281039SN/A                      if (!zero)
1294046Sbinkertn@umich.edu                          out.setf(ios::showbase);
1301039SN/A                      else {
1311039SN/A                          if (hexnum) {
1324046Sbinkertn@umich.edu                              out << "0x";
1331039SN/A                              width -= 2;
1341039SN/A                          } else if (octal) {
1354046Sbinkertn@umich.edu                              out << "0";
1361039SN/A                              width -= 1;
1371039SN/A                          }
1381039SN/A                      }
1391039SN/A                  }
1401039SN/A
1411039SN/A                  if (zero)
1424046Sbinkertn@umich.edu                      out.fill('0');
1431039SN/A
1441039SN/A                  if (width > 0)
1454046Sbinkertn@umich.edu                      out.width(width);
1461039SN/A
1471039SN/A                  if (leftjustify && !zero)
1484046Sbinkertn@umich.edu                      out.setf(ios::left);
1491039SN/A
1501039SN/A                  if (sign) {
1511039SN/A                      if (islong)
1524046Sbinkertn@umich.edu                          out << (int64_t)args;
1531039SN/A                      else
1544046Sbinkertn@umich.edu                          out << (int32_t)args;
1551039SN/A                  } else {
1561039SN/A                      if (islong)
1574046Sbinkertn@umich.edu                          out << (uint64_t)args;
1581039SN/A                      else
1594046Sbinkertn@umich.edu                          out << (uint32_t)args;
1601039SN/A                  }
1611039SN/A
1621039SN/A                  if (zero)
1634046Sbinkertn@umich.edu                      out.fill(' ');
1641039SN/A
1651039SN/A                  if (width > 0)
1664046Sbinkertn@umich.edu                      out.width(0);
1671039SN/A
1684046Sbinkertn@umich.edu                  out << dec;
1691039SN/A
1701039SN/A                  ++args;
1711039SN/A                }
1721039SN/A                  break;
1731039SN/A
1741039SN/A                case 's': {
1759034Sandreas.hansson@arm.com                    const char *s = (char *)args;
1761039SN/A                    if (!s)
1771039SN/A                        s = "<NULL>";
1781039SN/A
1791039SN/A                    if (width > 0)
1804046Sbinkertn@umich.edu                        out.width(width);
1811039SN/A                    if (leftjustify)
1824046Sbinkertn@umich.edu                        out.setf(ios::left);
1831039SN/A
1844046Sbinkertn@umich.edu                    out << s;
1851039SN/A                    ++args;
1861039SN/A                }
1871039SN/A                  break;
1881039SN/A                case 'C':
1891039SN/A                case 'c': {
1901039SN/A                    uint64_t mask = (*p == 'C') ? 0xffL : 0x7fL;
1911039SN/A                    uint64_t num;
1929550Sandreas.hansson@arm.com                    int cwidth;
1931039SN/A
1941039SN/A                    if (islong) {
1951039SN/A                        num = (uint64_t)args;
1969550Sandreas.hansson@arm.com                        cwidth = sizeof(uint64_t);
1971039SN/A                    } else {
1981039SN/A                        num = (uint32_t)args;
1999550Sandreas.hansson@arm.com                        cwidth = sizeof(uint32_t);
2001039SN/A                    }
2011039SN/A
2029550Sandreas.hansson@arm.com                    while (cwidth-- > 0) {
2031039SN/A                        char c = (char)(num & mask);
2041039SN/A                        if (c)
2054046Sbinkertn@umich.edu                            out << c;
2061039SN/A                        num >>= 8;
2071039SN/A                    }
2081039SN/A
2091039SN/A                    ++args;
2101039SN/A                }
2111039SN/A                  break;
2121039SN/A                case 'b': {
2131039SN/A                  uint64_t n = (uint64_t)args++;
2141039SN/A                  char *s = (char *)args++;
2154046Sbinkertn@umich.edu                  out << s << ": " << n;
2161039SN/A                }
2171039SN/A                  break;
2181039SN/A                case 'n':
2191039SN/A                case 'N': {
2201039SN/A                    args += 2;
2211039SN/A                }
2221039SN/A                  break;
2231039SN/A                case 'r':
2241039SN/A                case 'R': {
2251039SN/A                    args += 2;
2261039SN/A                }
2271039SN/A                  break;
2281039SN/A                case '%':
2294046Sbinkertn@umich.edu                  out << '%';
2301039SN/A                  break;
2311039SN/A              }
2321039SN/A              ++p;
2331039SN/A          }
2341039SN/A            break;
2351039SN/A          case '\n':
2364046Sbinkertn@umich.edu            out << endl;
2371039SN/A            ++p;
2381039SN/A            break;
2391039SN/A          case '\r':
2401039SN/A            ++p;
2411039SN/A            if (*p != '\n')
2424046Sbinkertn@umich.edu                out << endl;
2431039SN/A            break;
2441039SN/A
2451039SN/A          default: {
2461039SN/A              size_t len = strcspn(p, "%\n\r\0");
2474046Sbinkertn@umich.edu              out.write(p, len);
2481039SN/A              p += len;
2491039SN/A          }
2501039SN/A        }
2511039SN/A    }
2521039SN/A
2531039SN/A}
254