?? shell.cpp
字號:
PrintAllKt(&g_pKThreadQueue[0]);
goto __TERMINAL;
}
pCmdObj = FormParameterObj(pszPara);
if(NULL == pCmdObj)
{
PrintLine("Can not allocate the resource to interpret the command.");
goto __TERMINAL;
}
switch(pCmdObj->byFunctionLabel)
{
case 'i':
case 0:
bResult = Str2Hex(pCmdObj->Parameter[0],&dwID);
if(FALSE == bResult)
{
PrintLine("Can not interpret the command's parameter.");
goto __TERMINAL;
}
PrintKtByID(dwID);
break;
case '?':
default:
KtViewUsage();
break;
}
__TERMINAL:
if(NULL != pCmdObj)
KMemFree((LPVOID)pCmdObj,KMEM_SIZE_TYPE_4K,4096);
return;
}
//
//The helper functions,print out the usage information.
//
static VOID SendMsgUsage()
{
PrintLine(" Usage :");
PrintLine(" sendmsg kthread_id command [parameter1] [parameter2]");
PrintLine(" Where :");
PrintLine(" kthread_id : Kernal thread ID.");
PrintLine(" command : Command number.");
PrintLine(" parameter1 : The first parameter(optional).");
PrintLine(" parameter2 : The second parameter(optional).");
}
VOID SendMsgHandler(LPSTR pszPara)
{
__CMD_PARA_OBJ* pCmdObj = NULL;
DWORD dwID = 0L;
__KTHREAD_MSG msg;
BOOL bResult = FALSE;
DWORD dwCommand = 0L;
__KTHREAD_CONTROL_BLOCK* pControlBlock = NULL;
if((NULL == pszPara) || (0 == *pszPara))
{
SendMsgUsage();
goto __TERMINAL;
}
pCmdObj = FormParameterObj(pszPara);
if(NULL == pCmdObj)
{
PrintLine("Can not allocate resource to interpret the command.");
goto __TERMINAL;
}
if(pCmdObj->byParameterNum < 2)
{
PrintLine("Miss command code.");
goto __TERMINAL;
}
bResult = Str2Hex(pCmdObj->Parameter[0],&dwID);
if((FALSE == bResult)
|| (dwID < 1)
|| (dwID > MAX_KTHREAD_NUM))
{
PrintLine("Invalid kernal thread ID.");
goto __TERMINAL;
}
pControlBlock = GetKThreadControlBlock(dwID);
if(NULL == pControlBlock)
{
PrintLine("The kernal thread is not exist.");
goto __TERMINAL;
}
bResult = Str2Hex(pCmdObj->Parameter[1],&dwCommand);
if(FALSE == bResult)
{
PrintLine("Invalid command code.");
goto __TERMINAL;
}
switch(pCmdObj->byParameterNum)
{
case 2: //No parameter.
msg.wCommand = LOWORD(dwCommand);
msg.dwParam_01 = 0L;
msg.dwParam_02 = 0L;
KtSendMessage(pControlBlock,&msg);
break;
case 3: //With one parameter.
msg.wCommand = LOWORD(dwCommand);
bResult = Str2Hex(pCmdObj->Parameter[2],&msg.dwParam_01);
if(FALSE == bResult)
{
PrintLine("Invalid parameter.");
break;
}
msg.dwParam_02 = 0L;
KtSendMessage(pControlBlock,&msg);
break;
case 4: //With two parameter.
default:
msg.wCommand = LOWORD(dwCommand);
bResult = Str2Hex(pCmdObj->Parameter[2],&msg.dwParam_01);
if(FALSE == bResult)
{
PrintLine("Invalid parameter.");
break;
}
bResult = Str2Hex(pCmdObj->Parameter[3],&msg.dwParam_02);
if(FALSE == bResult)
{
PrintLine("Invalid parameter.");
break;
}
KtSendMessage(pControlBlock,&msg);
break;
}
__TERMINAL:
if(NULL != pCmdObj)
KMemFree((LPVOID)pCmdObj,KMEM_SIZE_TYPE_4K,4096);
return;
}
//
//Memview handler.
//
VOID MemViewHandler(LPSTR pszCmd)
{
__CMD_PARA_OBJ* pParaObj = NULL;
DWORD dwIndex = 0x00000000;
DWORD dwStartAddress = 0x00000000; //Local variables defination.
DWORD bResult = FALSE;
DWORD dwNumber = 0x00000000;
DWORD i = 0x00000000;
BYTE memBuffer[12];
if(0 == *pszCmd)
{
__PRINT_USAGE:
PrintLine(" Usage : View a block memory's content.");
PrintLine(" memview -k[u] [process_id] start_mem_addr number");
PrintLine(" Where :");
PrintLine(" -k : View the kernal mode memory content.");
PrintLine(" -u : View the user mode memory content.");
PrintLine(" process_id : The process to be viewed.");
PrintLine(" start_mem_addr : The start address of the memory be viewed.");
PrintLine(" number : How many double word's memory to be viewed.");
goto __TERMINAL;
}
pParaObj = FormParameterObj(pszCmd); //Form the command parameter object by
//the parameter pszCmd.
switch(pParaObj->byFunctionLabel)
{
case 0: //If no function label,default to kernal memory
//view.
//break;
case 'k':
bResult = Str2Hex(pParaObj->Parameter[0],&dwStartAddress); //Convert tht first parameter
//from string to hex number.
if(FALSE == bResult)
{
PrintLine("Can not convert the first parameter to hex number.");
break;
}
bResult = Str2Hex(pParaObj->Parameter[1],&dwNumber); //Convert the second parameter
//from string to hex number.
if(FALSE == bResult)
{
PrintLine("Can not convert the second parameter to hex number.");
break;
}
if(dwStartAddress + dwNumber * 4 > 16 * 1024 * 1024)
{
PrintLine("The memory block you want to view is exceed the kernal space.");
break;
}
PrintLine(" ---------- ** Mem Content ** ---------- ");
ChangeLine();
GotoHome();
for(i = 0;i < dwNumber;i ++)
{
PrintStr(" 0x");
Hex2Str(dwStartAddress,memBuffer);
PrintStr(memBuffer);
PrintStr(" 0x");
Hex2Str(*(DWORD*)dwStartAddress,memBuffer);
PrintStr(memBuffer);
ChangeLine();
GotoHome();
dwStartAddress += 4;
}
break;
case 'u':
PrintLine("Does not support now.");
break;
default:
goto __PRINT_USAGE;
break;
}
/*while(pParaObj)
{
for(dwIndex = 0;dwIndex < (DWORD)pParaObj->byParameterNum;dwIndex ++)
{
ConvertToUper(pParaObj->Parameter[dwIndex]);
PrintLine(pParaObj->Parameter[dwIndex]);
}
pParaObj = pParaObj->pNext;
}*/
__TERMINAL:
if(NULL != pParaObj)
KMemFree((LPVOID)pParaObj,KMEM_SIZE_TYPE_4K,4096);
return;
}
//LPSTR g_pszTest = "Hello,China!!";
#define KMSG_USER_SUM 0x00FF
#define KMSG_USER_ACC 0x00FE
static __KTHREAD_CONTROL_BLOCK* g_pThread1ControlBlock = NULL;
VOID ShellThread1(LPVOID)
{
static DWORD dwSum = 0L;
__KTHREAD_CONTROL_BLOCK* pControlBlock = g_pThread1ControlBlock;
__KTHREAD_MSG msg;
DWORD dwTmp1 = 0L;
DWORD dwTmp2 = 0L;
BYTE Buffer[32];
while(TRUE)
{
if(KtGetMessage(pControlBlock,&msg))
{
switch(msg.wCommand)
{
case KMSG_USER_SUM:
dwTmp1 = msg.dwParam_01;
dwTmp2 = msg.dwParam_02;
dwSum = dwTmp1 + dwTmp2;
Int2Str(dwSum,Buffer);
PrintLine("The sum is : ");
PrintStr(Buffer);
break;
case KMSG_USER_ACC:
dwTmp1 = msg.dwParam_01;
dwSum = 0L;
for(dwTmp2 = 0;dwTmp2 < dwTmp1;dwTmp2 ++)
{
dwSum += dwTmp2 + 1;
}
PrintLine("The accumulated result is : ");
Int2Str(dwSum,Buffer);
PrintStr(Buffer);
break;
default:
break;
}
}
}
}
VOID ShellThread2(LPVOID)
{
static DWORD dwThread2Counter = 0x0000;
while(TRUE)
{
dwThread2Counter ++;
if(0 == dwThread2Counter % 0x00FFFFFF)
{
PrintLine("Kernal thread is running : kernal-thread-2.");
dwThread2Counter = 0;
}
}
}
DWORD ShellThread3(LPVOID)
{
DWORD dwCounter = 1L;
while(dwCounter < 10)
{
dwCounter ++;
PrintLine("Thread 3 is running,increment the dwCounter.");
}
return 0L;
}
__EVENT* lpWriteEvent = NULL;
__EVENT* lpReadEvent = NULL;
DWORD ReadThread(LPVOID)
{
while(TRUE)
{
lpReadEvent->WaitForThisObject((__COMMON_OBJECT*)lpReadEvent);
PrintLine("Read resource from shared pool.");
lpReadEvent->ResetEvent((__COMMON_OBJECT*)lpReadEvent);
lpWriteEvent->SetEvent((__COMMON_OBJECT*)lpWriteEvent);
}
return 0L;
}
DWORD WriteThread(LPVOID)
{
while(TRUE)
{
lpWriteEvent->WaitForThisObject((__COMMON_OBJECT*)lpWriteEvent);
PrintLine("Write resource to shared pool.");
lpWriteEvent->ResetEvent((__COMMON_OBJECT*)lpWriteEvent);
lpReadEvent->SetEvent((__COMMON_OBJECT*)lpReadEvent);
}
return 0L;
}
DWORD SleepThread(LPVOID)
{
DWORD dwSleepCount = 10L;
while(dwSleepCount)
{
PrintLine("I am sleeping thread.");
KernelThreadManager.Sleep((__COMMON_OBJECT*)&KernelThreadManager,500);
dwSleepCount --;
}
return 0L;
}
DWORD EchoRoutine(LPVOID)
{
__KERNEL_THREAD_MESSAGE Msg;
BYTE bt;
WORD wr = 0x0700;
while(TRUE)
{
if(GetMessage(&Msg))
{
if(Msg.wCommand == MSG_KEY_DOWN)
{
bt = LOBYTE(LOWORD(Msg.dwParam));
if(('q' == bt) || ('x' == bt))
goto __TERMINAL;
wr += bt;
PrintCh(wr);
wr = 0x0700;
}
}
}
__TERMINAL:
return 0L;
}
__MUTEX* lpMutex = NULL;
__MAILBOX* lpMailBox = NULL;
__EVENT* lpEvent = NULL;
static DWORD SynThread1(LPVOID)
{
__KERNEL_THREAD_MESSAGE Msg;
BYTE bt;
while(TRUE)
{
if(GetMessage(&Msg))
{
if(Msg.wCommand == MSG_KEY_DOWN)
{
PrintLine("Set event object.");
lpEvent->SetEvent((__COMMON_OBJECT*)lpEvent);
bt = LOBYTE(LOWORD(Msg.dwParam));
if('q' == bt)
return 0L;
}
}
}
return 0L;
}
static DWORD SynThread2(LPVOID)
{
DWORD dwRetVal = 0L;
while(TRUE)
{
dwRetVal = lpEvent->WaitForThisObjectEx((__COMMON_OBJECT*)lpEvent,
300);
switch(dwRetVal)
{
case OBJECT_WAIT_RESOURCE:
PrintLine("I am first thread,wait a resource.");
lpEvent->ResetEvent((__COMMON_OBJECT*)lpEvent);
break;
case OBJECT_WAIT_TIMEOUT:
PrintLine("I am first thread,wait time out...");
break;
}
}
return 0L;
}
static DWORD SynThread3(LPVOID)
{
DWORD dwRetVal = 0L;
while(TRUE)
{
dwRetVal = lpEvent->WaitForThisObjectEx((__COMMON_OBJECT*)lpEvent,
600);
switch(dwRetVal)
{
case OBJECT_WAIT_RESOURCE:
PrintLine("I am second thread,wait a resource.");
lpEvent->ResetEvent((__COMMON_OBJECT*)lpEvent);
break;
case OBJECT_WAIT_TIMEOUT:
PrintLine("I am second thread,wait time out...");
break;
}
}
return 0L;
}
VOID TestHandler(LPSTR)
{
LPVOID lpAddr[16];
BYTE Buffer[12];
int i = 0;
PrintLine("Begin testing...");
for(int j = 0;j < 1000;j ++)
{
for(i = 0;i < 16;i ++)
{
lpAddr[i] = malloc(i * 8192);
//Hex2Str((DWORD)lpAddr[i],Buffer);
//PrintLine(Buffer);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -