printk.cc revision 2665
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 * 282665Ssaidi@eecs.umich.edu * Authors: Ali Saidi 291039SN/A */ 301039SN/A 311039SN/A#include <sys/types.h> 321039SN/A#include <algorithm> 331039SN/A 341039SN/A#include "base/trace.hh" 352170SN/A#include "arch/arguments.hh" 361039SN/A 371039SN/Ausing namespace std; 381039SN/A 391039SN/A 401039SN/Avoid 412521SN/APrintk(AlphaISA::AlphaArguments args) 421039SN/A{ 431039SN/A char *p = (char *)args++; 441039SN/A 451039SN/A ios::fmtflags saved_flags = DebugOut().flags(); 461039SN/A char old_fill = DebugOut().fill(); 471039SN/A int old_precision = DebugOut().precision(); 481039SN/A 491039SN/A while (*p) { 501039SN/A switch (*p) { 511039SN/A case '%': { 521039SN/A bool more = true; 531039SN/A bool islong = false; 541039SN/A bool leftjustify = false; 551039SN/A bool format = false; 561039SN/A bool zero = false; 571039SN/A int width = 0; 581039SN/A while (more && *++p) { 591039SN/A switch (*p) { 601039SN/A case 'l': 611039SN/A case 'L': 621039SN/A islong = true; 631039SN/A break; 641039SN/A case '-': 651039SN/A leftjustify = true; 661039SN/A break; 671039SN/A case '#': 681039SN/A format = true; 691039SN/A break; 701039SN/A case '0': 711039SN/A if (width) 721039SN/A width *= 10; 731039SN/A else 741039SN/A zero = true; 751039SN/A break; 761039SN/A default: 771039SN/A if (*p >= '1' && *p <= '9') 781039SN/A width = 10 * width + *p - '0'; 791039SN/A else 801039SN/A more = false; 811039SN/A break; 821039SN/A } 831039SN/A } 841039SN/A 851039SN/A bool hexnum = false; 861039SN/A bool octal = false; 871039SN/A bool sign = false; 881039SN/A switch (*p) { 891039SN/A case 'X': 901039SN/A case 'x': 911039SN/A hexnum = true; 921039SN/A break; 931039SN/A case 'O': 941039SN/A case 'o': 951039SN/A octal = true; 961039SN/A break; 971039SN/A case 'D': 981039SN/A case 'd': 991039SN/A sign = true; 1001039SN/A break; 1011039SN/A case 'P': 1021039SN/A format = true; 1031039SN/A case 'p': 1041039SN/A hexnum = true; 1051039SN/A break; 1061039SN/A } 1071039SN/A 1081039SN/A switch (*p) { 1091039SN/A case 'D': 1101039SN/A case 'd': 1111039SN/A case 'U': 1121039SN/A case 'u': 1131039SN/A case 'X': 1141039SN/A case 'x': 1151039SN/A case 'O': 1161039SN/A case 'o': 1171039SN/A case 'P': 1181039SN/A case 'p': { 1191039SN/A if (hexnum) 1201039SN/A DebugOut() << hex; 1211039SN/A 1221039SN/A if (octal) 1231039SN/A DebugOut() << oct; 1241039SN/A 1251039SN/A if (format) { 1261039SN/A if (!zero) 1271039SN/A DebugOut().setf(ios::showbase); 1281039SN/A else { 1291039SN/A if (hexnum) { 1301039SN/A DebugOut() << "0x"; 1311039SN/A width -= 2; 1321039SN/A } else if (octal) { 1331039SN/A DebugOut() << "0"; 1341039SN/A width -= 1; 1351039SN/A } 1361039SN/A } 1371039SN/A } 1381039SN/A 1391039SN/A if (zero) 1401039SN/A DebugOut().fill('0'); 1411039SN/A 1421039SN/A if (width > 0) 1431039SN/A DebugOut().width(width); 1441039SN/A 1451039SN/A if (leftjustify && !zero) 1461039SN/A DebugOut().setf(ios::left); 1471039SN/A 1481039SN/A if (sign) { 1491039SN/A if (islong) 1501039SN/A DebugOut() << (int64_t)args; 1511039SN/A else 1521039SN/A DebugOut() << (int32_t)args; 1531039SN/A } else { 1541039SN/A if (islong) 1551039SN/A DebugOut() << (uint64_t)args; 1561039SN/A else 1571039SN/A DebugOut() << (uint32_t)args; 1581039SN/A } 1591039SN/A 1601039SN/A if (zero) 1611039SN/A DebugOut().fill(' '); 1621039SN/A 1631039SN/A if (width > 0) 1641039SN/A DebugOut().width(0); 1651039SN/A 1661039SN/A DebugOut() << dec; 1671039SN/A 1681039SN/A ++args; 1691039SN/A } 1701039SN/A break; 1711039SN/A 1721039SN/A case 's': { 1731039SN/A char *s = (char *)args; 1741039SN/A if (!s) 1751039SN/A s = "<NULL>"; 1761039SN/A 1771039SN/A if (width > 0) 1781039SN/A DebugOut().width(width); 1791039SN/A if (leftjustify) 1801039SN/A DebugOut().setf(ios::left); 1811039SN/A 1821039SN/A DebugOut() << s; 1831039SN/A ++args; 1841039SN/A } 1851039SN/A break; 1861039SN/A case 'C': 1871039SN/A case 'c': { 1881039SN/A uint64_t mask = (*p == 'C') ? 0xffL : 0x7fL; 1891039SN/A uint64_t num; 1901039SN/A int width; 1911039SN/A 1921039SN/A if (islong) { 1931039SN/A num = (uint64_t)args; 1941039SN/A width = sizeof(uint64_t); 1951039SN/A } else { 1961039SN/A num = (uint32_t)args; 1971039SN/A width = sizeof(uint32_t); 1981039SN/A } 1991039SN/A 2001039SN/A while (width-- > 0) { 2011039SN/A char c = (char)(num & mask); 2021039SN/A if (c) 2031039SN/A DebugOut() << c; 2041039SN/A num >>= 8; 2051039SN/A } 2061039SN/A 2071039SN/A ++args; 2081039SN/A } 2091039SN/A break; 2101039SN/A case 'b': { 2111039SN/A uint64_t n = (uint64_t)args++; 2121039SN/A char *s = (char *)args++; 2131039SN/A DebugOut() << s << ": " << n; 2141039SN/A } 2151039SN/A break; 2161039SN/A case 'n': 2171039SN/A case 'N': { 2181039SN/A args += 2; 2191039SN/A#if 0 2201039SN/A uint64_t n = (uint64_t)args++; 2211039SN/A struct reg_values *rv = (struct reg_values *)args++; 2221039SN/A#endif 2231039SN/A } 2241039SN/A break; 2251039SN/A case 'r': 2261039SN/A case 'R': { 2271039SN/A args += 2; 2281039SN/A#if 0 2291039SN/A uint64_t n = (uint64_t)args++; 2301039SN/A struct reg_desc *rd = (struct reg_desc *)args++; 2311039SN/A#endif 2321039SN/A } 2331039SN/A break; 2341039SN/A case '%': 2351039SN/A DebugOut() << '%'; 2361039SN/A break; 2371039SN/A } 2381039SN/A ++p; 2391039SN/A } 2401039SN/A break; 2411039SN/A case '\n': 2421039SN/A DebugOut() << endl; 2431039SN/A ++p; 2441039SN/A break; 2451039SN/A case '\r': 2461039SN/A ++p; 2471039SN/A if (*p != '\n') 2481039SN/A DebugOut() << endl; 2491039SN/A break; 2501039SN/A 2511039SN/A default: { 2521039SN/A size_t len = strcspn(p, "%\n\r\0"); 2531039SN/A DebugOut().write(p, len); 2541039SN/A p += len; 2551039SN/A } 2561039SN/A } 2571039SN/A } 2581039SN/A 2591039SN/A DebugOut().flags(saved_flags); 2601039SN/A DebugOut().fill(old_fill); 2611039SN/A DebugOut().precision(old_precision); 2621039SN/A} 2631039SN/A 264