This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Add support for a "global" lldbinit file
ClosedPublic

Authored by labath on Feb 15 2022, 4:48 AM.

Details

Summary

This patch adds introduces a new kind of an lldbinit file. Unlike the
lldbinit in the home directory (useful for customizing lldb to the needs
of a particular user), or the cwd lldbinit file (useful for
project-specific settings), this file can be used to customize an entire
lldb installation to a particular environment.

The feature is enabled at build time, by setting the
LLDB_GLOBAL_INIT_DIRECTORY variable to a path to a directory which
should contain an "lldbinit" file. Lldb will then load the file at
startup, if it exists, and if automatic init loading has not been
disabled. Relative paths will be resolved (at runtime) relative to the
location of the lldb library (liblldb or LLDB.framework).

The system-wide lldbinit file will be loaded first, before any
$HOME/.lldbinit and $CWD/.lldbinit files are processed, so that those
can override any system-wide settings.

More information can be found on the RFC thread at
https://discourse.llvm.org/t/rfc-system-wide-lldbinit/59933.

Diff Detail

Event Timeline

labath created this revision.Feb 15 2022, 4:48 AM
labath requested review of this revision.Feb 15 2022, 4:48 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2022, 4:48 AM

This is in line with the existing ways of sourcing init files in LLDB and has all the things we discussed in the RFC. I left few nits but this all looks good to me. I'll hold off on accepting so it continues to show up in the other reviewer's queue.

lldb/include/lldb/Interpreter/CommandInterpreter.h
256

SourceInitFileSystem for consistency with the other two? Or maybe you were planning to change the other two in another patch?

lldb/source/API/SBCommandInterpreter.cpp
426–428

Nit: all the other functions should have a newline because that's what lldb-instr generates.

lldb/source/Interpreter/CommandInterpreter.cpp
2385–2393

Why not put the ifdef around the m_skip_lldbinit_files check?

LGTM. I agree with Jonas about keeping the naming consistent. I find it easier to spot the differentiating bits in a series of function names if they are at the beginning or the end, but that's a very mild preference.

wallace added inline comments.Feb 15 2022, 1:28 PM
lldb/include/lldb/Interpreter/CommandInterpreter.h
256

calling it SourceInitFileSystem is a bit confusing. What about using Global instead of System to avoid naming issues?

This looks good to me. Just a few things to possibly think about:

  • Maybe we would want addition system init files for different workflows and then we would start lldb with a new option like "lldb --workflow qemu" and it would load the system ".lldbinit-qemu" init file. That way we could have many supported workflows from one distribution. Here at Facebook we would have at least 3 that I know of: "fb-ios", "fb-android" and "fb-server". We would still have a system ".lldbinit" for global settings of course.
  • We were currently doing stuff like this in python where it would auto import some of our facebook specific python modules when the debugger starts up, would that be useful as well? It wouldn't really be needed here because we could just "command script import" in the init files

This looks good to me. Just a few things to possibly think about:

  • Maybe we would want addition system init files for different workflows and then we would start lldb with a new option like "lldb --workflow qemu" and it would load the system ".lldbinit-qemu" init file. That way we could have many supported workflows from one distribution. Here at Facebook we would have at least 3 that I know of: "fb-ios", "fb-android" and "fb-server". We would still have a system ".lldbinit" for global settings of course.

You are correctly guessing that this is motivated (at least partially, anyway) by the qemu patchset, but the idea there is to have a plain "lldb" just work. So, I don't think this would be interesting for us right now.

Nonetheless, I do find that idea intriguing. We would need to reconciliate this somehow with the existing concept of application-specific lldbinit files -- I think having both would be too much. Perhaps if we rename the app-specific files to "workflow-specific init files", and have the default workflow be derived from the application name... I don't think this would work right now, as we probably realpath everything, but if we avoided that then we could even allow using symlinks to automatically choose different workflows (ln -s lldb lldb.qemu; ./lldb.qemu could launch a "qemu" workflow).

(Of course, at that point, one could just make lldb.qemu a shell script which effectively does a exec lldb -S qemu.lldbinit "$@", and avoid all this extra infrastructure.)

  • We were currently doing stuff like this in python where it would auto import some of our facebook specific python modules when the debugger starts up, would that be useful as well? It wouldn't really be needed here because we could just "command script import" in the init files

Right now, it looks like all of our setup will be done in python as well, and our lldbinit file will probably consist of a single command script import --relative-to-command-file line. Going straight to python would probably make things easier for us, but this seemed to fit in better into the existing lldb feature set.

lldb/include/lldb/Interpreter/CommandInterpreter.h
256

Yeah, that was the reason why I changed the word order. We can just call it "global" instead.

lldb/source/Interpreter/CommandInterpreter.cpp
2385–2393

It just happened to fall out that way. I agree switching the order looks better.

labath updated this revision to Diff 409196.Feb 16 2022, 3:27 AM

Use the term "global" and rename functions to make it more consistent with
existing code.

It's worth noting that (as a part of the consistency), I have changed the cmake
variable to point to a directory instead of a file. This means that the init
file name is currently hardcoded -- currently it's "lldbinit", but we could
change that into something more complex to support the workflow feature.

In particular, I note the fact that the file name does not include a leading
dot, as I believe is the practice for global configuration files.

labath retitled this revision from [lldb] Add support for a "system-wide" lldbinit file to [lldb] Add support for a "global" lldbinit file.Feb 16 2022, 3:28 AM
labath edited the summary of this revision. (Show Details)
labath updated this revision to Diff 409197.Feb 16 2022, 3:30 AM
labath marked 4 inline comments as done.
labath edited the summary of this revision. (Show Details)

And add the extra blank line.

It's worth noting that (as a part of the consistency), I have changed the cmake
variable to point to a directory instead of a file. This means that the init
file name is currently hardcoded -- currently it's "lldbinit", but we could
change that into something more complex to support the workflow feature.

In particular, I note the fact that the file name does not include a leading
dot, as I believe is the practice for global configuration files.

Specifying a directory instead of a file makes sense. Why omit the leading dot though? I can think of a few reasons (you can specify a subdir unlike home, it's probably not something the user modifies unlike the one in home and cwd), but still it seems needlessly confusing to me.

Specifying a directory instead of a file makes sense. Why omit the leading dot though? I can think of a few reasons (you can specify a subdir unlike home, it's probably not something the user modifies unlike the one in home and cwd), but still it seems needlessly confusing to me.

I did it mainly because (I believe) that is common practice (so, *not* doing it could be confusing), and I believe the reason is precisely the one you mention -- as it's going to be in a global directory, you don't need it to be hidden (quite the opposite).

If we take bash as an example, here are some of the files it accesses:
~/.terminfo vs /etc/terminfo (a directory) -- done through the terminfo library, so which is used by lldb as well
~/.bashrc vs /etc/bash/bashrc
~/.dir_colors vs /etc/DIR_COLORS -- this isn't hardcoded in bash, but accessed from /etc/bash/bashrc
~/.inputrc vs /etc/inputrc
~/.bash_profile vs /etc/profile

JDevlieghere accepted this revision.Feb 17 2022, 9:21 AM

Specifying a directory instead of a file makes sense. Why omit the leading dot though? I can think of a few reasons (you can specify a subdir unlike home, it's probably not something the user modifies unlike the one in home and cwd), but still it seems needlessly confusing to me.

I did it mainly because (I believe) that is common practice (so, *not* doing it could be confusing), and I believe the reason is precisely the one you mention -- as it's going to be in a global directory, you don't need it to be hidden (quite the opposite).

If we take bash as an example, here are some of the files it accesses:
~/.terminfo vs /etc/terminfo (a directory) -- done through the terminfo library, so which is used by lldb as well
~/.bashrc vs /etc/bash/bashrc
~/.dir_colors vs /etc/DIR_COLORS -- this isn't hardcoded in bash, but accessed from /etc/bash/bashrc
~/.inputrc vs /etc/inputrc
~/.bash_profile vs /etc/profile

Good point, I was thinking about it too narrowly, strictly from an LLDB perspective.

LGTM!

This revision is now accepted and ready to land.Feb 17 2022, 9:21 AM
clayborg accepted this revision.Feb 17 2022, 2:13 PM
This revision was automatically updated to reflect the committed changes.