1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*++

Copyright (c) 1998-2001 Klaus P. Gerlicher

Module Name:

    output.c

Abstract:

    catch debugging outputs

Environment:

    Kernel mode only

Author:

    Klaus P. Gerlicher

Revision History:

    14-Nov-1999:	created
    15-Nov-2000:    general cleanup of source files

Copyright notice:

  This file may be distributed under the terms of the GNU Public License.

--*/

////////////////////////////////////////////////////
// INCLUDES
////
#include "remods.h"
#include "precomp.h"
/*
#include <linux/sched.h>
#include <asm/io.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <linux/utsname.h>
#include <linux/sched.h>
#include <linux/console.h>
#include <asm/delay.h>
*/

char tempOutput[1024],tempOutput2[1024];

//ULONG ulPrintk=0;

ULONG (*ulPrintk) (PANSI_STRING String);

BOOLEAN bInPrintk = FALSE;
BOOLEAN bIsDebugPrint = FALSE;
BOOLEAN bIsPrintkPatched = FALSE;

ULONG ulCountTimerEvents = 0;

#ifdef __cplusplus
#define CPP_ASMLINKAGE extern "C"
#else
#define CPP_ASMLINKAGE
#endif
#define asmlinkage CPP_ASMLINKAGE __attribute__((regparm(0)))

asmlinkage int printk(const char *fmt, ...);

//EXPORT_SYMBOL(printk);

//*************************************************************************
// printk()
//
// this function overrides printk() in the kernel
//*************************************************************************
asmlinkage int printk(const char *fmt, ...)
{
	ULONG len,ulRingBufferLock;
    static LONGLONG ulOldJiffies = 0;
	LARGE_INTEGER jiffies;

	va_list args;
	va_start(args, fmt);

	if((len = PICE_strlen((LPSTR)fmt)) )
	{
	    save_flags(ulRingBufferLock);
	    cli();

		PICE_vsprintf(tempOutput, fmt, args);
		bIsDebugPrint = TRUE;
        // if the last debug print was longer than 50 ms ago
        // directly print it, else just add it to the ring buffer
        // and let the timer process it.
		KeQuerySystemTime(&jiffies);
        if( (jiffies.QuadPart-ulOldJiffies) > 10000*(1*wWindow[OUTPUT_WINDOW].cy)/2)
        {
            ulOldJiffies = jiffies.QuadPart;
		    Print(OUTPUT_WINDOW,tempOutput);
        }
        else
        {
		    AddToRingBuffer(tempOutput);
        }

		bIsDebugPrint = FALSE;
        restore_flags(ulRingBufferLock);
	}
	va_end(args);

    return 0;
}

//*************************************************************************
// CountArgs()
//
// count occurrence of '%' in format string (except %%)
// validity of whole format string must have been enforced
//*************************************************************************
ULONG CountArgs(LPSTR fmt)
{
	ULONG count=0;

	while(*fmt)
	{
		if(*fmt=='%' && *(fmt+1)!='%')
			count++;
		fmt++;
	}
	return count;
}

//***********************************************************************************
//	Our replacement of kernel function.
//	Must not make any calls to KdpPrintString (e.g. by calling DbgPrint).
//***********************************************************************************
ULONG PICE_KdpPrintString(PANSI_STRING String)
{
	ULONG ulRingBufferLock;

  save_flags(ulRingBufferLock);
  cli();

  /* CH: What is bIsDebugPrint used for? */
  bIsDebugPrint = FALSE;

	DPRINT((0,"PICE_KdpPrintString\n\n\n"));
  AddToRingBuffer(String->Buffer);
  restore_flags(ulRingBufferLock);
}
//*************************************************************************
// PrintkCallback()
//
// called from RealIsr() when processing INT3 placed
// Must not make any calls to KdpPrintString (e.g. by calling DbgPrint).
//*************************************************************************
void PrintkCallback(void)
{
	LPSTR fmt,args;<--- Unused variable: fmt<--- Unused variable: args
	ULONG ulAddress;
	ULONG countArgs,i,len;<--- Unused variable: countArgs<--- Unused variable: i<--- Unused variable: len
	PANSI_STRING temp;
  CHAR buf[128];

	DPRINT((0,"In PrintkCallback\n"));

	bInPrintk = TRUE;

	// get the linear address of stack where string resides
	ulAddress = GetLinearAddress(CurrentSS,CurrentESP);
	if(ulAddress)
	{
		DPRINT((0,"In PrintkCallback: ulAddress: %x\n", ulAddress));
		if(IsAddressValid(ulAddress+sizeof(char *)) )
		{
			//KdpPrintString has PANSI_STRING as a parameter
			temp = (PANSI_STRING)*(PULONG)(ulAddress+sizeof(char *));
    	DPRINT((0,"PrintkCallback: %s\n", temp->Buffer));
      /* Call our version of KdpPrintString() */
			CurrentEIP = (ULONG_PTR)PICE_KdpPrintString;
		}
	}
	bInPrintk = FALSE;
}

//*************************************************************************
// PiceRunningTimer()
//
//*************************************************************************

KTIMER PiceTimer;
KDPC PiceTimerDPC;

// do I need it here? Have to keep DPC memory resident #pragma code_seg()
VOID PiceRunningTimer(IN PKDPC Dpc,
                       IN PVOID DeferredContext,
                       IN PVOID SystemArgument1,
                       IN PVOID SystemArgument2)
{
   	CheckRingBuffer();

    if(ulCountTimerEvents++ > 10)
    {
		LARGE_INTEGER jiffies;

		ulCountTimerEvents = 0;

		KeQuerySystemTime(&jiffies);
        SetForegroundColor(COLOR_TEXT);
	    SetBackgroundColor(COLOR_CAPTION);
        PICE_sprintf(tempOutput,"jiffies = %.8X\n",jiffies.u.LowPart);
	    PutChar(tempOutput,GLOBAL_SCREEN_WIDTH-strlen(tempOutput),GLOBAL_SCREEN_HEIGHT-1);
        ResetColor();
    }
}

//*************************************************************************
// InitPiceRunningTimer()
//
//*************************************************************************
void InitPiceRunningTimer(void)
{
	LARGE_INTEGER   Interval;<--- Unused variable: Interval

	ENTER_FUNC();
#if 0  //won't work. we have to intercept timer interrupt so dpc will never fire while we are in pice
	KeInitializeTimer( &PiceTimer );
	KeInitializeDpc( &PiceTimerDPC, PiceRunningTimer, NULL );

	Interval.QuadPart=-1000000L;  // 100 millisec. (unit is 100 nanosec.)

    KeSetTimerEx(&PiceTimer,
                        Interval, 1000000L,
                        &PiceTimerDpc);
#endif
    LEAVE_FUNC();
}

//*************************************************************************
// RemovePiceRunningTimer()
//
//*************************************************************************
void RemovePiceRunningTimer(void)
{
	KeCancelTimer( &PiceTimer );
}

//*************************************************************************
// InstallPrintkHook()
//
//*************************************************************************
void InstallPrintkHook(void)
{

	ENTER_FUNC();

	if( bIsPrintkPatched )
			return;

  DPRINT((0,"installing PrintString hook\n"));
	ScanExports("_KdpPrintString",(PULONG)&ulPrintk);

	DPRINT((0,"_KdpPrintString @ %x\n", ulPrintk));
	ASSERT( ulPrintk );                 // temporary
    if(ulPrintk)
    {
      bIsPrintkPatched = InstallSWBreakpoint(ulPrintk,TRUE,PrintkCallback);
  		DPRINT((0,"KdpPrintStringTest breakpoint installed? %d\n", bIsPrintkPatched));
    }

	LEAVE_FUNC();
}

//*************************************************************************
// DeInstallPrintkHook()
//
//*************************************************************************
void DeInstallPrintkHook(void)
{
    ENTER_FUNC();

	DPRINT((0,"enter DeInstallPrintkHook()\n"));
    if(bIsPrintkPatched && ulPrintk)
    {
		// will be done on exit debugger
        if (DeInstallSWBreakpoint(ulPrintk))
				  bIsPrintkPatched = FALSE;
    }
    LEAVE_FUNC();
}