Add ${ansi.XX} parsing to lldb prompt, use-color setting, and -no-use-colors command line options.

settings set use-color [false|true]
settings set prompt "${ansi.bold}${ansi.fg.green}(lldb)${ansi.normal} "
also "--no-use-colors" on the command prompt

llvm-svn: 182609
This commit is contained in:
Michael Sartain 2013-05-23 20:47:45 +00:00
parent 75ef31f607
commit c3ce7f2740
10 changed files with 170 additions and 226 deletions

View File

@ -172,6 +172,12 @@ public:
bool
GetUseExternalEditor ();
bool
SetUseColor (bool use_color);
bool
GetUseColor () const;
static bool
GetDefaultArchitecture (char *arch_name, size_t arch_name_len);

View File

@ -236,7 +236,6 @@ public:
const ExecutionContext *exe_ctx,
const Address *addr,
Stream &s,
const char **end,
ValueObject* valobj = NULL);
@ -308,6 +307,12 @@ public:
bool
SetUseExternalEditor (bool use_external_editor_p);
bool
GetUseColor () const;
bool
SetUseColor (bool use_color);
uint32_t
GetStopSourceLineCount (bool before) const;

View File

@ -21,7 +21,7 @@
#define ANSI_BG_COLOR_BLACK 40
#define ANSI_BG_COLOR_RED 41
#define ANSI_BG_COLOR_GREEN 42
#define ANSI_BG_COLOR_YELLOW 44
#define ANSI_BG_COLOR_YELLOW 43
#define ANSI_BG_COLOR_BLUE 44
#define ANSI_BG_COLOR_PURPLE 45
#define ANSI_BG_COLOR_CYAN 46
@ -82,5 +82,75 @@ namespace lldb_utility {
const char *k_ctrl_conceal = "8";
const char *k_ctrl_crossed_out = "9";
inline std::string
FormatAnsiTerminalCodes(const char *format, bool do_color = true)
{
// Convert "${ansi.XXX}" tokens to ansi values or clear them if do_color is false.
static const struct
{
const char *name;
const char *value;
} g_color_tokens[] =
{
#define _TO_STR2(_val) #_val
#define _TO_STR(_val) _TO_STR2(_val)
{ "fg.black}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLACK) ANSI_ESC_END },
{ "fg.red}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_RED) ANSI_ESC_END },
{ "fg.green}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_GREEN) ANSI_ESC_END },
{ "fg.yellow}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_YELLOW) ANSI_ESC_END },
{ "fg.blue}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_BLUE) ANSI_ESC_END },
{ "fg.purple}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_PURPLE) ANSI_ESC_END },
{ "fg.cyan}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_CYAN) ANSI_ESC_END },
{ "fg.white}", ANSI_ESC_START _TO_STR(ANSI_FG_COLOR_WHITE) ANSI_ESC_END },
{ "bg.black}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLACK) ANSI_ESC_END },
{ "bg.red}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_RED) ANSI_ESC_END },
{ "bg.green}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_GREEN) ANSI_ESC_END },
{ "bg.yellow}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_YELLOW) ANSI_ESC_END },
{ "bg.blue}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_BLUE) ANSI_ESC_END },
{ "bg.purple}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_PURPLE) ANSI_ESC_END },
{ "bg.cyan}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_CYAN) ANSI_ESC_END },
{ "bg.white}", ANSI_ESC_START _TO_STR(ANSI_BG_COLOR_WHITE) ANSI_ESC_END },
{ "normal}", ANSI_ESC_START _TO_STR(ANSI_CTRL_NORMAL) ANSI_ESC_END },
{ "bold}", ANSI_ESC_START _TO_STR(ANSI_CTRL_BOLD) ANSI_ESC_END },
{ "faint}", ANSI_ESC_START _TO_STR(ANSI_CTRL_FAINT) ANSI_ESC_END },
{ "italic}", ANSI_ESC_START _TO_STR(ANSI_CTRL_ITALIC) ANSI_ESC_END },
{ "underline}", ANSI_ESC_START _TO_STR(ANSI_CTRL_UNDERLINE) ANSI_ESC_END },
{ "slow-blink}", ANSI_ESC_START _TO_STR(ANSI_CTRL_SLOW_BLINK) ANSI_ESC_END },
{ "fast-blink}", ANSI_ESC_START _TO_STR(ANSI_CTRL_FAST_BLINK) ANSI_ESC_END },
{ "negative}", ANSI_ESC_START _TO_STR(ANSI_CTRL_IMAGE_NEGATIVE) ANSI_ESC_END },
{ "conceal}", ANSI_ESC_START _TO_STR(ANSI_CTRL_CONCEAL) ANSI_ESC_END },
{ "crossed-out}", ANSI_ESC_START _TO_STR(ANSI_CTRL_CROSSED_OUT) ANSI_ESC_END },
#undef _TO_STR
#undef _TO_STR2
};
static const char tok_hdr[] = "${ansi.";
std::string fmt;
for (const char *p = format; *p; ++p)
{
const char *tok_start = strstr (p, tok_hdr);
if (!tok_start)
{
fmt.append (p, strlen(p));
break;
}
fmt.append (p, tok_start - p);
p = tok_start;
const char *tok_str = tok_start + sizeof(tok_hdr) - 1;
for (size_t i = 0; i < sizeof(g_color_tokens) / sizeof(g_color_tokens[0]); ++i)
{
if (!strncmp (tok_str, g_color_tokens[i].name, strlen(g_color_tokens[i].name)))
{
if (do_color)
fmt.append (g_color_tokens[i].value);
p = tok_str + strlen (g_color_tokens[i].name) - 1;
break;
}
}
}
return fmt;
}
}
}

View File

@ -1056,6 +1056,22 @@ SBDebugger::GetUseExternalEditor ()
return false;
}
bool
SBDebugger::SetUseColor (bool value)
{
if (m_opaque_sp)
return m_opaque_sp->SetUseColor (value);
return false;
}
bool
SBDebugger::GetUseColor () const
{
if (m_opaque_sp)
return m_opaque_sp->GetUseColor ();
return false;
}
bool
SBDebugger::GetDescription (SBStream &description)
{

View File

@ -131,6 +131,7 @@ g_properties[] =
{ "term-width", OptionValue::eTypeSInt64 , true, 80 , NULL, NULL, "The maximum number of columns to use for displaying text." },
{ "thread-format", OptionValue::eTypeString , true, 0 , DEFAULT_THREAD_FORMAT, NULL, "The default thread format string to use when displaying thread information." },
{ "use-external-editor", OptionValue::eTypeBoolean, true, false, NULL, NULL, "Whether to use an external editor or not." },
{ "use-color", OptionValue::eTypeBoolean, true, true , NULL, NULL, "Whether to use Ansi color codes or not." },
{ NULL, OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
};
@ -148,7 +149,8 @@ enum
ePropertyStopLineCountBefore,
ePropertyTerminalWidth,
ePropertyThreadFormat,
ePropertyUseExternalEditor
ePropertyUseExternalEditor,
ePropertyUseColor,
};
//
@ -186,9 +188,17 @@ Debugger::SetPropertyValue (const ExecutionContext *exe_ctx,
if (strcmp(property_path, g_properties[ePropertyPrompt].name) == 0)
{
const char *new_prompt = GetPrompt();
std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
if (str.length())
new_prompt = str.c_str();
EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));
GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
}
else if (strcmp(property_path, g_properties[ePropertyUseColor].name) == 0)
{
// use-color changed. Ping the prompt so it can reset the ansi terminal codes.
SetPrompt (GetPrompt());
}
else if (is_load_script && target_sp && load_script_old_value == eLoadScriptFromSymFileWarn)
{
if (target_sp->TargetProperties::GetLoadScriptFromSymbolFile() == eLoadScriptFromSymFileTrue)
@ -244,6 +254,9 @@ Debugger::SetPrompt(const char *p)
const uint32_t idx = ePropertyPrompt;
m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
const char *new_prompt = GetPrompt();
std::string str = lldb_utility::ansi::FormatAnsiTerminalCodes (new_prompt, GetUseColor());
if (str.length())
new_prompt = str.c_str();
EventSP prompt_change_event_sp (new Event(CommandInterpreter::eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
GetCommandInterpreter().BroadcastEvent (prompt_change_event_sp);
}
@ -297,6 +310,22 @@ Debugger::SetUseExternalEditor (bool b)
return m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
}
bool
Debugger::GetUseColor () const
{
const uint32_t idx = ePropertyUseColor;
return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
}
bool
Debugger::SetUseColor (bool b)
{
const uint32_t idx = ePropertyUseColor;
bool ret = m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
SetPrompt (GetPrompt());
return ret;
}
uint32_t
Debugger::GetStopSourceLineCount (bool before) const
{
@ -629,6 +658,11 @@ Debugger::Debugger (lldb::LogOutputCallback log_callback, void *baton) :
OptionValueSInt64 *term_width = m_collection_sp->GetPropertyAtIndexAsOptionValueSInt64 (NULL, ePropertyTerminalWidth);
term_width->SetMinimumValue(10);
term_width->SetMaximumValue(1024);
// Turn off use-color if this is a dumb terminal.
const char *term = getenv ("TERM");
if (term && !strcmp (term, "dumb"))
SetUseColor (false);
}
Debugger::~Debugger ()
@ -1140,13 +1174,12 @@ TestPromptFormats (StackFrame *frame)
ExecutionContext exe_ctx;
frame->CalculateExecutionContext(exe_ctx);
const char *end = NULL;
if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, &end))
if (Debugger::FormatPrompt (prompt_format, &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s))
{
printf("%s\n", s.GetData());
}
else
{
printf ("error: at '%s'\n", end);
printf ("what we got: %s\n", s.GetData());
}
}
@ -1323,8 +1356,8 @@ ExpandIndexedExpression (ValueObject* valobj,
return item;
}
bool
Debugger::FormatPrompt
static bool
FormatPromptRecurse
(
const char *format,
const SymbolContext *sc,
@ -1339,6 +1372,7 @@ Debugger::FormatPrompt
bool success = true;
const char *p;
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
for (p = format; *p != '\0'; ++p)
{
if (realvalobj)
@ -1372,8 +1406,8 @@ Debugger::FormatPrompt
StreamString sub_strm;
++p; // Skip the '{'
if (FormatPrompt (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
if (FormatPromptRecurse (p, sc, exe_ctx, addr, sub_strm, &p, valobj))
{
// The stream had all it needed
s.Write(sub_strm.GetData(), sub_strm.GetSize());
@ -1711,7 +1745,7 @@ Debugger::FormatPrompt
if (!special_directions)
var_success &= item->DumpPrintableRepresentation(s,val_obj_display, custom_format);
else
var_success &= FormatPrompt(special_directions, sc, exe_ctx, addr, s, NULL, item);
var_success &= FormatPromptRecurse(special_directions, sc, exe_ctx, addr, s, NULL, item);
if (--max_num_children == 0)
{
@ -1735,214 +1769,6 @@ Debugger::FormatPrompt
format_addr = *addr;
}
}
else if (::strncmp (var_name_begin, "ansi.", strlen("ansi.")) == 0)
{
var_success = true;
var_name_begin += strlen("ansi."); // Skip the "ansi."
if (::strncmp (var_name_begin, "fg.", strlen("fg.")) == 0)
{
var_name_begin += strlen("fg."); // Skip the "fg."
if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_fg_black,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_fg_red,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_fg_green,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_fg_yellow,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_fg_blue,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_fg_purple,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_fg_cyan,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_fg_white,
lldb_utility::ansi::k_escape_end);
}
else
{
var_success = false;
}
}
else if (::strncmp (var_name_begin, "bg.", strlen("bg.")) == 0)
{
var_name_begin += strlen("bg."); // Skip the "bg."
if (::strncmp (var_name_begin, "black}", strlen("black}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_bg_black,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "red}", strlen("red}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_bg_red,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "green}", strlen("green}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_bg_green,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "yellow}", strlen("yellow}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_bg_yellow,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "blue}", strlen("blue}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_bg_blue,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "purple}", strlen("purple}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_bg_purple,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "cyan}", strlen("cyan}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_bg_cyan,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "white}", strlen("white}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_bg_white,
lldb_utility::ansi::k_escape_end);
}
else
{
var_success = false;
}
}
else if (::strncmp (var_name_begin, "normal}", strlen ("normal}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_normal,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "bold}", strlen("bold}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_bold,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "faint}", strlen("faint}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_faint,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "italic}", strlen("italic}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_italic,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "underline}", strlen("underline}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_underline,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "slow-blink}", strlen("slow-blink}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_slow_blink,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "fast-blink}", strlen("fast-blink}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_fast_blink,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "negative}", strlen("negative}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_negative,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "conceal}", strlen("conceal}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_conceal,
lldb_utility::ansi::k_escape_end);
}
else if (::strncmp (var_name_begin, "crossed-out}", strlen("crossed-out}")) == 0)
{
s.Printf ("%s%s%s",
lldb_utility::ansi::k_escape_start,
lldb_utility::ansi::k_ctrl_crossed_out,
lldb_utility::ansi::k_escape_end);
}
else
{
var_success = false;
}
}
break;
case 'p':
@ -2631,6 +2457,24 @@ Debugger::FormatPrompt
return success;
}
bool
Debugger::FormatPrompt
(
const char *format,
const SymbolContext *sc,
const ExecutionContext *exe_ctx,
const Address *addr,
Stream &s,
ValueObject* valobj
)
{
bool use_color = exe_ctx ? exe_ctx->GetTargetRef().GetDebugger().GetUseColor() : true;
std::string format_str = lldb_utility::ansi::FormatAnsiTerminalCodes (format, use_color);
if (format_str.length())
format = format_str.c_str();
return FormatPromptRecurse (format, sc, exe_ctx, addr, s, NULL, valobj);
}
void
Debugger::SetLoggingCallback (lldb::LogOutputCallback log_callback, void *baton)
{

View File

@ -516,5 +516,5 @@ lldb_private::formatters::LibcxxContainerSummaryProvider (ValueObject& valobj, S
return false;
stream.Printf("0x%016llx ", value);
}
return Debugger::FormatPrompt("size=${svar%#}", NULL, NULL, NULL, stream, NULL, &valobj);
return Debugger::FormatPrompt("size=${svar%#}", NULL, NULL, NULL, stream, &valobj);
}

View File

@ -113,7 +113,7 @@ StringSummaryFormat::FormatObject (ValueObject *valobj,
}
else
{
if (Debugger::FormatPrompt(m_format.c_str(), &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, NULL, valobj))
if (Debugger::FormatPrompt(m_format.c_str(), &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, valobj))
{
retval.assign(s.GetString());
return true;

View File

@ -1282,13 +1282,12 @@ StackFrame::DumpUsingSettingsFormat (Stream *strm)
GetSymbolContext(eSymbolContextEverything);
ExecutionContext exe_ctx (shared_from_this());
const char *end = NULL;
StreamString s;
const char *frame_format = NULL;
Target *target = exe_ctx.GetTargetPtr();
if (target)
frame_format = target->GetDebugger().GetFrameFormat();
if (frame_format && Debugger::FormatPrompt (frame_format, &m_sc, &exe_ctx, NULL, s, &end))
if (frame_format && Debugger::FormatPrompt (frame_format, &m_sc, &exe_ctx, NULL, s))
{
strm->Write(s.GetData(), s.GetSize());
}

View File

@ -1747,13 +1747,11 @@ Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
assert (thread_format);
const char *end = NULL;
Debugger::FormatPrompt (thread_format,
frame_sp ? &frame_sc : NULL,
&exe_ctx,
NULL,
strm,
&end);
strm);
}
void

View File

@ -105,7 +105,9 @@ static OptionDefinition g_options[] =
"Tells the debugger to open source files using the host's \"external editor\" mechanism." },
{ LLDB_3_TO_5, false, "no-lldbinit" , 'x', no_argument , 0, eArgTypeNone,
"Do not automatically parse any '.lldbinit' files." },
{ LLDB_OPT_SET_6, true , "python-path" , 'P', no_argument , 0, eArgTypeNone,
{ LLDB_3_TO_5, false, "no-use-colors" , 'o', no_argument , 0, eArgTypeNone,
"Do not use colors." },
{ LLDB_OPT_SET_6, true , "python-path" , 'P', no_argument , 0, eArgTypeNone,
"Prints out the path to the lldb.py file for this version of lldb." },
{ 0, false, NULL , 0 , 0 , 0, eArgTypeNone, NULL }
};
@ -621,6 +623,10 @@ Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exit)
m_debugger.SkipAppInitFiles (true);
break;
case 'o':
m_debugger.SetUseColor (false);
break;
case 'f':
{
SBFileSpec file(optarg);