| 1 |
//----------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (c) Microsoft. All rights reserved. |
| 4 |
// This code is licensed under the Microsoft Public License. |
| 5 |
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF |
| 6 |
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY |
| 7 |
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR |
| 8 |
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. |
| 9 |
// |
| 10 |
//----------------------------------------------------------------------------- |
| 11 |
using System; |
| 12 |
using System.Collections; |
| 13 |
using System.Collections.Generic; |
| 14 |
|
| 15 |
namespace Microsoft.Cci.Pdb { |
| 16 |
internal class PdbFunction { |
| 17 |
static internal readonly Guid msilMetaData = new Guid(0xc6ea3fc9, 0x59b3, 0x49d6, 0xbc, 0x25, |
| 18 |
0x09, 0x02, 0xbb, 0xab, 0xb4, 0x60); |
| 19 |
static internal readonly IComparer byAddress = new PdbFunctionsByAddress(); |
| 20 |
static internal readonly IComparer byAddressAndToken = new PdbFunctionsByAddressAndToken(); |
| 21 |
//static internal readonly IComparer byToken = new PdbFunctionsByToken(); |
| 22 |
|
| 23 |
internal uint token; |
| 24 |
internal uint slotToken; |
| 25 |
internal uint tokenOfMethodWhoseUsingInfoAppliesToThisMethod; |
| 26 |
//internal string name; |
| 27 |
//internal string module; |
| 28 |
//internal ushort flags; |
| 29 |
|
| 30 |
internal uint segment; |
| 31 |
internal uint address; |
| 32 |
//internal uint length; |
| 33 |
|
| 34 |
//internal byte[] metadata; |
| 35 |
internal PdbScope[] scopes; |
| 36 |
internal PdbSlot[] slots; |
| 37 |
internal PdbConstant[] constants; |
| 38 |
internal string[] usedNamespaces; |
| 39 |
internal PdbLines[] lines; |
| 40 |
internal ushort[]/*?*/ usingCounts; |
| 41 |
internal IEnumerable<INamespaceScope>/*?*/ namespaceScopes; |
| 42 |
internal string/*?*/ iteratorClass; |
| 43 |
internal List<ILocalScope>/*?*/ iteratorScopes; |
| 44 |
internal PdbSynchronizationInformation/*?*/ synchronizationInformation; |
| 45 |
|
| 46 |
private static string StripNamespace(string module) { |
| 47 |
int li = module.LastIndexOf('.'); |
| 48 |
if (li > 0) { |
| 49 |
return module.Substring(li + 1); |
| 50 |
} |
| 51 |
return module; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
internal static PdbFunction[] LoadManagedFunctions(/*string module,*/ |
| 56 |
BitAccess bits, uint limit, |
| 57 |
bool readStrings) { |
| 58 |
//string mod = StripNamespace(module); |
| 59 |
int begin = bits.Position; |
| 60 |
int count = 0; |
| 61 |
|
| 62 |
while (bits.Position < limit) { |
| 63 |
ushort siz; |
| 64 |
ushort rec; |
| 65 |
|
| 66 |
bits.ReadUInt16(out siz); |
| 67 |
int star = bits.Position; |
| 68 |
int stop = bits.Position + siz; |
| 69 |
bits.Position = star; |
| 70 |
bits.ReadUInt16(out rec); |
| 71 |
|
| 72 |
switch ((SYM)rec) { |
| 73 |
case SYM.S_GMANPROC: |
| 74 |
case SYM.S_LMANPROC: |
| 75 |
ManProcSym proc; |
| 76 |
bits.ReadUInt32(out proc.parent); |
| 77 |
bits.ReadUInt32(out proc.end); |
| 78 |
bits.Position = (int)proc.end; |
| 79 |
count++; |
| 80 |
break; |
| 81 |
|
| 82 |
case SYM.S_END: |
| 83 |
bits.Position = stop; |
| 84 |
break; |
| 85 |
|
| 86 |
default: |
| 87 |
//Console.WriteLine("{0,6}: {1:x2} {2}", |
| 88 |
// bits.Position, rec, (SYM)rec); |
| 89 |
bits.Position = stop; |
| 90 |
break; |
| 91 |
} |
| 92 |
} |
| 93 |
if (count == 0) { |
| 94 |
return null; |
| 95 |
} |
| 96 |
|
| 97 |
bits.Position = begin; |
| 98 |
PdbFunction[] funcs = new PdbFunction[count]; |
| 99 |
int func = 0; |
| 100 |
|
| 101 |
while (bits.Position < limit) { |
| 102 |
ushort siz; |
| 103 |
ushort rec; |
| 104 |
|
| 105 |
bits.ReadUInt16(out siz); |
| 106 |
int star = bits.Position; |
| 107 |
int stop = bits.Position + siz; |
| 108 |
bits.ReadUInt16(out rec); |
| 109 |
|
| 110 |
switch ((SYM)rec) { |
| 111 |
|
| 112 |
case SYM.S_GMANPROC: |
| 113 |
case SYM.S_LMANPROC: |
| 114 |
ManProcSym proc; |
| 115 |
//int offset = bits.Position; |
| 116 |
|
| 117 |
bits.ReadUInt32(out proc.parent); |
| 118 |
bits.ReadUInt32(out proc.end); |
| 119 |
bits.ReadUInt32(out proc.next); |
| 120 |
bits.ReadUInt32(out proc.len); |
| 121 |
bits.ReadUInt32(out proc.dbgStart); |
| 122 |
bits.ReadUInt32(out proc.dbgEnd); |
| 123 |
bits.ReadUInt32(out proc.token); |
| 124 |
bits.ReadUInt32(out proc.off); |
| 125 |
bits.ReadUInt16(out proc.seg); |
| 126 |
bits.ReadUInt8(out proc.flags); |
| 127 |
bits.ReadUInt16(out proc.retReg); |
| 128 |
if (readStrings) { |
| 129 |
bits.ReadCString(out proc.name); |
| 130 |
} else { |
| 131 |
bits.SkipCString(out proc.name); |
| 132 |
} |
| 133 |
//Console.WriteLine("token={0:X8} [{1}::{2}]", proc.token, module, proc.name); |
| 134 |
|
| 135 |
bits.Position = stop; |
| 136 |
funcs[func++] = new PdbFunction(/*module,*/ proc, bits); |
| 137 |
break; |
| 138 |
|
| 139 |
default: { |
| 140 |
//throw new PdbDebugException("Unknown SYMREC {0}", (SYM)rec); |
| 141 |
bits.Position = stop; |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
return funcs; |
| 147 |
} |
| 148 |
|
| 149 |
internal static void CountScopesAndSlots(BitAccess bits, uint limit, |
| 150 |
out int constants, out int scopes, out int slots, out int usedNamespaces) { |
| 151 |
int pos = bits.Position; |
| 152 |
BlockSym32 block; |
| 153 |
constants = 0; |
| 154 |
slots = 0; |
| 155 |
scopes = 0; |
| 156 |
usedNamespaces = 0; |
| 157 |
|
| 158 |
while (bits.Position < limit) { |
| 159 |
ushort siz; |
| 160 |
ushort rec; |
| 161 |
|
| 162 |
bits.ReadUInt16(out siz); |
| 163 |
int star = bits.Position; |
| 164 |
int stop = bits.Position + siz; |
| 165 |
bits.Position = star; |
| 166 |
bits.ReadUInt16(out rec); |
| 167 |
|
| 168 |
switch ((SYM)rec) { |
| 169 |
case SYM.S_BLOCK32: { |
| 170 |
bits.ReadUInt32(out block.parent); |
| 171 |
bits.ReadUInt32(out block.end); |
| 172 |
|
| 173 |
scopes++; |
| 174 |
bits.Position = (int)block.end; |
| 175 |
break; |
| 176 |
} |
| 177 |
|
| 178 |
case SYM.S_MANSLOT: |
| 179 |
slots++; |
| 180 |
bits.Position = stop; |
| 181 |
break; |
| 182 |
|
| 183 |
case SYM.S_UNAMESPACE: |
| 184 |
usedNamespaces++; |
| 185 |
bits.Position = stop; |
| 186 |
break; |
| 187 |
|
| 188 |
case SYM.S_MANCONSTANT: |
| 189 |
constants++; |
| 190 |
bits.Position = stop; |
| 191 |
break; |
| 192 |
|
| 193 |
default: |
| 194 |
bits.Position = stop; |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
bits.Position = pos; |
| 199 |
} |
| 200 |
|
| 201 |
internal PdbFunction() { |
| 202 |
} |
| 203 |
|
| 204 |
internal PdbFunction(/*string module, */ManProcSym proc, BitAccess bits) { |
| 205 |
this.token = proc.token; |
| 206 |
//this.module = module; |
| 207 |
//this.name = proc.name; |
| 208 |
//this.flags = proc.flags; |
| 209 |
this.segment = proc.seg; |
| 210 |
this.address = proc.off; |
| 211 |
//this.length = proc.len; |
| 212 |
|
| 213 |
if (proc.seg != 1) { |
| 214 |
throw new PdbDebugException("Segment is {0}, not 1.", proc.seg); |
| 215 |
} |
| 216 |
if (proc.parent != 0 || proc.next != 0) { |
| 217 |
throw new PdbDebugException("Warning parent={0}, next={1}", |
| 218 |
proc.parent, proc.next); |
| 219 |
} |
| 220 |
//if (proc.dbgStart != 0 || proc.dbgEnd != 0) { |
| 221 |
// throw new PdbDebugException("Warning DBG start={0}, end={1}", |
| 222 |
// proc.dbgStart, proc.dbgEnd); |
| 223 |
//} |
| 224 |
|
| 225 |
int constantCount; |
| 226 |
int scopeCount; |
| 227 |
int slotCount; |
| 228 |
int usedNamespacesCount; |
| 229 |
CountScopesAndSlots(bits, proc.end, out constantCount, out scopeCount, out slotCount, out usedNamespacesCount); |
| 230 |
int scope = constantCount > 0 || slotCount > 0 || usedNamespacesCount > 0 ? 1 : 0; |
| 231 |
int slot = 0; |
| 232 |
int constant = 0; |
| 233 |
int usedNs = 0; |
| 234 |
scopes = new PdbScope[scopeCount+scope]; |
| 235 |
slots = new PdbSlot[slotCount]; |
| 236 |
constants = new PdbConstant[constantCount]; |
| 237 |
usedNamespaces = new string[usedNamespacesCount]; |
| 238 |
|
| 239 |
if (scope > 0) |
| 240 |
scopes[0] = new PdbScope(this.address, proc.len, slots, constants, usedNamespaces); |
| 241 |
|
| 242 |
while (bits.Position < proc.end) { |
| 243 |
ushort siz; |
| 244 |
ushort rec; |
| 245 |
|
| 246 |
bits.ReadUInt16(out siz); |
| 247 |
int star = bits.Position; |
| 248 |
int stop = bits.Position + siz; |
| 249 |
bits.Position = star; |
| 250 |
bits.ReadUInt16(out rec); |
| 251 |
|
| 252 |
switch ((SYM)rec) { |
| 253 |
case SYM.S_OEM: { // 0x0404 |
| 254 |
OemSymbol oem; |
| 255 |
|
| 256 |
bits.ReadGuid(out oem.idOem); |
| 257 |
bits.ReadUInt32(out oem.typind); |
| 258 |
// internal byte[] rgl; // user data, force 4-byte alignment |
| 259 |
|
| 260 |
if (oem.idOem == msilMetaData) { |
| 261 |
string name = bits.ReadString(); |
| 262 |
if (name == "MD2") { |
| 263 |
byte version; |
| 264 |
bits.ReadUInt8(out version); |
| 265 |
if (version == 4) { |
| 266 |
byte count; |
| 267 |
bits.ReadUInt8(out count); |
| 268 |
bits.Align(4); |
| 269 |
while (count-- > 0) |
| 270 |
this.ReadCustomMetadata(bits); |
| 271 |
} |
| 272 |
} else if (name == "asyncMethodInfo") { |
| 273 |
this.synchronizationInformation = new PdbSynchronizationInformation(bits); |
| 274 |
} |
| 275 |
bits.Position = stop; |
| 276 |
break; |
| 277 |
} else { |
| 278 |
throw new PdbDebugException("OEM section: guid={0} ti={1}", |
| 279 |
oem.idOem, oem.typind); |
| 280 |
// bits.Position = stop; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
case SYM.S_BLOCK32: { |
| 285 |
BlockSym32 block = new BlockSym32(); |
| 286 |
|
| 287 |
bits.ReadUInt32(out block.parent); |
| 288 |
bits.ReadUInt32(out block.end); |
| 289 |
bits.ReadUInt32(out block.len); |
| 290 |
bits.ReadUInt32(out block.off); |
| 291 |
bits.ReadUInt16(out block.seg); |
| 292 |
bits.SkipCString(out block.name); |
| 293 |
bits.Position = stop; |
| 294 |
|
| 295 |
scopes[scope++] = new PdbScope(this.address, block, bits, out slotToken); |
| 296 |
bits.Position = (int)block.end; |
| 297 |
break; |
| 298 |
} |
| 299 |
|
| 300 |
case SYM.S_MANSLOT: |
| 301 |
slots[slot++] = new PdbSlot(bits); |
| 302 |
bits.Position = stop; |
| 303 |
break; |
| 304 |
|
| 305 |
case SYM.S_MANCONSTANT: |
| 306 |
constants[constant++] = new PdbConstant(bits); |
| 307 |
bits.Position = stop; |
| 308 |
break; |
| 309 |
|
| 310 |
case SYM.S_UNAMESPACE: |
| 311 |
bits.ReadCString(out usedNamespaces[usedNs++]); |
| 312 |
bits.Position = stop; |
| 313 |
break; |
| 314 |
|
| 315 |
case SYM.S_END: |
| 316 |
bits.Position = stop; |
| 317 |
break; |
| 318 |
|
| 319 |
default: { |
| 320 |
//throw new PdbDebugException("Unknown SYM: {0}", (SYM)rec); |
| 321 |
bits.Position = stop; |
| 322 |
break; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if (bits.Position != proc.end) { |
| 328 |
throw new PdbDebugException("Not at S_END"); |
| 329 |
} |
| 330 |
|
| 331 |
ushort esiz; |
| 332 |
ushort erec; |
| 333 |
bits.ReadUInt16(out esiz); |
| 334 |
bits.ReadUInt16(out erec); |
| 335 |
|
| 336 |
if (erec != (ushort)SYM.S_END) { |
| 337 |
throw new PdbDebugException("Missing S_END"); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
private void ReadCustomMetadata(BitAccess bits) { |
| 342 |
int savedPosition = bits.Position; |
| 343 |
byte version; |
| 344 |
bits.ReadUInt8(out version); |
| 345 |
if (version != 4) { |
| 346 |
throw new PdbDebugException("Unknown custom metadata item version: {0}", version); |
| 347 |
} |
| 348 |
byte kind; |
| 349 |
bits.ReadUInt8(out kind); |
| 350 |
bits.Align(4); |
| 351 |
uint numberOfBytesInItem; |
| 352 |
bits.ReadUInt32(out numberOfBytesInItem); |
| 353 |
switch (kind) { |
| 354 |
case 0: this.ReadUsingInfo(bits); break; |
| 355 |
case 1: this.ReadForwardInfo(bits); break; |
| 356 |
case 2: break; // this.ReadForwardedToModuleInfo(bits); break; |
| 357 |
case 3: this.ReadIteratorLocals(bits); break; |
| 358 |
case 4: this.ReadForwardIterator(bits); break; |
| 359 |
// TODO: handle unknown custom metadata, 5 & 6 are new with roslyn, see https://roslyn.codeplex.com/workitem/54 |
| 360 |
} |
| 361 |
bits.Position = savedPosition+(int)numberOfBytesInItem; |
| 362 |
} |
| 363 |
|
| 364 |
private void ReadForwardIterator(BitAccess bits) { |
| 365 |
this.iteratorClass = bits.ReadString(); |
| 366 |
} |
| 367 |
|
| 368 |
private void ReadIteratorLocals(BitAccess bits) { |
| 369 |
uint numberOfLocals; |
| 370 |
bits.ReadUInt32(out numberOfLocals); |
| 371 |
this.iteratorScopes = new List<ILocalScope>((int)numberOfLocals); |
| 372 |
while (numberOfLocals-- > 0) { |
| 373 |
uint ilStartOffset; |
| 374 |
uint ilEndOffset; |
| 375 |
bits.ReadUInt32(out ilStartOffset); |
| 376 |
bits.ReadUInt32(out ilEndOffset); |
| 377 |
this.iteratorScopes.Add(new PdbIteratorScope(ilStartOffset, ilEndOffset-ilStartOffset)); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
//private void ReadForwardedToModuleInfo(BitAccess bits) { |
| 382 |
//} |
| 383 |
|
| 384 |
private void ReadForwardInfo(BitAccess bits) { |
| 385 |
bits.ReadUInt32(out this.tokenOfMethodWhoseUsingInfoAppliesToThisMethod); |
| 386 |
} |
| 387 |
|
| 388 |
private void ReadUsingInfo(BitAccess bits) { |
| 389 |
ushort numberOfNamespaces; |
| 390 |
bits.ReadUInt16(out numberOfNamespaces); |
| 391 |
this.usingCounts = new ushort[numberOfNamespaces]; |
| 392 |
for (ushort i = 0; i < numberOfNamespaces; i++) { |
| 393 |
bits.ReadUInt16(out this.usingCounts[i]); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
internal class PdbFunctionsByAddress : IComparer { |
| 398 |
public int Compare(Object x, Object y) { |
| 399 |
PdbFunction fx = (PdbFunction)x; |
| 400 |
PdbFunction fy = (PdbFunction)y; |
| 401 |
|
| 402 |
if (fx.segment < fy.segment) { |
| 403 |
return -1; |
| 404 |
} else if (fx.segment > fy.segment) { |
| 405 |
return 1; |
| 406 |
} else if (fx.address < fy.address) { |
| 407 |
return -1; |
| 408 |
} else if (fx.address > fy.address) { |
| 409 |
return 1; |
| 410 |
} else { |
| 411 |
return 0; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
internal class PdbFunctionsByAddressAndToken : IComparer { |
| 417 |
public int Compare(Object x, Object y) { |
| 418 |
PdbFunction fx = (PdbFunction)x; |
| 419 |
PdbFunction fy = (PdbFunction)y; |
| 420 |
|
| 421 |
if (fx.segment < fy.segment) { |
| 422 |
return -1; |
| 423 |
} else if (fx.segment > fy.segment) { |
| 424 |
return 1; |
| 425 |
} else if (fx.address < fy.address) { |
| 426 |
return -1; |
| 427 |
} else if (fx.address > fy.address) { |
| 428 |
return 1; |
| 429 |
} else { |
| 430 |
if (fx.token < fy.token) |
| 431 |
return -1; |
| 432 |
else if (fx.token > fy.token) |
| 433 |
return 1; |
| 434 |
else |
| 435 |
return 0; |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
//internal class PdbFunctionsByToken : IComparer { |
| 441 |
// public int Compare(Object x, Object y) { |
| 442 |
// PdbFunction fx = (PdbFunction)x; |
| 443 |
// PdbFunction fy = (PdbFunction)y; |
| 444 |
|
| 445 |
// if (fx.token < fy.token) { |
| 446 |
// return -1; |
| 447 |
// } else if (fx.token > fy.token) { |
| 448 |
// return 1; |
| 449 |
// } else { |
| 450 |
// return 0; |
| 451 |
// } |
| 452 |
// } |
| 453 |
|
| 454 |
//} |
| 455 |
} |
| 456 |
|
| 457 |
internal class PdbSynchronizationInformation { |
| 458 |
internal uint kickoffMethodToken; |
| 459 |
internal uint generatedCatchHandlerIlOffset; |
| 460 |
internal PdbSynchronizationPoint[] synchronizationPoints; |
| 461 |
|
| 462 |
internal PdbSynchronizationInformation(BitAccess bits) { |
| 463 |
uint asyncStepInfoCount; |
| 464 |
bits.ReadUInt32(out this.kickoffMethodToken); |
| 465 |
bits.ReadUInt32(out this.generatedCatchHandlerIlOffset); |
| 466 |
bits.ReadUInt32(out asyncStepInfoCount); |
| 467 |
this.synchronizationPoints = new PdbSynchronizationPoint[asyncStepInfoCount]; |
| 468 |
for (uint i = 0; i < asyncStepInfoCount; i += 1) { |
| 469 |
this.synchronizationPoints[i] = new PdbSynchronizationPoint(bits); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
public uint GeneratedCatchHandlerOffset { |
| 474 |
get { return this.generatedCatchHandlerIlOffset; } |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
internal class PdbSynchronizationPoint { |
| 479 |
internal uint synchronizeOffset; |
| 480 |
internal uint continuationMethodToken; |
| 481 |
internal uint continuationOffset; |
| 482 |
|
| 483 |
internal PdbSynchronizationPoint(BitAccess bits) { |
| 484 |
bits.ReadUInt32(out this.synchronizeOffset); |
| 485 |
bits.ReadUInt32(out this.continuationMethodToken); |
| 486 |
bits.ReadUInt32(out this.continuationOffset); |
| 487 |
} |
| 488 |
|
| 489 |
public uint SynchronizeOffset { |
| 490 |
get { return this.synchronizeOffset; } |
| 491 |
} |
| 492 |
|
| 493 |
public uint ContinuationOffset { |
| 494 |
get { return this.continuationOffset; } |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
} |