summaryrefslogtreecommitdiffstats
path: root/private/ntos/nbt/vxd/vxddebug.c
blob: 663ccf780d99e6d21494067d385eccfc568dc2ca (plain) (blame)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/**********************************************************************/
/**                       Microsoft Windows NT                       **/
/**                Copyright(c) Microsoft Corp., 1993                **/
/**********************************************************************/

/*
    debug.c

    This module contains debug support routines.


    FILE HISTORY:
        KeithMo     20-Sep-1993 Created.

*/

#include <nbtprocs.h>
#include <vxddebug.h>


#ifdef DEBUG


//
//  Private constants.
//

#define MAX_PRINTF_OUTPUT       1024            // characters
#define MAX_SUBSTRING_LEN        256
#define OUTPUT_LABEL            "VNBT"

#define IS_DIGIT(ch)            (((ch) >= '0') && ((ch) <= '9'))


//
//  Private types.
//


//
//  Private globals.
//


//
//  Private prototypes.
//

int VxdVsprintf( char * pszStr,
                 char * pszFmt,
                 char * ArgPtr );

void VxdCopyToDBOut( void );

//
//  Public functions.
//

/*******************************************************************

    NAME:       VxdAssert

    SYNOPSIS:   Called if an assertion fails.  Displays the failed
                assertion, file name, and line number.  Gives the
                user the opportunity to ignore the assertion or
                break into the debugger.

    ENTRY:      pAssertion - The text of the failed expression.

                pFileName - The containing source file.

                nLineNumber - The guilty line number.

    HISTORY:
        KeithMo     20-Sep-1993 Created.

********************************************************************/
void VxdAssert( void          * pAssertion,
                void          * pFileName,
                unsigned long   nLineNumber )
{
    VxdPrintf( "\n"
               "*** Assertion failed: %s\n"
               "*** Source file %s, line %lu\n\n",
               pAssertion,
               pFileName,
               nLineNumber );

    DEBUG_BREAK;

}   // VxdAssert

/*******************************************************************

    NAME:       VxdPrintf

    SYNOPSIS:   Customized debug output routine.

    ENTRY:      Usual printf-style parameters.

    HISTORY:
        KeithMo     20-Sep-1993 Created.

********************************************************************/
char    szOutput[MAX_PRINTF_OUTPUT];

void VxdPrintf( char * pszFormat,
                ... )
{
    va_list ArgList;
    int     cch;

    cch = VxdSprintf( szOutput,
                      "%s: ",
                      OUTPUT_LABEL );

    va_start( ArgList, pszFormat );
    VxdVsprintf( szOutput + cch, pszFormat, ArgList );
    va_end( ArgList );

    VxdSprintf( szOutput, "%s\r\n", szOutput ) ;

    VxdCopyToDBOut() ;

    NbtDebugOut( DBOut+iCurPos ) ;

}   // VxdPrintf


//
//  Private functions.
//

/*******************************************************************
    NAME:       VxdCopyToDBOut

    SYNOPSIS:   Copies everything from szOutput into DBOut
                First checks to see if DBOut has enough room to hold what we
                have temporarily put in szOutput.  If not, resets the iCurPos
                to point to beginning of DBOut.

    RETURNS:    Nothing

 *******************************************************************/

void VxdCopyToDBOut( void )
{

    int     bytesTowrite;
    int     spaceAvailable;
    int     i;

    spaceAvailable = sizeof(DBOut) - iCurPos;

    bytesTowrite = strlen( szOutput ) + 1;

    // if not enough room, start at the beginning
    if ( spaceAvailable <= bytesTowrite )
    {
        for ( i=iCurPos; i<sizeof(DBOut); i++ )
            DBOut[i] = '+';                       // so that strings don't mix

        iCurPos = 0;

        if ( bytesTowrite > sizeof(szOutput) )
        {
            bytesTowrite = sizeof(szOutput);
            szOutput[bytesTowrite-1] = '\0';
        }
    }

    CTEMemCopy( DBOut+iCurPos, szOutput, bytesTowrite ) ;

}

/*******************************************************************

    NAME:       VxdVsprintf

    SYNOPSIS:   Half-baked vsprintf() clone for VxD environment.

    ENTRY:      pszStr - Will receive the formatted string.

                pszFmt - The format, with field specifiers.

                ArgPtr - Points to the actual printf() arguments.

    RETURNS:    int - Number of characters stored in *pszStr.

    HISTORY:
        KeithMo     20-Sep-1993 Created.

********************************************************************/
int VxdVsprintf( char * pszStr,
                 char * pszFmt,
                 char * ArgPtr )

{
    char   ch;
    char * pszStrStart;
    int    fZeroPad;
    int    cchWidth;
    int    ccMaxToCopy;

    //
    //  Remember start of output, so we can calc length.
    //

    pszStrStart = pszStr;

    while( ( ch = *pszFmt++ ) != '\0' )
    {
        //
        //  Scan for format specifiers.
        //

        if( ch != '%' )
        {
            *pszStr++ = ch;
            continue;
        }

        //
        //  Got one.
        //

        ch = *pszFmt++;

        //
        //  Initialize attributes for this item.
        //

        fZeroPad = 0;
        cchWidth = 0;
        ccMaxToCopy = MAX_SUBSTRING_LEN;

        //
        //  Interpret the field specifiers.
        //

        if( ch == '-' )
        {
            //
            //  Left justification not supported.
            //

            ch = *pszFmt++;
        }

        if( ch == '0' )
        {
            //
            //  Zero padding.
            //

            fZeroPad = 1;
            ch       = *pszFmt++;
        }

        if( ch == '*' )
        {
            //
            //  Retrieve width from next argument.
            //

            cchWidth = va_arg( ArgPtr, int );
            ch       = *pszFmt++;
        }
        else
        {
            //
            //  Calculate width.
            //

            while( IS_DIGIT(ch) )
            {
                cchWidth = ( cchWidth * 10 ) + ( ch - '0' );
                ch       = *pszFmt++;
            }
        }

        if( ch == '.' )
        {
            ch = *pszFmt++;

            if( ch == '*' )
            {
                ccMaxToCopy = va_arg( ArgPtr, int );
                ch = *pszFmt++;
            }
            else
            {
                ccMaxToCopy = 0;
                while( IS_DIGIT(ch) )
                {
                    ccMaxToCopy =  ( ccMaxToCopy * 10 ) + ( ch - '0' );
                    ch = *pszFmt++;
                }
            }
        }

        //
        //  All numbers are longs.
        //

        if( ch == 'l' )
        {
            ch = *pszFmt++;
        }

        //
        //  Decipher the format specifier.
        //

        if( ( ch == 'd' ) || ( ch == 'u' ) || ( ch == 'x' ) || ( ch == 'X' ) )
        {
            unsigned long   ul;
            unsigned long   radix;
            char            xbase;
            char          * pszTmp;
            char          * pszEnd;
            int             cchNum;
            int             fNegative;

            //
            //  Numeric.  Retrieve the value.
            //

            ul = va_arg( ArgPtr, unsigned long );

            //
            //  If this is a negative number, remember and negate.
            //

            if( ( ch == 'd' ) && ( (long)ul < 0 ) )
            {
                fNegative = 1;
                ul        = (unsigned long)(-(long)ul);
            }
            else
            {
                fNegative = 0;
            }

            //
            //  Remember start of digits.
            //

            pszTmp = pszStr;
            cchNum = 0;

            //
            //  Special goodies for hex conversion.
            //

            radix  = ( ( ch == 'x' ) || ( ch == 'X' ) ) ? 16 : 10;
            xbase  = ( ch == 'x' ) ? 'a' : 'A';

            //
            //  Loop until we're out of digits.
            //

            do
            {
                unsigned int digit;

                digit  = (unsigned int)( ul % radix );
                ul    /= radix;

                if( digit > 9 )
                {
                    *pszTmp++ = (char)( digit - 10 + xbase );
                }
                else
                {
                    *pszTmp++ = (char)( digit + '0' );
                }

                cchNum++;

            } while( ul > 0 );

            //
            //  Add the negative sign if necessary.
            //

            if( fNegative )
            {
                *pszTmp++ = '-';
                cchNum++;
            }

            //
            //  Add any necessary padding.
            //

            while( cchNum < cchWidth )
            {
                *pszTmp++ = fZeroPad ? '0' : ' ';
                cchNum++;
            }

            //
            //  Now reverse the digits.
            //

            pszEnd = pszTmp--;

            do
            {
                char tmp;

                tmp     = *pszTmp;
                *pszTmp = *pszStr;
                *pszStr = tmp;

                pszTmp--;
                pszStr++;

            } while( pszTmp > pszStr );

            pszStr = pszEnd;
        }
        else
        if( ch == 's' )
        {
            char * pszTmp;
            int    count;

            //
            //  Copy the string.
            //

            pszTmp = va_arg( ArgPtr, char * );

            count = 0;
            while( *pszTmp )
            {
                *pszStr++ = *pszTmp++;
                count++;
                //
                // if we get passed a weird pointer, don't go on writing!  That
                // overwrites other things (like tdidispatch table!) and very
                // bad things happen....
                //
                if (count >= ccMaxToCopy)
                   break;
            }
        }
        else
        if( ch == 'c' )
        {
            //
            //  A single character.
            //

            *pszStr++ = (char)va_arg( ArgPtr, int );
        }
        else
        {
            //
            //  Unknown.  Ideally we should copy the entire
            //  format specifier, including any width & precision
            //  values, but who really cares?
            //

            *pszStr++ = ch;
        }
    }

    //
    //  Terminate it properly.
    //

    *pszStr = '\0';

    //
    //  Return the length of the generated string.
    //

    return pszStr - pszStrStart;

}   // VxdVsprintf

/*******************************************************************

    NAME:       VxdSprintf

    SYNOPSIS:   Half-baked sprintf() clone for VxD environment.

    ENTRY:      pszStr - Will receive the formatted string.

                pszFmt - The format, with field specifiers.

                ... - Usual printf()-like parameters.

    RETURNS:    int - Number of characters stored in *pszStr.

    HISTORY:
        KeithMo     20-Sep-1993 Created.

********************************************************************/
int VxdSprintf( char * pszStr,
                char * pszFmt,
                ... )
{
    int     cch;
    va_list ArgPtr;

    va_start( ArgPtr, pszFmt );
    cch = VxdVsprintf( pszStr, pszFmt, ArgPtr );
    va_end( ArgPtr );

    return( cch );

}   // VxdSprintf


/*******************************************************************

    NAME:       DbgAllocMem

    SYNOPSIS:   Keep track of all allocated memory so we can catch
                memory leak when we unload
                This is only on debug builds.  On non-debug builds
                this function doesn't exist: calls directly go to
                CTEAllocMem.

    ENTRY:      ReqSize - how much memory is needed

    RETURNS:    PVOID - pointer to the memory block that client will
                use directly.

    HISTORY:
        Koti     11-Nov-1994 Created.

********************************************************************/

//
// IMPORTANT: we are undef'ing CTEAllocMem because we need to make a
//            call to the actual CTE function CTEAllocMem.  That's why
//            this function and this undef are at the end of the file.
//
#undef CTEAllocMem
PVOID DbgAllocMem( DWORD ReqSize )
{

    DWORD          ActualSize;
    PVOID          pBuffer;
    DbgMemBlkHdr  *pMemHdr;
    PVOID          pRetAddr;


    ActualSize = ReqSize + sizeof(DbgMemBlkHdr);
    pBuffer = CTEAllocMem( ActualSize );
    if ( !pBuffer )
    {
        return( NULL );
    }

    pMemHdr = (DbgMemBlkHdr *)pBuffer;

    pMemHdr->Verify = DBG_MEMALLOC_VERIFY;
    pMemHdr->ReqSize = ReqSize;
    pRetAddr = &pMemHdr->Owner[0];

//
// now memory is allocated from NCBHandler, too where stack trace isn't more
// than 2 deep!  unifdef when memory leaks becomes an issue...
//
#if 0
    _asm
    {
        push   ebx
        push   ecx
        push   edx
        mov    ebx, pRetAddr
        mov    eax, ebp
        mov    ecx, 4
    again:
        mov    edx, dword ptr [eax+4]           ; return address
        mov    dword ptr [ebx], edx
        mov    eax, dword ptr [eax]             ; previous frame pointer
        add    ebx, 4
        dec    ecx
        cmp    ecx, 0
        je     done
        jmp    again
    done:
        pop    edx
        pop    ecx
        pop    ebx
    }
#endif

    //
    // BUGBUG: if ever ported to NT (or if chicago needs MP support),
    // put spinlocks.  (we will need a spinlock field in DbgMemBlkHdr struct).
    //
    InsertTailList(&DbgMemList, &pMemHdr->Linkage);

    return( (PCHAR)pBuffer + sizeof(DbgMemBlkHdr) );
}

/*******************************************************************

    NAME:       DbgFreeMem

    SYNOPSIS:   This routine removes the memory block from our list and
                frees the memory by calling the CTE function CTEFreeMem

    ENTRY:      pBufferToFree - memory to free (caller's buffer)

    RETURNS:    nothing

    HISTORY:
        Koti     11-Nov-1994 Created.

********************************************************************/

//
// IMPORTANT: we are undef'ing CTEFreeMem because we need to make a
//            call to the actual CTE function CTEFreeMem.  That's why
//            this function and this undef are at the end of the file.
//
#undef CTEMemFree
#undef CTEFreeMem

VOID DbgFreeMem( PVOID  pBufferToFree )
{

    DbgMemBlkHdr  *pMemHdr;


    if ( !pBufferToFree )
    {
        return;
    }

    pMemHdr = (DbgMemBlkHdr *)((PCHAR)pBufferToFree - sizeof(DbgMemBlkHdr));

    ASSERT( pMemHdr->Verify == DBG_MEMALLOC_VERIFY );

    //
    // change our signature: if we are freeing some memory twice, we'll know!
    //
    pMemHdr->Verify -= 1;

    //
    // BUGBUG: if ever ported to NT (or if chicago needs MP support),
    // put spinlocks.  (we will need a spinlock field in DbgMemBlkHdr struct).
    //
    RemoveEntryList(&pMemHdr->Linkage);

    CTEFreeMem( (PVOID)pMemHdr );
}

#endif  // DEBUG