File size: 19,887 Bytes
8c763fb | 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#if UNIX
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Management.Automation.Internal;
namespace System.Management.Automation.Tracing
{
/// <summary>
/// Encapsulates the message resource and SysLog logging for an ETW event.
/// The other half of the partial class is generated by EtwGen and contains a
/// static dictionary containing the event id mapped to the associated event meta data
/// and resource string reference.
/// </summary>
/// <remarks>
/// This component logs ETW trace events to syslog.
/// The log entries use the following common format
/// (commitId:threadId:channelid) [context] payload
/// Where:
/// commitId: A hash code of the full git commit id string.
/// threadid: The thread identifier of calling code.
/// channelid: The identifier for the output channel. See PSChannel for values.
/// context: Dependent on the type of log entry.
/// payload: Dependent on the type of log entry.
/// Note:
/// commitId, threadId, and eventId are logged as HEX without a leading
/// '0x'.
///
/// 4 types of log entries are produced.
/// NOTE: Where constant string are logged, the template places the string in
/// double quotes. For example, the GitCommitId log entry uses "GitCommitId"
/// for the context value.
///
/// Note that the examples illustrate the output from SysLogProvider.Log,
/// Data automatically prepended by syslog, such as timestamp, hostname, ident,
/// and processid are not shown.
///
/// GitCommitId
/// This is the first log entry for a session. It provides a correlation
/// between the full git commit id string and a hash code used for subsequent
/// log entries.
/// Context: "GitCommitId"
/// Payload: string "Hash:" hashcode as HEX string.
/// For official builds, the GitCommitID is the release tag. For other builds the commit id may include an SHA-1 hash at the
/// end of the release tag.
/// Example 1: Official release
/// (19E1025:3:10) [GitCommitId] v6.0.0-beta.9 Hash:64D0C08D
/// Example 2: Commit id with SHA-1 hash
/// (19E1025:3:10) [GitCommitId] v6.0.0-beta.8-67-gca2630a3dea6420a3cd3914c84a74c1c45311f54 Hash:8EE3A3B3
///
/// Transfer
/// A log entry to record a transfer event.
/// Context: "Transfer"
/// The playload is two, space separated string guids, the first being the
/// parent activityid followed by the new activityid.
/// Example: (19E1025:3:10) [Transfer] {de168a71-6bb9-47e4-8712-bc02506d98be} {ab0077f6-c042-4728-be76-f688cfb1b054}
///
/// Activity
/// A log entry for when activity is set.
/// Context: "Activity"
/// Payload: The string guid of the activity id.
/// Example: (19E1025:3:10) [Activity] {ab0077f6-c042-4728-be76-f688cfb1b054}
///
/// Event
/// Application logging (Events)
/// Context: EventId:taskname.opcodename.levelname
/// Payload: The event's message text formatted with arguments from the caller.
/// Example: (19E1025:3:10) [Perftrack_ConsoleStartupStart:PowershellConsoleStartup.WinStart.Informational] PowerShell console is starting up
/// </remarks>
internal class SysLogProvider
{
// Ensure the string pointer is not garbage collected.
private static IntPtr _nativeSyslogIdent = IntPtr.Zero;
private static readonly NativeMethods.SysLogPriority _facility = NativeMethods.SysLogPriority.Local0;
private readonly byte _channelFilter;
private readonly ulong _keywordFilter;
private readonly byte _levelFilter;
/// <summary>
/// Initializes a new instance of this class.
/// </summary>
/// <param name="applicationId">The log identity name used to identify the application in syslog.</param>
/// <param name="level">The trace level to enable.</param>
/// <param name="keywords">The keywords to enable.</param>
/// <param name="channels">The output channels to enable.</param>
public SysLogProvider(string applicationId, PSLevel level, PSKeyword keywords, PSChannel channels)
{
// NOTE: This string needs to remain valid for the life of the process since the underlying API keeps
// a reference to it.
// FUTURE: If logging is redesigned, make these details static or a singleton since there should only be one
// instance active.
_nativeSyslogIdent = Marshal.StringToHGlobalAnsi(applicationId);
NativeMethods.OpenLog(_nativeSyslogIdent, _facility);
_keywordFilter = (ulong)keywords;
_levelFilter = (byte)level;
_channelFilter = (byte)channels;
if ((_channelFilter & (ulong)PSChannel.Operational) != 0)
{
_keywordFilter |= (ulong)PSKeyword.UseAlwaysOperational;
}
if ((_channelFilter & (ulong)PSChannel.Analytic) != 0)
{
_keywordFilter |= (ulong)PSKeyword.UseAlwaysAnalytic;
}
}
/// <summary>
/// Defines a thread local StringBuilder for building log messages.
/// </summary>
/// <remarks>
/// NOTE: do not access this field directly, use the MessageBuilder
/// property to ensure correct thread initialization; otherwise, a null reference can occur.
/// </remarks>
[ThreadStatic]
private static StringBuilder t_messageBuilder;
private static StringBuilder MessageBuilder
{
get
{
// NOTE: Thread static fields must be explicitly initialized for each thread.
t_messageBuilder ??= new StringBuilder(200);
return t_messageBuilder;
}
}
/// <summary>
/// Defines a activity id for the current thread.
/// </summary>
/// <remarks>
/// NOTE: do not access this field directly, use the Activity property
/// to ensure correct thread initialization.
/// </remarks>
[ThreadStatic]
private static Guid? t_activity;
private static Guid Activity
{
get
{
if (!t_activity.HasValue)
{
// NOTE: Thread static fields must be explicitly initialized for each thread.
t_activity = Guid.NewGuid();
}
return t_activity.Value;
}
set
{
t_activity = value;
}
}
/// <summary>
/// Gets the value indicating if the specified level and keywords are enabled for logging.
/// </summary>
/// <param name="level">The PSLevel to check.</param>
/// <param name="keywords">The PSKeyword to check.</param>
/// <returns>True if the specified level and keywords are enabled for logging.</returns>
internal bool IsEnabled(PSLevel level, PSKeyword keywords)
{
return ((ulong)keywords & _keywordFilter) != 0
&& ((int)level <= _levelFilter);
}
// NOTE: There are a number of places where PowerShell code sends analytic events
// to the operational channel. This is a side-effect of the custom wrappers that
// use flags that are not consistent with the event definition.
// To ensure filtering of analytic events is consistent, both keyword and channel
// filtering is performed to suppress analytic events.
private bool ShouldLog(PSLevel level, PSKeyword keywords, PSChannel channel)
{
return (_channelFilter & (ulong)channel) != 0
&& IsEnabled(level, keywords);
}
#region resource manager
private static global::System.Resources.ResourceManager _resourceManager;
private static global::System.Globalization.CultureInfo _resourceCulture;
private static global::System.Resources.ResourceManager ResourceManager
{
get
{
_resourceManager ??= new global::System.Resources.ResourceManager("System.Management.Automation.resources.EventResource", typeof(EventResource).Assembly);
return _resourceManager;
}
}
/// <summary>
/// Overrides the current threads CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture
{
get
{
return _resourceCulture;
}
set
{
_resourceCulture = value;
}
}
private static string GetResourceString(string resourceName)
{
string value = ResourceManager.GetString(resourceName, Culture);
if (string.IsNullOrEmpty(value))
{
value = string.Create(CultureInfo.InvariantCulture, $"Unknown resource: {resourceName}");
Diagnostics.Assert(false, value);
}
return value;
}
#endregion resource manager
/// <summary>
/// Gets the EventMessage for a given event.
/// </summary>
/// <param name="sb">The StringBuilder to append.</param>
/// <param name="eventId">The id of the event to retrieve.</param>
/// <param name="args">An array of zero or more payload objects.</param>
private static void GetEventMessage(StringBuilder sb, PSEventId eventId, params object[] args )
{
int parameterCount;
string resourceName = EventResource.GetMessage((int)eventId, out parameterCount);
if (resourceName == null)
{
// If an event id was specified that is not found in the event resource lookup table,
// use a placeholder message that includes the event id.
resourceName = EventResource.GetMissingEventMessage(out parameterCount);
Diagnostics.Assert(false, sb.ToString());
args = new object[] {eventId};
}
string resourceValue = GetResourceString(resourceName);
if (parameterCount > 0)
{
sb.AppendFormat(resourceValue, args);
}
else
{
sb.Append(resourceValue);
}
}
#region logging
// maps a LogLevel to an associated SysLogPriority.
private static readonly NativeMethods.SysLogPriority[] _levels =
{
NativeMethods.SysLogPriority.Info,
NativeMethods.SysLogPriority.Critical,
NativeMethods.SysLogPriority.Error,
NativeMethods.SysLogPriority.Warning,
NativeMethods.SysLogPriority.Info,
NativeMethods.SysLogPriority.Info
};
/// <summary>
/// Logs a activity transfer.
/// </summary>
/// <param name="parentActivityId">The parent activity id.</param>
public void LogTransfer(Guid parentActivityId)
{
// NOTE: always log
int threadId = Environment.CurrentManagedThreadId;
string message = string.Format(CultureInfo.InvariantCulture,
"({0}:{1:X}:{2:X}) [Transfer]:{3} {4}",
PSVersionInfo.GitCommitId, threadId, PSChannel.Operational,
parentActivityId.ToString("B"),
Activity.ToString("B"));
NativeMethods.SysLog(NativeMethods.SysLogPriority.Info, message);
}
/// <summary>
/// Logs the activity identifier for the current thread.
/// </summary>
/// <param name="activity">The Guid activity identifier.</param>
public void SetActivity(Guid activity)
{
int threadId = Environment.CurrentManagedThreadId;
Activity = activity;
// NOTE: always log
string message = string.Format(CultureInfo.InvariantCulture,
"({0:X}:{1:X}:{2:X}) [Activity] {3}",
PSVersionInfo.GitCommitId, threadId, PSChannel.Operational, activity.ToString("B"));
NativeMethods.SysLog(NativeMethods.SysLogPriority.Info, message);
}
/// <summary>
/// Writes a log entry.
/// </summary>
/// <param name="eventId">The event id of the log entry.</param>
/// <param name="channel">The channel to log.</param>
/// <param name="task">The task for the log entry.</param>
/// <param name="opcode">The operation for the log entry.</param>
/// <param name="level">The logging level.</param>
/// <param name="keyword">The keyword(s) for the event.</param>
/// <param name="args">The payload for the log message.</param>
public void Log(PSEventId eventId, PSChannel channel, PSTask task, PSOpcode opcode, PSLevel level, PSKeyword keyword, params object[] args)
{
if (ShouldLog(level, keyword, channel))
{
int threadId = Environment.CurrentManagedThreadId;
StringBuilder sb = MessageBuilder;
sb.Clear();
// add the message preamble
sb.AppendFormat(CultureInfo.InvariantCulture,
"({0}:{1:X}:{2:X}) [{3:G}:{4:G}.{5:G}.{6:G}] ",
PSVersionInfo.GitCommitId, threadId, channel, eventId, task, opcode, level);
// add the message
GetEventMessage(sb, eventId, args);
NativeMethods.SysLogPriority priority;
if ((int)level <= _levels.Length)
{
priority = _levels[(int)level];
}
else
{
priority = NativeMethods.SysLogPriority.Info;
}
// log it.
NativeMethods.SysLog(priority, sb.ToString());
}
}
#endregion logging
}
internal enum LogLevel : uint
{
Always = 0,
Critical = 1,
Error = 2,
Warning = 3,
Information = 4,
Verbose = 5
}
internal static class NativeMethods
{
private const string libpslnative = "libpsl-native";
/// <summary>
/// Write a message to the system logger, which in turn writes the message to the system console, log files, etc.
/// See man 3 syslog for more info.
/// </summary>
/// <param name="priority">
/// The OR of a priority and facility in the SysLogPriority enum indicating the priority and facility of the log entry.
/// </param>
/// <param name="message">The message to put in the log entry.</param>
[DllImport(libpslnative, CharSet = CharSet.Ansi, EntryPoint = "Native_SysLog")]
internal static extern void SysLog(SysLogPriority priority, string message);
[DllImport(libpslnative, CharSet = CharSet.Ansi, EntryPoint = "Native_OpenLog")]
internal static extern void OpenLog(IntPtr ident, SysLogPriority facility);
[DllImport(libpslnative, EntryPoint = "Native_CloseLog")]
internal static extern void CloseLog();
[Flags]
internal enum SysLogPriority : uint
{
// Priorities enum values.
/// <summary>
/// System is unusable.
/// </summary>
Emergency = 0,
/// <summary>
/// Action must be taken immediately.
/// </summary>
Alert = 1,
/// <summary>
/// Critical conditions.
/// </summary>
Critical = 2,
/// <summary>
/// Error conditions.
/// </summary>
Error = 3,
/// <summary>
/// Warning conditions.
/// </summary>
Warning = 4,
/// <summary>
/// Normal but significant condition.
/// </summary>
Notice = 5,
/// <summary>
/// Informational.
/// </summary>
Info = 6,
/// <summary>
/// Debug-level messages.
/// </summary>
Debug = 7,
// Facility enum values.
/// <summary>
/// Kernel messages.
/// </summary>
Kernel = (0 << 3),
/// <summary>
/// Random user-level messages.
/// </summary>
User = (1 << 3),
/// <summary>
/// Mail system.
/// </summary>
Mail = (2 << 3),
/// <summary>
/// System daemons.
/// </summary>
Daemon = (3 << 3),
/// <summary>
/// Authorization messages.
/// </summary>
Authorization = (4 << 3),
/// <summary>
/// Messages generated internally by syslogd.
/// </summary>
Syslog = (5 << 3),
/// <summary>
/// Line printer subsystem.
/// </summary>
Lpr = (6 << 3),
/// <summary>
/// Network news subsystem.
/// </summary>
News = (7 << 3),
/// <summary>
/// UUCP subsystem.
/// </summary>
Uucp = (8 << 3),
/// <summary>
/// Clock daemon.
/// </summary>
Cron = (9 << 3),
/// <summary>
/// Security/authorization messages (private)
/// </summary>
Authpriv = (10 << 3),
/// <summary>
/// FTP daemon.
/// </summary>
Ftp = (11 << 3),
// Reserved for system use
/// <summary>
/// Reserved for local use.
/// </summary>
Local0 = (16 << 3),
/// <summary>
/// Reserved for local use.
/// </summary>
Local1 = (17 << 3),
/// <summary>
/// Reserved for local use.
/// </summary>
Local2 = (18 << 3),
/// <summary>
/// Reserved for local use.
/// </summary>
Local3 = (19 << 3),
/// <summary>
/// Reserved for local use.
/// </summary>
Local4 = (20 << 3),
/// <summary>
/// Reserved for local use.
/// </summary>
Local5 = (21 << 3),
/// <summary>
/// Reserved for local use.
/// </summary>
Local6 = (22 << 3),
/// <summary>
/// Reserved for local use.
/// </summary>
Local7 = (23 << 3),
}
}
}
#endif // UNIX
|