Skip to content

[lldb] print a notice when source list paging reaches the end of th… #137515

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

Merged
merged 1 commit into from
May 8, 2025

Conversation

hapeeeeee
Copy link
Contributor

This PR fixes the issue where the list command does not output a prompt when reaching the end of the file.
Closes #128507.

@hapeeeeee hapeeeeee requested a review from JDevlieghere as a code owner April 27, 2025 13:45
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the lldb label Apr 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 27, 2025

@llvm/pr-subscribers-lldb

Author: None (hapeeeeee)

Changes

This PR fixes the issue where the list command does not output a prompt when reaching the end of the file.
Closes #128507.


Full diff: https://github.com/llvm/llvm-project/pull/137515.diff

2 Files Affected:

  • (modified) lldb/source/Core/SourceManager.cpp (+5-2)
  • (added) lldb/test/Shell/Commands/command-list-reach-end-of-file.test (+16)
diff --git a/lldb/source/Core/SourceManager.cpp b/lldb/source/Core/SourceManager.cpp
index d63d42de14e80..b57d8b3e20316 100644
--- a/lldb/source/Core/SourceManager.cpp
+++ b/lldb/source/Core/SourceManager.cpp
@@ -360,8 +360,11 @@ size_t SourceManager::DisplayMoreWithLineNumbers(
     GetDefaultFileAndLine();
 
   if (last_file_sp) {
-    if (m_last_line == UINT32_MAX)
-      return 0;
+    if (m_last_line == UINT32_MAX) {
+      Stream::ByteDelta delta(*s);
+      s->Printf("note: reached the end of current file, no more to page\n");
+      return *delta;
+    }
 
     if (reverse && m_last_line == 1)
       return 0;
diff --git a/lldb/test/Shell/Commands/command-list-reach-end-of-file.test b/lldb/test/Shell/Commands/command-list-reach-end-of-file.test
new file mode 100644
index 0000000000000..61237ce4af542
--- /dev/null
+++ b/lldb/test/Shell/Commands/command-list-reach-end-of-file.test
@@ -0,0 +1,16 @@
+# RUN: %clang_host -g -O0 %S/Inputs/sigchld.c -o %t.out
+# RUN: %lldb %t.out -b -s %s | FileCheck %s
+
+b main
+r
+list
+# CHECK: assert (child_pid != -1);
+
+list
+# CHECK: printf("signo = %d\n", SIGCHLD);
+
+list 
+# CHECK: return 0;
+
+list 
+# CHECK: note: reached the end of current file, no more to page

@jimingham
Copy link
Collaborator

It seems wrong to have the source manager pretend there are more lines in the file with the content "there are no more lines in this file". At some point, someone is going to want to know that there are no more lines and this artificial content will be confusing.

It would be better to have the source manager return some kind of error and have the printer cons up the user-facing message.

@hapeeeeee hapeeeeee force-pushed the issue-list-no-source branch from 2d66140 to cc39f34 Compare April 29, 2025 02:53
@hapeeeeee
Copy link
Contributor Author

It seems wrong to have the source manager pretend there are more lines in the file with the content "there are no more lines in this file". At some point, someone is going to want to know that there are no more lines and this artificial content will be confusing.

It would be better to have the source manager return some kind of error and have the printer cons up the user-facing message.

I believe you are correct. I have updated the code so that the prompt output is now returned by SourceManager, and I have differentiated the causes. Could you please review my commit again? @jimingham

Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's better, but checking something the SourceManager calls the last line against UINT32_MAX seem to me too much implementation detail. I also can't see any other reason why m_last_line would be useful outside the SourceManager.

So maybe a better API would be

bool SourceManager::AtLastLine() { return m_last_line == UINT32_MAX; }

Then client code doesn't have to know how the SourceManager manages its paging through the source file.

Also, it would be good in your test to page twice past the end of the source buffer, not just once. That SHOULD just present the same error again, but it would be good to assert that it does.

@hapeeeeee hapeeeeee requested a review from jimingham April 29, 2025 23:58
@hapeeeeee hapeeeeee force-pushed the issue-list-no-source branch from cc39f34 to edd562c Compare April 30, 2025 01:36
@hapeeeeee
Copy link
Contributor Author

That's better, but checking something the SourceManager calls the last line against UINT32_MAX seem to me too much implementation detail. I also can't see any other reason why m_last_line would be useful outside the SourceManager.

So maybe a better API would be

bool SourceManager::AtLastLine() { return m_last_line == UINT32_MAX; }

Then client code doesn't have to know how the SourceManager manages its paging through the source file.

As you suggested, I’ve encapsulated the AsLastLine for checking whether a line is the last one, and reused this API in the SourceManager to replace the original logic for determining the last line.

Also, it would be good in your test to page twice past the end of the source buffer, not just once. That SHOULD just present the same error again, but it would be good to assert that it does.

I added a second list command when reaching the end of the file to ensure the output is consistent across both attempts.

Could you please review my commit again? @jimingham

@@ -1067,7 +1067,14 @@ class CommandObjectSourceList : public CommandObjectParsed {
&result.GetOutputStream(), m_options.num_lines,
m_options.reverse, GetBreakpointLocations())) {
result.SetStatus(eReturnStatusSuccessFinishResult);
} else {
if (target.GetSourceManager().AsLastLine(m_options.reverse)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you go in reverse and try to page past the beginning of the file we really shouldn't print "Reached the end to the file". Maybe:

result.AppendNoteWithFormat("Reached {0} of the file, no more to page", m_options.reverse ? "beginning" : "end");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you go in reverse and try to page past the beginning of the file we really shouldn't print "Reached the end to the file". Maybe:如果您反向操作并尝试翻页超过文件开头,我们真的不应该打印 "已到达文件末尾"。或许:

result.AppendNoteWithFormat("Reached {0} of the file, no more to page", m_options.reverse ? "beginning" : "end");result.AppendNoteWithFormat("已到达文件的第{0}处,没有更多可翻页", m_options.reverse ? "开始" : "结束");

I have already added different prompt messages based on reverse printing.

list
# CHECK: note: Reached end of the file, no more to page

list
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good. There should also be a test for scrolling past the beginning of the file in reverse, since you added a good behavior for that.

Copy link
Contributor Author

@hapeeeeee hapeeeeee May 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test file command-list-reach-beginning-of-file.test for when the beginning of the file is reached, and ran the test twice, getting expected result both times.

@hapeeeeee hapeeeeee force-pushed the issue-list-no-source branch from edd562c to 3c0bd90 Compare May 1, 2025 01:16
@hapeeeeee hapeeeeee requested a review from jimingham May 1, 2025 01:30
Copy link
Collaborator

@jimingham jimingham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -155,6 +155,9 @@ class SourceManager {
~SourceManager();

FileSP GetLastFile() { return GetFile(m_last_support_file_sp); }
bool AsLastLine(bool reverse) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a typo?

Suggested change
bool AsLastLine(bool reverse) {
bool AtLastLine(bool reverse) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a typo?

Yes, it is a mistake. I have fixed it. Could you please review my commit again? @JDevlieghere

@hapeeeeee hapeeeeee force-pushed the issue-list-no-source branch from 3c0bd90 to 0d44818 Compare May 2, 2025 00:51
@hapeeeeee
Copy link
Contributor Author

Hi @JDevlieghere , I've corrected the typos. Could you please review my changes again?

@hapeeeeee
Copy link
Contributor Author

Hi @JDevlieghere @jinmingjian , all required checks have passed and the PR has received approvals. Would you mind helping merge this PR? Thanks!

@JDevlieghere JDevlieghere merged commit b5674cb into llvm:main May 8, 2025
10 checks passed
Copy link

github-actions bot commented May 8, 2025

@hapeeeeee Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

DavidSpickett added a commit that referenced this pull request May 9, 2025
Added in #137515,
as the source uses unistd.h which isn't present there.

| C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\llvm-project\lldb\test\Shell\Commands/Inputs/sigchld.c:4:10: fatal error: 'unistd.h' file not found
|     4 | #include <unistd.h>
|       |          ^~~~~~~~~~
| 1 error generated.
@DavidSpickett
Copy link
Collaborator

I've skipped the new tests on Windows because we don't have the headers the sigchld example uses.

(no notification of the failure was sent because we were already failing some lldb-dap tests)

llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request May 9, 2025
Added in llvm/llvm-project#137515,
as the source uses unistd.h which isn't present there.

| C:\Users\tcwg\llvm-worker\lldb-aarch64-windows\llvm-project\lldb\test\Shell\Commands/Inputs/sigchld.c:4:10: fatal error: 'unistd.h' file not found
|     4 | #include <unistd.h>
|       |          ^~~~~~~~~~
| 1 error generated.
@hapeeeeee
Copy link
Contributor Author

I've skipped the new tests on Windows because we don't have the headers the sigchld example uses.我跳过了 Windows 上的新测试,因为我们没有 sigchld 示例使用的头文件。

(no notification of the failure was sent because we were already failing some lldb-dap tests)(没有发送失败通知,因为我们已经有一些 lldb-dap 测试失败了)

The new tests are only applied to the command line, and they should be cross-platform. Maybe I should choose a more general source code for testing?

@DavidSpickett
Copy link
Collaborator

That would be great if you can, there isn't one in that folder right now but you can find another elsewhere, or add a new file. I didn't want to go changing all the check lines myself in case I broke something.

@hapeeeeee
Copy link
Contributor Author

That would be great if you can, there isn't one in that folder right now but you can find another elsewhere, or add a new file. I didn't want to go changing all the check lines myself in case I broke something.如果你能做到那就太好了,那个文件夹里现在没有,但你可以在别处找到另一个,或者添加一个新文件。我不想自己去改所有的检查行,以免破坏了什么。

If I modified the test file, should I create a new PR to fix it?

@DavidSpickett
Copy link
Collaborator

Yes please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

lldb should print a notice when source list paging reaches the end of the file
5 participants