17d62b00eSchristos /* Error reporting facilities. 27d62b00eSchristos 3*6881a400Schristos Copyright (C) 1986-2023 Free Software Foundation, Inc. 47d62b00eSchristos 57d62b00eSchristos This file is part of GDB. 67d62b00eSchristos 77d62b00eSchristos This program is free software; you can redistribute it and/or modify 87d62b00eSchristos it under the terms of the GNU General Public License as published by 97d62b00eSchristos the Free Software Foundation; either version 3 of the License, or 107d62b00eSchristos (at your option) any later version. 117d62b00eSchristos 127d62b00eSchristos This program is distributed in the hope that it will be useful, 137d62b00eSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 147d62b00eSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 157d62b00eSchristos GNU General Public License for more details. 167d62b00eSchristos 177d62b00eSchristos You should have received a copy of the GNU General Public License 187d62b00eSchristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 197d62b00eSchristos 207d62b00eSchristos #include "common-defs.h" 217d62b00eSchristos #include "errors.h" 22*6881a400Schristos #if defined (USE_WIN32API) || defined(__CYGWIN__) 23*6881a400Schristos #include <windows.h> 24*6881a400Schristos #endif /* USE_WIN32API */ 257d62b00eSchristos 267d62b00eSchristos /* See gdbsupport/errors.h. */ 277d62b00eSchristos 287d62b00eSchristos void 297d62b00eSchristos warning (const char *fmt, ...) 307d62b00eSchristos { 317d62b00eSchristos va_list ap; 327d62b00eSchristos 337d62b00eSchristos va_start (ap, fmt); 347d62b00eSchristos vwarning (fmt, ap); 357d62b00eSchristos va_end (ap); 367d62b00eSchristos } 377d62b00eSchristos 387d62b00eSchristos /* See gdbsupport/errors.h. */ 397d62b00eSchristos 407d62b00eSchristos void 417d62b00eSchristos error (const char *fmt, ...) 427d62b00eSchristos { 437d62b00eSchristos va_list ap; 447d62b00eSchristos 457d62b00eSchristos va_start (ap, fmt); 467d62b00eSchristos verror (fmt, ap); 477d62b00eSchristos va_end (ap); 487d62b00eSchristos } 497d62b00eSchristos 507d62b00eSchristos /* See gdbsupport/errors.h. */ 517d62b00eSchristos 527d62b00eSchristos void 53*6881a400Schristos internal_error_loc (const char *file, int line, const char *fmt, ...) 547d62b00eSchristos { 557d62b00eSchristos va_list ap; 567d62b00eSchristos 577d62b00eSchristos va_start (ap, fmt); 587d62b00eSchristos internal_verror (file, line, fmt, ap); 597d62b00eSchristos va_end (ap); 607d62b00eSchristos } 617d62b00eSchristos 627d62b00eSchristos /* See gdbsupport/errors.h. */ 637d62b00eSchristos 647d62b00eSchristos void 65*6881a400Schristos internal_warning_loc (const char *file, int line, const char *fmt, ...) 667d62b00eSchristos { 677d62b00eSchristos va_list ap; 687d62b00eSchristos 697d62b00eSchristos va_start (ap, fmt); 707d62b00eSchristos internal_vwarning (file, line, fmt, ap); 717d62b00eSchristos va_end (ap); 727d62b00eSchristos } 73*6881a400Schristos 74*6881a400Schristos #if defined (USE_WIN32API) || defined(__CYGWIN__) 75*6881a400Schristos 76*6881a400Schristos /* See errors.h. */ 77*6881a400Schristos 78*6881a400Schristos const char * 79*6881a400Schristos strwinerror (ULONGEST error) 80*6881a400Schristos { 81*6881a400Schristos static char buf[1024]; 82*6881a400Schristos TCHAR *msgbuf; 83*6881a400Schristos DWORD lasterr = GetLastError (); 84*6881a400Schristos DWORD chars = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM 85*6881a400Schristos | FORMAT_MESSAGE_ALLOCATE_BUFFER, 86*6881a400Schristos NULL, 87*6881a400Schristos error, 88*6881a400Schristos 0, /* Default language */ 89*6881a400Schristos (LPTSTR) &msgbuf, 90*6881a400Schristos 0, 91*6881a400Schristos NULL); 92*6881a400Schristos if (chars != 0) 93*6881a400Schristos { 94*6881a400Schristos /* If there is an \r\n appended, zap it. */ 95*6881a400Schristos if (chars >= 2 96*6881a400Schristos && msgbuf[chars - 2] == '\r' 97*6881a400Schristos && msgbuf[chars - 1] == '\n') 98*6881a400Schristos { 99*6881a400Schristos chars -= 2; 100*6881a400Schristos msgbuf[chars] = 0; 101*6881a400Schristos } 102*6881a400Schristos 103*6881a400Schristos if (chars > ARRAY_SIZE (buf) - 1) 104*6881a400Schristos { 105*6881a400Schristos chars = ARRAY_SIZE (buf) - 1; 106*6881a400Schristos msgbuf [chars] = 0; 107*6881a400Schristos } 108*6881a400Schristos 109*6881a400Schristos #ifdef UNICODE 110*6881a400Schristos wcstombs (buf, msgbuf, chars + 1); 111*6881a400Schristos #else 112*6881a400Schristos strncpy (buf, msgbuf, chars + 1); 113*6881a400Schristos #endif 114*6881a400Schristos LocalFree (msgbuf); 115*6881a400Schristos } 116*6881a400Schristos else 117*6881a400Schristos sprintf (buf, "unknown win32 error (%u)", (unsigned) error); 118*6881a400Schristos 119*6881a400Schristos SetLastError (lasterr); 120*6881a400Schristos return buf; 121*6881a400Schristos } 122*6881a400Schristos 123*6881a400Schristos #endif /* USE_WIN32API */ 124