blob: af9f66bb7b2f3314fc60e0b9b94d1807e084401e [file] [log] [blame]
Greg Claytonb43767a2011-03-22 01:34:44 +00001//===-- lldb-platform.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include <errno.h>
11#include <getopt.h>
12#include <signal.h>
13#include <stdint.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include "GDBRemoteCommunication.h"
19
20//----------------------------------------------------------------------
21// option descriptors for getopt_long()
22//----------------------------------------------------------------------
23
24int g_debug = 0;
25int g_verbose = 0;
26
27static struct option g_long_options[] =
28{
29 { "debug", no_argument, &g_debug, 1 },
30 { "verbose", no_argument, &g_verbose, 1 },
31 { "log-file", required_argument, NULL, 'l' },
32 { "log-flags", required_argument, NULL, 'f' },
33 { NULL, 0, NULL, 0 }
34};
35
36//----------------------------------------------------------------------
37// Watch for signals
38//----------------------------------------------------------------------
39int g_sigpipe_received = 0;
40void
41signal_handler(int signo)
42{
43 switch (signo)
44 {
45 case SIGPIPE:
46 g_sigpipe_received = 1;
47 break;
48 }
49}
50
51//----------------------------------------------------------------------
52// main
53//----------------------------------------------------------------------
54int
55main (int argc, char *argv[])
56{
57 signal (SIGPIPE, signal_handler);
58 int long_option_index = 0;
59 FILE* log_file = NULL;
60 uint32_t log_flags = 0;
61 char ch;
62
63 while ((ch = getopt_long(argc, argv, "l:f:", g_long_options, &long_option_index)) != -1)
64 {
65// DNBLogDebug("option: ch == %c (0x%2.2x) --%s%c%s\n",
66// ch, (uint8_t)ch,
67// g_long_options[long_option_index].name,
68// g_long_options[long_option_index].has_arg ? '=' : ' ',
69// optarg ? optarg : "");
70 switch (ch)
71 {
72 case 0: // Any optional that auto set themselves will return 0
73 break;
74
75 case 'l': // Set Log File
76 if (optarg && optarg[0])
77 {
78 if (strcasecmp(optarg, "stdout") == 0)
79 log_file = stdout;
80 else if (strcasecmp(optarg, "stderr") == 0)
81 log_file = stderr;
82 else
83 {
84 log_file = fopen(optarg, "w");
85 if (log_file != NULL)
86 setlinebuf(log_file);
87 }
88
89 if (log_file == NULL)
90 {
91 const char *errno_str = strerror(errno);
92 fprintf (stderr, "Failed to open log file '%s' for writing: errno = %i (%s)", optarg, errno, errno_str ? errno_str : "unknown error");
93 }
94 }
95 break;
96
97 case 'f': // Log Flags
98 if (optarg && optarg[0])
99 log_flags = strtoul(optarg, NULL, 0);
100 break;
101 }
102 }
103
104 // Skip any options we consumed with getopt_long
105 argc -= optind;
106 argv += optind;
107
108
109 GDBRemoteCommunication gdb_comm;
110
111 return 0;
112}