threadinfo.hh revision 14018
19329Sdam.sunwoo@arm.com/* 29329Sdam.sunwoo@arm.com * Copyright (c) 2004 The Regents of The University of Michigan 39329Sdam.sunwoo@arm.com * All rights reserved. 49329Sdam.sunwoo@arm.com * 59329Sdam.sunwoo@arm.com * Redistribution and use in source and binary forms, with or without 69329Sdam.sunwoo@arm.com * modification, are permitted provided that the following conditions are 79329Sdam.sunwoo@arm.com * met: redistributions of source code must retain the above copyright 89329Sdam.sunwoo@arm.com * notice, this list of conditions and the following disclaimer; 99329Sdam.sunwoo@arm.com * redistributions in binary form must reproduce the above copyright 109329Sdam.sunwoo@arm.com * notice, this list of conditions and the following disclaimer in the 119329Sdam.sunwoo@arm.com * documentation and/or other materials provided with the distribution; 129329Sdam.sunwoo@arm.com * neither the name of the copyright holders nor the names of its 139329Sdam.sunwoo@arm.com * contributors may be used to endorse or promote products derived from 149329Sdam.sunwoo@arm.com * this software without specific prior written permission. 159329Sdam.sunwoo@arm.com * 169329Sdam.sunwoo@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 179329Sdam.sunwoo@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 189329Sdam.sunwoo@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 199329Sdam.sunwoo@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 209329Sdam.sunwoo@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 219329Sdam.sunwoo@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 229329Sdam.sunwoo@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 239329Sdam.sunwoo@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 249329Sdam.sunwoo@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 259329Sdam.sunwoo@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 269329Sdam.sunwoo@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 279329Sdam.sunwoo@arm.com * 289329Sdam.sunwoo@arm.com * Authors: Ali Saidi 299329Sdam.sunwoo@arm.com * Nathan Binkert 309329Sdam.sunwoo@arm.com * Dam Sunwoo 319329Sdam.sunwoo@arm.com */ 329329Sdam.sunwoo@arm.com 339329Sdam.sunwoo@arm.com#ifndef __ARCH_GENERIC_LINUX_THREADINFO_HH__ 349329Sdam.sunwoo@arm.com#define __ARCH_GENERIC_LINUX_THREADINFO_HH__ 359329Sdam.sunwoo@arm.com 369329Sdam.sunwoo@arm.com#include "cpu/thread_context.hh" 379329Sdam.sunwoo@arm.com#include "sim/system.hh" 389329Sdam.sunwoo@arm.com#include "sim/vptr.hh" 399329Sdam.sunwoo@arm.com 409329Sdam.sunwoo@arm.comnamespace Linux { 419329Sdam.sunwoo@arm.com 429329Sdam.sunwoo@arm.comclass ThreadInfo 439329Sdam.sunwoo@arm.com{ 449329Sdam.sunwoo@arm.com private: 459329Sdam.sunwoo@arm.com ThreadContext *tc; 469329Sdam.sunwoo@arm.com System *sys; 479329Sdam.sunwoo@arm.com Addr pcbb; 489329Sdam.sunwoo@arm.com 499329Sdam.sunwoo@arm.com template <typename T> 509329Sdam.sunwoo@arm.com bool 519329Sdam.sunwoo@arm.com get_data(const char *symbol, T &data) 529329Sdam.sunwoo@arm.com { 539329Sdam.sunwoo@arm.com Addr addr = 0; 549329Sdam.sunwoo@arm.com if (!sys->kernelSymtab->findAddress(symbol, addr)) { 559329Sdam.sunwoo@arm.com warn_once("Unable to find kernel symbol %s\n", symbol); 569329Sdam.sunwoo@arm.com warn_once("Kernel not compiled with task_struct info; can't get " 579329Sdam.sunwoo@arm.com "currently executing task/process/thread name/ids!\n"); 589329Sdam.sunwoo@arm.com return false; 599329Sdam.sunwoo@arm.com } 609329Sdam.sunwoo@arm.com 6114018Sgabeblack@google.com data = tc->getVirtProxy().read<T>(addr, TheISA::GuestByteOrder); 629329Sdam.sunwoo@arm.com 639329Sdam.sunwoo@arm.com return true; 649329Sdam.sunwoo@arm.com } 659329Sdam.sunwoo@arm.com 669329Sdam.sunwoo@arm.com public: 679329Sdam.sunwoo@arm.com ThreadInfo(ThreadContext *_tc, Addr _pcbb = 0) 689329Sdam.sunwoo@arm.com : tc(_tc), sys(tc->getSystemPtr()), pcbb(_pcbb) 699329Sdam.sunwoo@arm.com { 709329Sdam.sunwoo@arm.com 719329Sdam.sunwoo@arm.com } 729329Sdam.sunwoo@arm.com ~ThreadInfo() 739329Sdam.sunwoo@arm.com {} 749329Sdam.sunwoo@arm.com 759329Sdam.sunwoo@arm.com inline Addr 769329Sdam.sunwoo@arm.com curThreadInfo() 779329Sdam.sunwoo@arm.com { 789329Sdam.sunwoo@arm.com if (!TheISA::CurThreadInfoImplemented) 799329Sdam.sunwoo@arm.com panic("curThreadInfo() not implemented for this ISA"); 809329Sdam.sunwoo@arm.com 819329Sdam.sunwoo@arm.com Addr addr = pcbb; 829329Sdam.sunwoo@arm.com Addr sp; 839329Sdam.sunwoo@arm.com 849329Sdam.sunwoo@arm.com if (!addr) 859329Sdam.sunwoo@arm.com addr = tc->readMiscRegNoEffect(TheISA::CurThreadInfoReg); 869329Sdam.sunwoo@arm.com 879329Sdam.sunwoo@arm.com PortProxy &p = tc->getPhysProxy(); 8814010Sgabeblack@google.com p.readBlob(addr, &sp, sizeof(Addr)); 899329Sdam.sunwoo@arm.com 909329Sdam.sunwoo@arm.com return sp & ~ULL(0x3fff); 919329Sdam.sunwoo@arm.com } 929329Sdam.sunwoo@arm.com 939329Sdam.sunwoo@arm.com inline Addr 949329Sdam.sunwoo@arm.com curTaskInfo(Addr thread_info = 0) 959329Sdam.sunwoo@arm.com { 9612090Sprosenfeld@micron.com // Note that in Linux 4.10 the thread_info struct will no longer have a 9712090Sprosenfeld@micron.com // pointer to the task_struct for arm64. See: 9812090Sprosenfeld@micron.com // https://patchwork.kernel.org/patch/9333699/ 9914018Sgabeblack@google.com int32_t offset = 0; 1009329Sdam.sunwoo@arm.com if (!get_data("thread_info_task", offset)) 1019329Sdam.sunwoo@arm.com return 0; 1029329Sdam.sunwoo@arm.com 1039329Sdam.sunwoo@arm.com if (!thread_info) 1049329Sdam.sunwoo@arm.com thread_info = curThreadInfo(); 1059329Sdam.sunwoo@arm.com 10614018Sgabeblack@google.com return tc->getVirtProxy().read<Addr>(thread_info + offset); 1079329Sdam.sunwoo@arm.com } 1089329Sdam.sunwoo@arm.com 1099329Sdam.sunwoo@arm.com int32_t 11012090Sprosenfeld@micron.com curTaskPIDFromTaskStruct(Addr task_struct) { 11114018Sgabeblack@google.com int32_t offset = 0; 1129329Sdam.sunwoo@arm.com if (!get_data("task_struct_pid", offset)) 1139329Sdam.sunwoo@arm.com return -1; 1149329Sdam.sunwoo@arm.com 11514018Sgabeblack@google.com return tc->getVirtProxy().read<int32_t>(task_struct + offset); 1169329Sdam.sunwoo@arm.com } 1179329Sdam.sunwoo@arm.com 1189329Sdam.sunwoo@arm.com int32_t 11912090Sprosenfeld@micron.com curTaskPID(Addr thread_info = 0) 12012090Sprosenfeld@micron.com { 12112090Sprosenfeld@micron.com return curTaskPIDFromTaskStruct(curTaskInfo(thread_info)); 12212090Sprosenfeld@micron.com } 12312090Sprosenfeld@micron.com 12412090Sprosenfeld@micron.com int32_t 12512090Sprosenfeld@micron.com curTaskTGIDFromTaskStruct(Addr task_struct) 1269329Sdam.sunwoo@arm.com { 12714018Sgabeblack@google.com int32_t offset = 0; 1289329Sdam.sunwoo@arm.com if (!get_data("task_struct_tgid", offset)) 1299329Sdam.sunwoo@arm.com return -1; 1309329Sdam.sunwoo@arm.com 13114018Sgabeblack@google.com return tc->getVirtProxy().read<int32_t>(task_struct + offset); 1329329Sdam.sunwoo@arm.com } 1339329Sdam.sunwoo@arm.com 13412090Sprosenfeld@micron.com int32_t 13512090Sprosenfeld@micron.com curTaskTGID(Addr thread_info = 0) 13612090Sprosenfeld@micron.com { 13712090Sprosenfeld@micron.com return curTaskTGIDFromTaskStruct(curTaskInfo(thread_info)); 13812090Sprosenfeld@micron.com } 13912090Sprosenfeld@micron.com 1409329Sdam.sunwoo@arm.com int64_t 14112090Sprosenfeld@micron.com curTaskStartFromTaskStruct(Addr task_struct) 1429329Sdam.sunwoo@arm.com { 14314018Sgabeblack@google.com int32_t offset = 0; 1449329Sdam.sunwoo@arm.com if (!get_data("task_struct_start_time", offset)) 1459329Sdam.sunwoo@arm.com return -1; 1469329Sdam.sunwoo@arm.com 1479329Sdam.sunwoo@arm.com // start_time is actually of type timespec, but if we just 1489329Sdam.sunwoo@arm.com // grab the first long, we'll get the seconds out of it 14914018Sgabeblack@google.com return tc->getVirtProxy().read<int64_t>(task_struct + offset); 1509329Sdam.sunwoo@arm.com } 1519329Sdam.sunwoo@arm.com 15212090Sprosenfeld@micron.com int64_t 15312090Sprosenfeld@micron.com curTaskStart(Addr thread_info = 0) 15412090Sprosenfeld@micron.com { 15512090Sprosenfeld@micron.com return curTaskStartFromTaskStruct(curTaskInfo(thread_info)); 15612090Sprosenfeld@micron.com } 15712090Sprosenfeld@micron.com 1589329Sdam.sunwoo@arm.com std::string 15912090Sprosenfeld@micron.com curTaskNameFromTaskStruct(Addr task_struct) 1609329Sdam.sunwoo@arm.com { 16114018Sgabeblack@google.com int32_t offset = 0; 16214018Sgabeblack@google.com int32_t size = 0; 1639329Sdam.sunwoo@arm.com 1649329Sdam.sunwoo@arm.com if (!get_data("task_struct_comm", offset)) 1659329Sdam.sunwoo@arm.com return "FailureIn_curTaskName"; 1669329Sdam.sunwoo@arm.com 1679329Sdam.sunwoo@arm.com if (!get_data("task_struct_comm_size", size)) 1689329Sdam.sunwoo@arm.com return "FailureIn_curTaskName"; 1699329Sdam.sunwoo@arm.com 1709329Sdam.sunwoo@arm.com char buffer[size + 1]; 17114018Sgabeblack@google.com tc->getVirtProxy().readString(buffer, task_struct + offset, size); 1729329Sdam.sunwoo@arm.com 1739329Sdam.sunwoo@arm.com return buffer; 1749329Sdam.sunwoo@arm.com } 1759329Sdam.sunwoo@arm.com 17612090Sprosenfeld@micron.com std::string 17712090Sprosenfeld@micron.com curTaskName(Addr thread_info = 0) 17812090Sprosenfeld@micron.com { 17912090Sprosenfeld@micron.com return curTaskNameFromTaskStruct(curTaskInfo(thread_info)); 18012090Sprosenfeld@micron.com } 18112090Sprosenfeld@micron.com 1829329Sdam.sunwoo@arm.com int32_t 18312090Sprosenfeld@micron.com curTaskMmFromTaskStruct(Addr task_struct) 1849329Sdam.sunwoo@arm.com { 1859329Sdam.sunwoo@arm.com int32_t offset; 1869329Sdam.sunwoo@arm.com if (!get_data("task_struct_mm", offset)) 1879329Sdam.sunwoo@arm.com return -1; 1889329Sdam.sunwoo@arm.com 18914018Sgabeblack@google.com return tc->getVirtProxy().read<int32_t>(task_struct + offset); 1909329Sdam.sunwoo@arm.com } 19112090Sprosenfeld@micron.com 19212090Sprosenfeld@micron.com int32_t 19312090Sprosenfeld@micron.com curTaskMm(Addr thread_info = 0) 19412090Sprosenfeld@micron.com { 19512090Sprosenfeld@micron.com return curTaskMmFromTaskStruct(curTaskInfo(thread_info)); 19612090Sprosenfeld@micron.com } 1979329Sdam.sunwoo@arm.com}; 1989329Sdam.sunwoo@arm.com 1999329Sdam.sunwoo@arm.com} // namespace Linux 2009329Sdam.sunwoo@arm.com 2019329Sdam.sunwoo@arm.com#endif // __ARCH_GENERIC_LINUX_THREADINFO_HH__ 202