Skip to content

[lldb] enable stdin with stdin command #52

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
Mar 27, 2025

Conversation

Vipul-Cariappa
Copy link
Contributor

@Vipul-Cariappa Vipul-Cariappa commented Mar 27, 2025

This is a tricky implementation.
Say the program uses scanf to read input. The user will need to populate stdin before the call to scanf. Otherwise, the program will freeze, because scanf is trying to read, but stdin is not populated.

Note: Using the stdin command with gdb will raise AttributeError for now (Not implemented with the gdb backend).

Try with:

// a/fib.c
#include <stdio.h>
#include <stdlib.h>


int internal_fib(int a, int b, int n) {
    if (n < 2)
        return b;
    return internal_fib(b, a + b, n - 1);
}

int fib(int x) {
    return internal_fib(0, 1, x);
}

int main(int argc, char *argv[]) {
    int x = 2;
    printf("Enter number: ");
    scanf("%d", &x);
    printf("%d\n", fib(x));
    return 0;
}
// b/fib.c
#include <stdio.h>
#include <stdlib.h>


int internal_fib(int a, int b, int n) {
    while (--n) {
        int tmp = a;
        a = b;
        b = tmp + b;
    }
    return a;
}

int fib(int x) {
    return internal_fib(0, 1, x);
}

int main(int argc, char *argv[]) {
    int x = 2;
    printf("Enter number: ");
    scanf("%d", &x);
    printf("%d\n", fib(x));
    return 0;
}

Example:

Screencast.From.2025-03-27.11-43-27.mp4

using `stdin` with gdb will raise AttributeError
@mvassilev mvassilev merged commit f30aadb into compiler-research:main Mar 27, 2025
@Vipul-Cariappa Vipul-Cariappa deleted the dev/lldb-stdin branch April 10, 2025 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants