Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Windows debug output #1541

Merged
merged 5 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/include/general.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
#define INCLUDE_GENERAL_H

#ifndef _GNU_SOURCE
// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
#define _GNU_SOURCE
#endif
#ifndef _DEFAULT_SOURCE
// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
#define _DEFAULT_SOURCE
#endif
#if !defined(__USE_MINGW_ANSI_STDIO)
// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
#define __USE_MINGW_ANSI_STDIO 1
#endif
#include <stdint.h>
Expand Down Expand Up @@ -93,17 +96,6 @@ void debug_serial_send_stdout(const uint8_t *data, size_t len);
#else
#include "debug.h"

#if defined(_WIN32) || defined(__CYGWIN__)
#define DEBUG_WIDEN(fmt) L##fmt
#define DEBUG_ERROR(fmt, ...) debug_error(DEBUG_WIDEN(fmt), ##__VA_ARGS__)
#define DEBUG_WARN(fmt, ...) debug_warning(DEBUG_WIDEN(fmt), ##__VA_ARGS__)
#define DEBUG_INFO(fmt, ...) debug_info(DEBUG_WIDEN(fmt), ##__VA_ARGS__)
#define DEBUG_GDB(fmt, ...) debug_gdb(DEBUG_WIDEN(fmt), ##__VA_ARGS__)
#define DEBUG_TARGET(fmt, ...) debug_target(DEBUG_WIDEN(fmt), ##__VA_ARGS__)
#define DEBUG_PROTO(fmt, ...) debug_protocol(DEBUG_WIDEN(fmt), ##__VA_ARGS__)
#define DEBUG_PROBE(fmt, ...) debug_probe(DEBUG_WIDEN(fmt), ##__VA_ARGS__)
#define DEBUG_WIRE(fmt, ...) debug_wire(DEBUG_WIDEN(fmt), ##__VA_ARGS__)
#else
#define DEBUG_ERROR(...) debug_error(__VA_ARGS__)
#define DEBUG_WARN(...) debug_warning(__VA_ARGS__)
#define DEBUG_INFO(...) debug_info(__VA_ARGS__)
Expand All @@ -113,7 +105,6 @@ void debug_serial_send_stdout(const uint8_t *data, size_t len);
#define DEBUG_PROBE(...) debug_probe(__VA_ARGS__)
#define DEBUG_WIRE(...) debug_wire(__VA_ARGS__)
#endif
#endif

#define ALIGN(x, n) (((x) + (n)-1) & ~((n)-1))
#undef MIN
Expand Down
4 changes: 2 additions & 2 deletions src/platforms/hosted/bmp_serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int find_debuggers(bmda_cli_options_s *cl_opts, bmp_info_s *info)
(BYTE *)busReportedDeviceSesc, sizeof busReportedDeviceSesc, &dwSize, 0)) {
probes_found++;
if (is_printing_probes_info) {
DEBUG_WARN("%2d: %s, %ls\n", probes_found, serial_number, busReportedDeviceSesc);
DEBUG_WARN("%2zu: %s, %ls\n", probes_found, serial_number, busReportedDeviceSesc);
} else {
bool probe_identified = true;
if ((cl_opts->opt_serial && strstr(serial_number, cl_opts->opt_serial)) ||
Expand Down Expand Up @@ -152,7 +152,7 @@ int find_debuggers(bmda_cli_options_s *cl_opts, bmp_info_s *info)
* and no probe was identified as selected by the user.
* Restart the identification loop, this time printing the probe information,
* and then return. */
DEBUG_WARN("%d debuggers found!\nSelect with -P <pos>, or "
DEBUG_WARN("%zu debuggers found!\nSelect with -P <pos>, or "
"-s <(partial)serial no.>\n",
probes_found);
probes_found = 0;
Expand Down
27 changes: 10 additions & 17 deletions src/platforms/hosted/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,21 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#if defined(_WIN32) || defined(__CYGWIN__)
#include <wchar.h>
#define PRINT_FN vfwprintf
#else
#include <stdio.h>
#define PRINT_FN vfprintf
#endif
#include <stdarg.h>
#include "general.h"
#include "debug.h"

uint16_t bmda_debug_flags = BMD_DEBUG_ERROR | BMD_DEBUG_WARNING;

static void debug_print(const uint16_t level, const debug_str_t format, va_list args)
static void debug_print(const uint16_t level, const char *format, va_list args)
{
/* Check if the required level is enabled */
if (!(bmda_debug_flags & level))
return;
/* Check to see which of stderr and stdout the message should go to */
FILE *const where = bmda_debug_flags & BMD_DEBUG_USE_STDERR ? stderr : stdout;
/* And shoot the message to the correct place */
(void)PRINT_FN(where, format, args);
(void)vfprintf(where, format, args);
/* Note: we have no useful way to use the output of the above call, so we ignore it. */
}

Expand All @@ -62,42 +55,42 @@ static void debug_print(const uint16_t level, const debug_str_t format, va_list
debug_print((level), format, args); \
va_end(args)

void debug_error(const debug_str_t format, ...)
void debug_error(const char *format, ...)
{
DEBUG_PRINT(BMD_DEBUG_ERROR);
}

void debug_warning(const debug_str_t format, ...)
void debug_warning(const char *format, ...)
{
DEBUG_PRINT(BMD_DEBUG_WARNING);
}

void debug_info(const debug_str_t format, ...)
void debug_info(const char *format, ...)
{
DEBUG_PRINT(BMD_DEBUG_INFO);
}

void debug_gdb(const debug_str_t format, ...)
void debug_gdb(const char *format, ...)
{
DEBUG_PRINT(BMD_DEBUG_GDB);
}

void debug_target(const debug_str_t format, ...)
void debug_target(const char *format, ...)
{
DEBUG_PRINT(BMD_DEBUG_TARGET);
}

void debug_protocol(const debug_str_t format, ...)
void debug_protocol(const char *format, ...)
{
DEBUG_PRINT(BMD_DEBUG_PROTO);
}

void debug_probe(const debug_str_t format, ...)
void debug_probe(const char *format, ...)
{
DEBUG_PRINT(BMD_DEBUG_PROBE);
}

void debug_wire(const debug_str_t format, ...)
void debug_wire(const char *format, ...)
{
DEBUG_PRINT(BMD_DEBUG_WIRE);
}
26 changes: 15 additions & 11 deletions src/platforms/hosted/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@
#ifndef PLATFORMS_HOSTED_DEBUG_H
#define PLATFORMS_HOSTED_DEBUG_H

#if !defined(__USE_MINGW_ANSI_STDIO)
// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
#define __USE_MINGW_ANSI_STDIO 1
#endif
#include <stdint.h>
#include <stdio.h>
typedef const char *debug_str_t;
#if defined(_WIN32) || defined(__CYGWIN__)
typedef const wchar_t *debug_str_t;
#define DEBUG_FORMAT_ATTR /*__attribute__((format(__wprintf__, 1, 2)))*/
#define DEBUG_FORMAT_ATTR __attribute__((format(__MINGW_PRINTF_FORMAT, 1, 2)))
#else
typedef const char *debug_str_t;
#define DEBUG_FORMAT_ATTR __attribute__((format(printf, 1, 2)))
#endif

Expand All @@ -59,13 +63,13 @@ typedef const char *debug_str_t;

extern uint16_t bmda_debug_flags;

void debug_error(debug_str_t format, ...) DEBUG_FORMAT_ATTR;
void debug_warning(debug_str_t format, ...) DEBUG_FORMAT_ATTR;
void debug_info(debug_str_t format, ...) DEBUG_FORMAT_ATTR;
void debug_gdb(debug_str_t format, ...) DEBUG_FORMAT_ATTR;
void debug_target(debug_str_t format, ...) DEBUG_FORMAT_ATTR;
void debug_protocol(debug_str_t format, ...) DEBUG_FORMAT_ATTR;
void debug_probe(debug_str_t format, ...) DEBUG_FORMAT_ATTR;
void debug_wire(debug_str_t format, ...) DEBUG_FORMAT_ATTR;
void debug_error(const char *format, ...) DEBUG_FORMAT_ATTR;
void debug_warning(const char *format, ...) DEBUG_FORMAT_ATTR;
void debug_info(const char *format, ...) DEBUG_FORMAT_ATTR;
void debug_gdb(const char *format, ...) DEBUG_FORMAT_ATTR;
void debug_target(const char *format, ...) DEBUG_FORMAT_ATTR;
void debug_protocol(const char *format, ...) DEBUG_FORMAT_ATTR;
void debug_probe(const char *format, ...) DEBUG_FORMAT_ATTR;
void debug_wire(const char *format, ...) DEBUG_FORMAT_ATTR;

#endif /*PLATFORMS_HOSTED_DEBUG_H*/
4 changes: 3 additions & 1 deletion src/platforms/hosted/gdb_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <winsock2.h>

typedef SOCKET socket_t;
#define PRI_SOCKET "zu"
#ifndef __CYGWIN__
typedef signed long long ssize_t;
#endif
Expand All @@ -46,6 +47,7 @@ typedef signed long long ssize_t;
#include <fcntl.h>

typedef int32_t socket_t;
#define PRI_SOCKET "d"
#define INVALID_SOCKET (-1)
#endif

Expand Down Expand Up @@ -170,7 +172,7 @@ static void display_socket_error(const int error, const socket_t socket, const c
#else
const char *message = strerror(error);
#endif
DEBUG_ERROR("Error %s %d, got error %d: %s\n", operation, socket, error, message);
DEBUG_ERROR("Error %s %" PRI_SOCKET ", got error %d: %s\n", operation, socket, error, message);
#if defined(_WIN32) || defined(__CYGWIN__)
LocalFree(message);
#endif
Expand Down
12 changes: 10 additions & 2 deletions src/platforms/hosted/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* Handle different BMP pc-hosted platforms/
*/
/* Implements core platform-specific functionality for BMDA */

#if defined(_WIN32) || defined(__CYGWIN__)
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

#include "general.h"
#include "platform.h"
Expand Down Expand Up @@ -90,6 +95,9 @@ static void sigterm_handler(int sig)

void platform_init(int argc, char **argv)
{
#if defined(_WIN32) || defined(__CYGWIN__)
SetConsoleOutputCP(CP_UTF8);
#endif
cl_init(&cl_opts, argc, argv);
atexit(exit_function);
signal(SIGTERM, sigterm_handler);
Expand Down
2 changes: 1 addition & 1 deletion src/platforms/hosted/serial_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static void display_error(const LSTATUS error, const char *const operation, cons
char *message = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *)&message, 0, NULL);
DEBUG_ERROR("Error %s %s, got error %08x: %s\n", operation, path, error, message);
DEBUG_ERROR("Error %s %s, got error %08lx: %s\n", operation, path, error, message);
LocalFree(message);
}

Expand Down