xref: /reactos/drivers/filesystems/vfatfs/kdbg.c (revision 09dde2cf)
1 /*
2  * PROJECT:     VFAT Filesystem
3  * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4  * PURPOSE:     KDBG extension
5  * COPYRIGHT:   Copyright 2018 Pierre Schweitzer <pierre@reactos.org>
6  */
7 
8 /*  -------------------------------------------------------  INCLUDES  */
9 
10 #include "vfat.h"
11 
12 #define NDEBUG
13 #include <debug.h>
14 
15 #include <stdio.h>
16 
17 /*  --------------------------------------------------------  DEFINES  */
18 
19 #ifdef KDBG
20 UNICODE_STRING DebugFile = {0, 0, NULL};
21 
22 BOOLEAN
23 NTAPI
24 vfatKdbgHandler(
25     IN PCHAR Command,
26     IN ULONG Argc,
27     IN PCH Argv[])
28 {
29     ULONG Len;
30 
31     Len = strlen(Command);
32     if (Len < sizeof("?fat."))
33     {
34         return FALSE;
35     }
36 
37     if (Command[0] != '?' || Command[1] != 'f' ||
38         Command[2] != 'a' || Command[3] != 't' ||
39         Command[4] != '.')
40     {
41         return FALSE;
42     }
43 
44     Command += (sizeof("?fat.") - sizeof(ANSI_NULL));
45     if (strcmp(Command, "vols") == 0)
46     {
47         ULONG Count = 0;
48         PLIST_ENTRY ListEntry;
49         PDEVICE_EXTENSION DeviceExt;
50 
51         for (ListEntry = VfatGlobalData->VolumeListHead.Flink;
52              ListEntry != &VfatGlobalData->VolumeListHead;
53              ListEntry = ListEntry->Flink)
54         {
55             DeviceExt = CONTAINING_RECORD(ListEntry, DEVICE_EXTENSION, VolumeListEntry);
56             DPRINT1("Volume: %p with VCB: %p\n", DeviceExt->VolumeDevice, DeviceExt);
57             ++Count;
58         }
59 
60         if (Count == 0)
61         {
62             DPRINT1("No volume found\n");
63         }
64     }
65     else if (strcmp(Command, "files") == 0)
66     {
67         if (Argc != 2)
68         {
69             DPRINT1("Please provide a volume or a VCB!\n");
70         }
71         else
72         {
73             PLIST_ENTRY ListEntry;
74             PDEVICE_EXTENSION DeviceExt;
75 
76             for (ListEntry = VfatGlobalData->VolumeListHead.Flink;
77                  ListEntry != &VfatGlobalData->VolumeListHead;
78                  ListEntry = ListEntry->Flink)
79             {
80                 CHAR Volume[17];
81 
82                 DeviceExt = CONTAINING_RECORD(ListEntry, DEVICE_EXTENSION, VolumeListEntry);
83                 sprintf(Volume, "%p", DeviceExt);
84                 if (strcmp(Volume, Argv[1]) == 0)
85                 {
86                     break;
87                 }
88 
89                 sprintf(Volume, "%p", DeviceExt->VolumeDevice);
90                 if (strcmp(Volume, Argv[1]) == 0)
91                 {
92                     break;
93                 }
94 
95                 DeviceExt = NULL;
96             }
97 
98             if (DeviceExt == NULL)
99             {
100                 DPRINT1("No volume %s found!\n", Argv[1]);
101             }
102             else
103             {
104                 PVFATFCB Fcb;
105 
106                 for (ListEntry = DeviceExt->FcbListHead.Flink;
107                      ListEntry != &DeviceExt->FcbListHead;
108                      ListEntry = ListEntry->Flink)
109                 {
110                     Fcb = CONTAINING_RECORD(ListEntry, VFATFCB, FcbListEntry);
111                     DPRINT1("FCB %p (ref: %d, oc: %d %s %s %s) for FO %p with path: %.*S\n",
112                             Fcb, Fcb->RefCount, Fcb->OpenHandleCount,
113                             ((Fcb->Flags & FCB_CLEANED_UP) ? "U" : "NU"),
114                             ((Fcb->Flags & FCB_CLOSED) ? "C" : "NC"),
115                             ((Fcb->Flags & FCB_DELAYED_CLOSE) ? "D" : "ND"),
116                             Fcb->FileObject, Fcb->PathNameU.Length, Fcb->PathNameU.Buffer);
117                 }
118             }
119         }
120     }
121     else if (strcmp(Command, "setdbgfile") == 0)
122     {
123         if (Argc < 2)
124         {
125             if (DebugFile.Buffer != NULL)
126             {
127                 ExFreePool(DebugFile.Buffer);
128                 DebugFile.Length = 0;
129                 DebugFile.MaximumLength = 0;
130             }
131 
132             DPRINT1("Debug file reset\n");
133         }
134         else
135         {
136             NTSTATUS Status;
137             ANSI_STRING Source;
138 
139             if (DebugFile.Buffer != NULL)
140             {
141                 ExFreePool(DebugFile.Buffer);
142                 DebugFile.Length = 0;
143                 DebugFile.MaximumLength = 0;
144             }
145 
146             RtlInitAnsiString(&Source, Argv[1]);
147             Status = RtlAnsiStringToUnicodeString(&DebugFile, &Source, TRUE);
148             if (NT_SUCCESS(Status))
149             {
150                 DPRINT1("Debug file set to: %.*S\n", DebugFile.Length, DebugFile.Buffer);
151             }
152         }
153     }
154     else
155     {
156         DPRINT1("Unknown command: %s\n", Command);
157     }
158 
159     return TRUE;
160 }
161 #endif
162